2012-02-10 35 views
0

我有一個位圖圖像。如何以編程方式創建更大的位圖?我試圖創建scaleBitMap,但它不顯示更大。如何從現有位圖創建更大的位圖

int newHeight=900; 
Bitmap bg1=Bitmap.createScaledBitmap(bg, width, newHeight, false); 
canvas.drawBitmap(bg1, null, new Rect(0, 0, getWidth(), newHeight), null); 

如何解決此問題?

+0

這是不工作時希望在內部使用scrollTo意見的滾動內容http://developer.android.com/reference/android/view/View.html#scrollTo%28int,%20int%29 – 2014-10-24 10:55:24

回答

2

也許你應該改爲嘗試這個辦法:

int newHeight=900; 
int newWidth= bg.getWidth; 
Bitmap bg1=Bitmap.createScaledBitmap(bg, newWidth, newHeight, true); 

總是值得一試

0

的Bitmap.createScaledBitmap()是做你想要什麼,問題是當你試圖把它拖到畫布。什麼是getWidth()返回?發佈其餘的代碼給我們一些上下文,我們可以找到你的問題。

+0

getWidth()給出屏幕的高度。 – 2012-02-10 05:40:26

0

試試這個代碼...

public Bitmap getResizeBitmap(Bitmap bitmap, int newHeight, int newWidth) 
     { 

      int width = bitmap.getWidth(); 
      int height = bitmap.getHeight(); 

      float scaleWidth = ((float)newWidth)/width; 
      float scaleHeight = ((float)newHeight)/height; 

      Matrix matrix = new Matrix(); 
      matrix.postScale(scaleWidth, scaleHeight); 
      Bitmap resizeBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true); 

      return resizeBitmap; 

     } 

然後在onCreate方法使用此...

myImage = Bitmap.createBitmap(BitmapFactory 
        .decodeResource(this.getResources(), R.drawable.mypic)); 

      myImage.setDensity(1); 

      imageview = (ImageView)findViewById(R.id.imageViewId); 
      imageview.setImageBitmap(getResizeBitmap(myImage,195, 480));//as per as your requirement 
0
private Bitmap resizeBitmap(Bitmap bitmap, float scaleHeight, float scaleWidth) 
{ 
    int width = bitmap.getWidth(); 
    int height = bitmap.getHeight(); 

    int newWidth = (int)(width * scaleWidth); 
    int newHeight = (int)(height * scaleHeight); 

    return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true); 
}