2014-07-08 28 views
0

我想根据稍后将要设置的尺寸绘制一些图片。但是familliar,唯一的方法IM是下列之一:如何用我自己的尺寸绘制位图?

canvas.drawBitmap(test, canvas.getWidth()/2 - test.getWidth()/2, canvas.getHeight()/2 - test.getHeight()/2, null); 

只绘制位图accordign到图像尺寸,所以我的问题是,是否有画有不同的尺寸或只是一种方法的位图的另一种方法改变它?

谢谢!

+0

[使用这一个: - http://stackoverflow.com/questions/23346412/draw-bitmap-on-canvas-with-original -dimension –

+0

如果你阅读文档,你可以看到canvas.drawBitmap的7个方法:http://developer.android.com/reference/android/graphics/Canvas.html –

+0

是的,我看到了其他方法,但哪一个做我使用? – Pachu

回答

0

使用下面的代码来调整位图与您选择的尺寸:

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, 
      int reqHeight) { 

    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    options.inSampleSize = calculateInSampleSize(options, reqWidth, 
      reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    Bitmap bmp = BitmapFactory.decodeFile(path, options); 
    return bmp; 
} 

public static int calculateInSampleSize(BitmapFactory.Options options, 
     int reqWidth, int reqHeight) { 

    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     if (width > height) { 
       inSampleSize = Math.round((float) height/(float) reqHeight); 
     } else { 
       inSampleSize = Math.round((float) width/(float) reqWidth); 
     } 
    } 
return inSampleSize; 
} 
相关问题