2014-09-06 34 views
1

我正在嘗試製作壁紙應用程序。 我在使用位圖設置壁紙的過程中遇到了很大的麻煩。我試圖找出一個星期的答案。用位圖設置壁紙避免裁剪並設置合身中心

我想在設置位圖牆紙像

  1. 避免作物
  2. scaleType:fit_center(居中對齊垂直,寬高比)

我如何能做到嗎?

P.S.我發現也許我可以使用Bitmap.createBitmap,但我的嘗試一遍又一遍地失敗。 我不知道我應該只使用WallPaperManager或兩者都Bitmap.createBitmp太。

靜態位圖createBitmap(位圖源,INT的x,INT Y,INT寬度,INT高度,矩陣M,布爾濾波器)

public void setScreenBitmap(final Bitmap bitmapInputed) 
{ 

    final WallpaperManager wpm = WallpaperManager.getInstance(myContext); 

    final Display display = ((Activity) this.myContext).getWindowManager().getDefaultDisplay(); 

    Point size = new Point(); 
    display.getSize(size); 
    int width = size.x; 
    int height = size.y; 


    try 
    { 
     wpm.setBitmap(Bitmap.createScaledBitmap(bitmapInputed, width, height, true)); 
    } 
    catch (IOException e) 
    { 
     Log.e(TAG+".setScreenBitmap", e.toString()); 
     e.printStackTrace(); 
    } 
    wpm.setWallpaperOffsetSteps(1.0f, 1.0f); 

    wpm.suggestDesiredDimensions(width, height); 

} 






09-06 20:38:15.563: W/System.err(4892): java.lang.IllegalArgumentException: x must be >= 0 
09-06 20:38:15.563: W/System.err(4892):  at android.graphics.Bitmap.checkXYSign(Bitmap.java:285) 
09-06 20:38:15.563: W/System.err(4892):  at android.graphics.Bitmap.createBitmap(Bitmap.java:580) 
09-06 20:38:15.568: W/System.err(4892):  at android.graphics.Bitmap.createBitmap(Bitmap.java:551) 
09-06 20:38:15.568: W/System.err(4892):  at com.myarena.util.MyWallpaperUtil.getBitmapFromCenterAndScreenSize(MyWallpaperUtil.java:459) 
09-06 20:38:15.568: W/System.err(4892):  at com.myarena.util.MyWallpaperUtil.setScreenBitmap(MyWallpaperUtil.java:485) 
09-06 20:38:15.568: W/System.err(4892):  at com.myarena.util.MyWallpaperUtil.changeWallpaper(MyWallpaperUtil.java:231) 
09-06 20:38:15.568: W/System.err(4892):  at com.myarena.controller.ControllerActivity$PlaceholderFragment$2.onClick(ControllerActivity.java:213) 
09-06 20:38:15.568: W/System.err(4892):  at android.view.View.performClick(View.java:4489) 
09-06 20:38:15.568: W/System.err(4892):  at android.view.View$PerformClick.run(View.java:18803) 
09-06 20:38:15.568: W/System.err(4892):  at android.os.Handler.handleCallback(Handler.java:730) 
09-06 20:38:15.568: W/System.err(4892):  at android.os.Handler.dispatchMessage(Handler.java:92) 
09-06 20:38:15.568: W/System.err(4892):  at android.os.Looper.loop(Looper.java:137) 
09-06 20:38:15.568: W/System.err(4892):  at android.app.ActivityThread.main(ActivityThread.java:5493) 
09-06 20:38:15.568: W/System.err(4892):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-06 20:38:15.568: W/System.err(4892):  at java.lang.reflect.Method.invoke(Method.java:525) 
09-06 20:38:15.568: W/System.err(4892):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209) 
09-06 20:38:15.568: W/System.err(4892):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025) 
09-06 20:38:15.568: W/System.err(4892):  at dalvik.system.NativeStart.main(Native Method) 

回答

2

如果你想從中心和同一畫面的位圖作物分辨率然後用下面的方法。 返回位圖與您的屏幕分辨率和裁剪相同。

For Ex。您的位圖尺寸480x820

您的設備尺寸480x800其返回480x800(前10像素和後10像素刪除後)。

您的設備尺寸800x1280其返回800x1280(前43像素和後43像素刪除後的比例)。

private Bitmap cropBitmapFromCenterAndScreenSize(Bitmap bitmap) { 
    float screenWidth, screenHeight; 
    float bitmap_width = bitmap.getWidth(), bitmap_height = bitmap 
      .getHeight(); 
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)) 
      .getDefaultDisplay(); 
    screenWidth = display.getWidth(); 
    screenHeight = display.getHeight(); 

    Log.i("TAG", "bitmap_width " + bitmap_width); 
    Log.i("TAG", "bitmap_height " + bitmap_height); 

    float bitmap_ratio = (float) (bitmap_width/bitmap_height); 
    float screen_ratio = (float) (screenWidth/screenHeight); 
    int bitmapNewWidth, bitmapNewHeight; 

    Log.i("TAG", "bitmap_ratio " + bitmap_ratio); 
    Log.i("TAG", "screen_ratio " + screen_ratio); 

    if (screen_ratio > bitmap_ratio) { 
     bitmapNewWidth = (int) screenWidth; 
     bitmapNewHeight = (int) (bitmapNewWidth/bitmap_ratio); 
    } else { 
     bitmapNewHeight = (int) screenHeight; 
     bitmapNewWidth = (int) (bitmapNewHeight * bitmap_ratio); 
    } 

    bitmap = Bitmap.createScaledBitmap(bitmap, bitmapNewWidth, 
      bitmapNewHeight, true); 

    Log.i("TAG", "screenWidth " + screenWidth); 
    Log.i("TAG", "screenHeight " + screenHeight); 
    Log.i("TAG", "bitmapNewWidth " + bitmapNewWidth); 
    Log.i("TAG", "bitmapNewHeight " + bitmapNewHeight); 

    int bitmapGapX, bitmapGapY; 
    bitmapGapX = (int) ((bitmapNewWidth - screenWidth)/2.0f); 
    bitmapGapY = (int) ((bitmapNewHeight - screenHeight)/2.0f); 

    Log.i("TAG", "bitmapGapX " + bitmapGapX); 
    Log.i("TAG", "bitmapGapY " + bitmapGapY); 

    bitmap = Bitmap.createBitmap(bitmap, bitmapGapX, bitmapGapY, 
      screenWidth,screenHeight); 
    return bitmap; 
} 
+0

感謝您回答我的問題大哥!但是,我的意思是'我不想裁剪我的位圖',並且我注意到知道(這麼晚)我寫了'match_parent width'意思是實際上match_parent的寬度或高度。對不起... – HoJunLee 2014-09-06 11:01:54

+0

我找到了我的自定義ur代碼..這將需要10分鐘... – HoJunLee 2014-09-06 11:04:25

+0

umm ...也許需要更多15min ... – HoJunLee 2014-09-06 11:11:15