在 ImageView 中使用矩阵缩放图像时出现问题

发布于 2024-10-07 06:09:35 字数 2290 浏览 3 评论 0原文

我在扩展 Activity 的类的 onCreate 方法中有以下代码片段:

 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>

I have the following snippet of code in the onCreate method of a class that extends Activity:

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

When I run it, the image appears on screen scaled to fit correctly. My issue is that I have to adjust the scale by 1.5 in order for this to be correct.

heightRatio = heightRatio / 1.5f;
widthRatio = widthRatio / 1.5f;

My intuition is that this has to do with the display density of the device but for the life of me, I can't wrap my head around why I would need to do this.

Here is the layout 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花浅忆 2024-10-14 06:09:35

好的,在这里回答我自己的问题。我看到的问题与我放置图像资源的位置有关。我仅将其放在 res/drawable-mdpi 文件夹中,并且我正在测试的设备是高密度的。因此,Android 会自动为我放大图像。为了纠正这种情况并直接处理像素,我将图像放置在 res/drawable-nodpi 文件夹中。

此行为在此页面上进行了描述: http://developer.android.com/guide/practices “Android 如何支持多屏幕”下的 /screens_support.html

Ok, answering my own question here. The issue that I was seeing has to do with where I put the image asset. I had it in the res/drawable-mdpi folder only and the device I was testing on was high density. Therefore, Android was scaling the image up for me automagically. To rectify the situation and deal with the pixels directly, I placed the image in the res/drawable-nodpi folder.

This behavior is described on this page: http://developer.android.com/guide/practices/screens_support.html under "How Android supports multiple screens".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文