请看下面的图片 我正在使用代码belove从sdcard加载图像。
我无法让图片自动填充ImageView。我将背景设置为绿色,仅突出显示问题。我尝试了很多android:ScaleType,如CENTER和FIT_XY但是。
我现在使用inSampleSize = 8但是ImageView如何自动将图像缩放为正确的高/宽比例并填充屏幕
另外,您可以看到ExitText和Button位于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);
}
}
答案 0 :(得分:2)
在这里,您使用目标矩形作为加载位图的大小而不是画布的完整大小:Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
您需要找出画布的大小。为此,您可以使用onSizeChanged:
@Override
public void onSizeChanged (int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
screenW = w;
screenH = h;
}