0

我正在尝试实现“扩展”按钮以使图像适合所选边界。

public void extendS(View v){
        ImageView iv = current;
        double width = gallery.getWidth();
        double hight= gallery.getHeight();

        double aspect = (width+0)/(hight+0);
        Log.d("aspect", "w: "+width+" h: "+hight+" a: "+aspect);
        if (aspect>1){
            hight/=aspect;
        }else{
            width*=aspect;
        }
        Bitmap b = ((BitmapDrawable) iv.getDrawable()).getBitmap();
//      Matrix matrix = new Matrix();
        Log.d("aspect", "w: "+width+" h: "+hight+" a: "+aspect);
         Bitmap scale = Bitmap.createBitmap(b, 0, 0, (int)width, (int)hight);
         iv.setImageBitmap(scale);

我收到的错误:07-16 12:26:36.855: E/AndroidRuntime(12647): Caused by: java.lang.IllegalArgumentException: y + height must be <= bitmap.height() 这个错误在我看来有点奇怪观点看法

4

2 回答 2

1
public static Bitmap resizeBitmap(Bitmap photo, float x, float y) {

    try {
        // get current bitmap width and height
        int width = photo.getWidth();
        int height = photo.getHeight();

        // determine how much to scale
        float scaleWidth = x / width;
        float scaleHeight = y / height;

        // create the matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bitmap
        matrix.postScale(scaleWidth, scaleHeight);

        // recreate the new bitmap
        Bitmap resizebitmap = Bitmap.createBitmap(photo, 0, 0, width,
                height, matrix, false);
        return resizebitmap;

    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
    }
    return null;
}
于 2013-07-16T09:31:22.713 回答
1

您收到此错误是因为您尝试访问原始位图区域之外的数据。您实际上非常接近,您只是使用了错误的 Bitmap.createBitmap() 函数。试试这个从原始源创建一个不同大小的位图:

Bitmap.createScaledBitmap(b, (int)width, (int)hight, true);

另外,你做错了纵横比。提示:您无需担心此代码的纵横比。

于 2013-07-16T09:37:43.437 回答