android - 如何在ImageView中将图像设置为正确比例
问题描述:
请看下面的图片。
我正在用代码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);
}
}
答
在这里,您正在使用的目标矩形要加载的位图的大小,而不是画布的全尺寸: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它们在任何情绪下都显示如此简单的任何图片 – Erik 2011-05-11 22:48:38
这是因为您在画布上绘图,因此您需要根据图像的比例以及图像的比例计算自己目标矩形的大小。如果您获得了正确的公式,那么它将根据方向变化正确调整大小。 – Lumis 2011-05-11 23:33:21