2
使用矩阵缩放图像时
我有以下在扩展活动类的onCreate方法的代码片断:问题在ImageView的
ImageView view = (ImageView) findViewById(R.id.ImageView01);
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.sample_landscape, bmpFactoryOptions);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Log.v("IMAGEVIEWERMATRIX", "Display Width: " + display.getWidth());
Log.v("IMAGEVIEWERMATRIX", "Display Height: " + display.getHeight());
Log.v("IMAGEVIEWERMATRIX", "BMP Width: " + bmpFactoryOptions.outWidth);
Log.v("IMAGEVIEWERMATRIX", "BMP Height: " + bmpFactoryOptions.outHeight);
if (bmpFactoryOptions.outWidth > width || bmpFactoryOptions.outHeight > height) {
float heightRatio = (float) height/(float) bmpFactoryOptions.outHeight;
float widthRatio = (float) width/(float) bmpFactoryOptions.outWidth;
// WHY
heightRatio = heightRatio/1.5f;
widthRatio = widthRatio/1.5f;
Log.v("IMAGEVIEWERMATRIX", "heightRatio:" + heightRatio);
Log.v("IMAGEVIEWERMATRIX", "widthRatio: " + widthRatio);
float scale = widthRatio;
if (heightRatio < widthRatio) {
scale = heightRatio;
}
matrix.setScale(scale, scale);
Log.v("IMAGEVIEWERMATRIX", "Scale: " + scale);
} else {
Log.v("IMAGEVIEWERMATRIX", "NOTNOTNOT");
matrix.setTranslate(1f, 1f);
}
Bitmap realBmp = BitmapFactory.decodeResource(getResources(),
R.drawable.sample_landscape);
view.setImageBitmap(realBmp);
view.setImageMatrix(matrix);
当运行它,图像在屏幕上显示缩放的正确地安装。我的问题是,我必须调整1.5的规模,以便这是正确的。
heightRatio = heightRatio/1.5f;
widthRatio = widthRatio/1.5f;
我的直觉是,这与该设备的显示密度的事,但对我的生活,我不能完成我的身边,为什么我需要做的这头。
这里是布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/ImageView01" android:scaleType="matrix" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>