2015-05-27 141 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" 
相關問題