简体   繁体   English

android - 如何在ImageView中将图像设置为正确的比例

[英]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. 我正在使用代码belove从sdcard加载图像。

I cannot get the picture to automatically fill the ImageView. 我无法让图片自动填充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. 我尝试了很多android:ScaleType,如CENTER和FIT_XY但是。

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 我现在使用inSampleSize = 8但是ImageView如何自动将图像缩放到正确的高/宽比例并填充屏幕

Also another thing, You see the ExitText and Button is placed above the Imageview(I think). 另外一件事,你看到ExitText和Button被放置在Imageview上方(我想)。 I want them to be under and not block the ImageView. 我希望它们在下面而不是阻止ImageView。 Maybe i misunderstand the android layout technical. 也许我误解了android布局技术。

在此输入图像描述

DrawView.java 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 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()); 在这里,您使用目标矩形作为加载的位图的大小而不是画布的完整大小: 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: 为此你可以使用onSizeChanged:

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

  screenW = w;
  screenH = h;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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