简体   繁体   中英

android - How to set image in ImageView to right proportions

Look at the picture below.
I'm loading an image from sdcard with code belove.

I cannot get the picture to automatically fill the ImageView. I set the background to color green only to highlight the problem. I have tried many android:ScaleType like CENTER and FIT_XY but.

Im now using inSampleSize = 8 but how can the ImageView automatically scale the image to be in the right hight/width proportions and fill the screen

Also another thing, You see the ExitText and Button is placed above the Imageview(I think). I want them to be under and not block the ImageView. Maybe i misunderstand the android layout technical.

在此输入图像描述

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>

Main Activity

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);
    }
}

Here you are using the destination rectangle to be the size of the loaded bitmap instead of the full size of canvas: Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

You need to find out the size of your canvas. For that you can use onSizeChanged:

@Override
public void onSizeChanged (int w, int h, int oldw, int oldh){
  super.onSizeChanged(w, h, oldw, oldh);

  screenW = w;
  screenH = h;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM