2012-12-10 134 views
0

Hie。我正在研究动态壁纸,并且遇到了问题。我完成了我的壁纸中的视差效果。现在,问题是我的动态壁纸的位图(即静态背景)没有正确缩放。在一些屏幕中,宽度是合适的,但是在一些位图(即背景)中只出现一半。调整位图大小

我试过密度,windowmanager和px到dp的转换。

他们都没有为我工作。或者可能是我对它的态度不正确。

我需要相同的帮助。

代码段

this._backgroundImage = BitmapFactory.decodeResource(context.getResources(),R.drawable.scene, options); 

Bitmap background_image = Bitmap.createScaledBitmap(_backgroundImage, width, height, false); 

canvas.drawBitmap(this.background_image, 0, 0, null); 
+1

你可以发布一些代码snippt的.... – Sumant

+0

我已经编辑我的问题 –

+0

改变最后一行canvas.drawBitmap(background_image,0,0,NULL); – ben75

回答

0

尝试这种情况:

位图resizedBitmap = Bitmap.createScaledBitmap(BB,newWidth,newHeight,FALSE);

告诉我,如果它的工作。

+0

我已经做了相同的,但我无法找到如何根据不同的屏幕尺寸计算宽度和高度 –

+0

请参阅http://stackoverflow.com/questions/1016896/android-how-to-get- screen-dimensions – ben75

1

我用下面的方法有时回来。我不知道如果这些将是你或没有帮助..请检查这对你的作品

float scale = getResources().getDisplayMetrics().density; 
Display display = ((WindowManager) main.getSystemService(Context.WINDOW_SERVICE)) 
.getDefaultDisplay(); 
int WIDTH = display.getWidth(); 
int HEIGHT = display.getHeight(); 

public static Drawable resizeDrawable(Drawable d, float scale) { 
    Drawable drawable = null; 
    if (d != null) { 
     try { 
      Bitmap bitmap1 = ((BitmapDrawable) d).getBitmap(); 
      int width = 0; 
      int height = 0; 
      if (Math.min(WIDTH, HEIGHT) > 600) { 
       width = (int) (100 * scale + 0.5f); 
       height = (int) (100 * scale + 0.5f); 

      } else if (Math.min(WIDTH, HEIGHT) > 240) { 
       width = (int) (70 * scale + 0.5f); 
       height = (int) (70 * scale + 0.5f); 

      } else { 
       width = (int) (44 * scale + 0.5f); 
       height = (int) (44 * scale + 0.5f); 
      } 

      drawable = new BitmapDrawable(resizeBitmap(bitmap1, 
        width, height)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    return drawable; 
} 

请注意如果其他适用于该值在resizeDrawable方法条件通过试n错误(这适合我的应用程序)取..你可以根据屏幕时,您的目标

尝试其他值只是任意值
public static Bitmap resizeBitmap(final Bitmap bitmap, final int width, 
     final int height) { 
    final int oldWidth = bitmap.getWidth(); 
    final int oldHeight = bitmap.getHeight(); 
    final int newWidth = width; 
    final int newHeight = height; 

    // calculate the scale 
    final float scaleWidth = ((float) newWidth)/oldWidth; 
    final float scaleHeight = ((float) newHeight)/oldHeight; 

    // create a matrix for the manipulation 
    final Matrix matrix = new Matrix(); 
    // resize the Bitmap 
    matrix.postScale(scaleWidth, scaleHeight); 
    // if you want to rotate the Bitmap 

    // recreate the new Bitmap 
    final Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, 
      oldWidth, oldHeight, matrix, true); 

    return resizedBitmap; 
} 
+0

以及newWidth和newHeight是什么? screenWidth和screenHeight? –

0

你可以用它来获取屏幕大小和规模它相应地。

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth(); // deprecated 
int height = display.getHeight();