使用Bitmap createBitmap遇到的問題,bitmapcreatebitmap

來源:互聯網
上載者:User

使用Bitmap createBitmap遇到的問題,bitmapcreatebitmap

public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
            Matrix m, boolean filter)

在使用這個方法進行圖片縮放裁剪時,x,y的意思理解錯誤,導致結果不是我想要的效果。

這裡的x,y的值在原始碼中有說明,但是我沒有注意,應該是擷取源bitmap中的座標。

還有x+width要小於或等於source.getWidth(),y+height要小於或等於source.getHeight()


    /**     * 按寬/高縮放圖片到指定大小並進行裁剪得到中間部分圖片     *     * @param bitmap 源bitmap     * @param w 縮放後指定的寬度     * @param h 縮放後指定的高度     * @return 縮放後的中間部分圖片     */    public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {        int width = bitmap.getWidth();        int height = bitmap.getHeight();        float scaleWidht, scaleHeight, x, y;        Bitmap newbmp;        Matrix matrix = new Matrix();        if (width > height) {            scaleWidht = ((float) h / height);            scaleHeight = ((float) h / height);            x = (width - w * height / h) / 2;//擷取bitmap源檔案中x做表需要位移的像數大小            y = 0;        } else if (width < height) {            scaleWidht = ((float) w / width);            scaleHeight = ((float) w / width);            x = 0;            y = (height - h * width / w) / 2;//擷取bitmap源檔案中y做表需要位移的像數大小        } else {            scaleWidht = ((float) w / width);            scaleHeight = ((float) w / width);            x = 0;            y = 0;        }        matrix.postScale(scaleWidht, scaleHeight);        try {            newbmp = Bitmap.createBitmap(bitmap, (int) x, (int) y, (int) (width - x), (int) (height - y), matrix,                    true);//createBitmap()方法中定義的參數x+width要小於或等於bitmap.getWidth(),y+height要小於或等於bitmap.getHeight()        } catch (Exception e) {            e.printStackTrace();            return null;        }        return newbmp;    }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.