如何使用我自己的尺寸绘制位图?

时间:2014-07-08 07:31:30

标签: android bitmap

我想根据稍后设定的尺寸画一点我的。但是我唯一喜欢的方法是:

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

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

谢谢!

1 个答案:

答案 0 :(得分: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;
 }