2015-05-27 140 views
0

我正在更衣室的应用程序,我需要显示背景作为手机壁纸,为了这个,我现在用的是下面的XML和Java代码,这个我能够管理的决议在某些手机上,但在较低分辨率的手机图片得到伸展,所以请看看我的代码,我如何能实现所有分辨率手机:Android的调整大小位图根据屏幕分辨率

我的XML图像的看法是:

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

我也用机器人: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); 
} 

回答

0

你并不需要在所有的调整图片的大小。只需设置:中

android:scaleType="fitCenter" 

代替

android:scaleType="fitXY" 
0

添加到您的ImageView告诉它,以适应它的边界

android:adjustViewBounds="true" 
相关问题