简体   繁体   English

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

[英]Android Resize Bitmap According to screen Resolution

I am working on a locker app where I need to show the background as the phone wallpaper, for this I am using the below xml and Java code, by this I am able to manage the resolution on some phones but on lower resolution phones Image getting stretched so please check my code that how can I achieve for all resolutions phones: 我正在开发一个更衣室应用程序,需要在其中将背景显示为手机壁纸,为此,我正在使用下面的xml和Java代码,这样我就可以管理某些手机的分辨率,但在较低分辨率的手机上拉伸,所以请检查我的代码,如何为所有分辨率的手机实现:

My xml Image view is: 我的xml图像视图是:

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

I have also used android:scaleType="centerInside" but the same stretching problem with this too. 我也使用了android:scaleType =“ centerInside”,但是同样存在拉伸问题。

Java Code: 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);
}

You don't need to resize your image at all. 您根本不需要调整图像大小。 Just set: 刚刚设置:

android:scaleType="fitCenter"

instead of 代替

android:scaleType="fitXY"

将此添加到您的ImageView中以使其适应边界

android:adjustViewBounds="true"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM