【问题标题】:android - How to set image in ImageView to right proportionsandroid - 如何将 ImageView 中的图像设置为正确的比例
【发布时间】:2011-08-23 16:44:35
【问题描述】:

看下图。
我正在使用代码 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);
    }
}

【问题讨论】:

    标签: android imageview


    【解决方案1】:

    在这里,您使用目标矩形作为加载位图的大小,而不是画布的完整大小: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;
    }
    

    【讨论】:

    • 谢谢@Lumis 成功了。图像是完美的,但在风景中它被拉伸了。猜猜我必须做一些计算,但这真的很难设置位图吗?我知道 Android.gallery 和 imageSwitcher 它们可以轻松地以各种心情显示任何图片
    • 这是因为你是在画布上绘制的,所以你需要根据图像的比例和位置来计算你的目标矩形的大小。如果您得到正确的公式,那么它将在方向更改时正确调整大小。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多