Android调整大小位图根据屏幕分辨率

时间:2015-05-27 18:04:33

标签: android

我正在开发一个更衣室应用程序,我需要将背景显示为手机壁纸,为此我使用下面的xml和Java代码,通过这个我可以在一些手机上管理分辨率,但分辨率较低手机图像变得紧张所以请检查我的代码,我怎样才能实现所有分辨率的手机:

我的xml图片视图是:

<ImageView
            android:id="@id/lock_iv_background"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="false"
            android:scaleType="fitXY" />

我也使用了android:scaleType =“centerInside”,但同样的拉伸问题也是如此。

Java代码:

WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
    Drawable wallpaperDrawable = wallpaperManager.getDrawable();
    // convert drawable to bitmap
    Bitmap bitmap = ((BitmapDrawable) wallpaperDrawable).getBitmap();
    getResizedBitmap(bitmap);

public void getResizedBitmap(Bitmap bm) {

    DisplayMetrics metrics = getApplicationContext().getResources()
            .getDisplayMetrics();
    // getWindowManager().getDefaultDisplay().getMetrics(metrics);

    // int width = bm.getWidth();
    // int h = bm.getHeight();

    float scaleWidth = metrics.scaledDensity;
    float scaleHeight = metrics.scaledDensity;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0,
            TimerPrefrences.getScreenSize("width", LockService.this),
            TimerPrefrences.getScreenSize("height", LockService.this),
            matrix, true);
    mViewBackground.setImageBitmap(resizedBitmap);
}

2 个答案:

答案 0 :(得分:0)

根本不需要调整图片大小。只需设置:

android:scaleType="fitCenter"

而不是

android:scaleType="fitXY"

答案 1 :(得分:0)

将此添加到您的ImageView,告诉它适合它的界限

android:adjustViewBounds="true"
相关问题