0
請看下面的圖片。
我正在用代碼belove從sdcard加載圖片。android - 如何在ImageView中將圖像設置爲正確比例
我無法讓圖片自動填充ImageView。我將背景設置爲綠色,以突出顯示問題。我曾嘗試過許多android:ScaleType,比如CENTER和FIT_XY。
林現在使用inSampleSize = 8,但如何能ImageView的自動縮放圖像是在右HIGHT /寬度比例和填充屏幕
另外另一件事,可以看到ExitText和按鈕被置於上方的Imageview(我認爲)。 我希望他們在下並不阻止ImageView。也許我誤解了android佈局技術。
DrawView.java
public class DrawView extends ImageView {
private Context ctx;
public DrawView(Context context, AttributeSet atts) {
super(context, atts);
this.ctx = context;
}
@Override
protected void onDraw(Canvas canvas) {
String filePath = Environment.getExternalStorageDirectory().toString() + "/PTPPservice/163693fe-7c48-4568-a082-00047123b9f1.2.IMAG2200.jpg";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(filePath,options);
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawBitmap(bitmap, null, rect,null);
}
}
Main1.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.hasse.move.DrawView
android:id="@+id/mainView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:background="#00FF00"
android:adjustViewBounds="true"
/>
<RelativeLayout
android:id="@+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="@+id/edittextaddtext"
android:layout_width="fill_parent"
android:layout_toLeftOf="@+id/btnsave"
android:layout_height="wrap_content"
android:text="wrap"
/>
<Button
android:id="@+id/btnsave"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
/>
</RelativeLayout>
</RelativeLayout>
主要活動
public class Main extends Activity {
DrawView theImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// draw the view
setContentView(R.layout.main1);
theImage = (DrawView) findViewById(R.id.mainView);
//do stuff
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
// Toast.makeText(this, "onConfigurationChanged",Toast.LENGTH_LONG).show();
super.onConfigurationChanged(newConfig);
}
}
謝謝@Lumis它的工作。圖像是完美的,但在橫向情緒中,它被拉長了。猜猜我必須做一些計算,但這真的很難設置一個位圖。我知道Android.gallery和imageSwitcher它們在任何情緒下都顯示如此簡單的任何圖片 – Erik 2011-05-11 22:48:38
這是因爲您在畫布上繪圖,因此您需要根據圖像的比例以及圖像的比例計算自己目標矩形的大小。如果您獲得了正確的公式,那麼它將根據方向變化正確調整大小。 – Lumis 2011-05-11 23:33:21