Android 之 圖片變換

來源:互聯網
上載者:User

Android 之 圖片變換

說到圖片,第一反映就是bitmap,那就先來認識一下bitmap

Bitmap是Android系統中的影像處理的最重要類之一。用它可以擷取影像檔資訊,進行映像剪下、旋轉、縮放等操作,並可以指定格式儲存影像檔

Bitmap實現在android.graphics包中。但是Bitmap類的建構函式是私人的,外面並不能執行個體化,只能是通過JNI執行個體化。這必然是 某個輔助類提供了建立Bitmap的介面,而這個類的實現通過JNI介面來執行個體化Bitmap的,這個類就是BitmapFactory

vc+zo9PDtcS8uLj2o6zG5NPgtcS/tNK7z8LUtMLrtcTXor3ivs3WqrXAPC9wPg0KZGVjb2RlUmVzb3VyY2UgZGVjb2RlRmlsZSBkZWNvZGVTdHJlYW0NCjxwPr3iwuvXytS0o6y94sLrzsS8/qOsveLC68H3o6y4+b7dy/y1xMP719a+zdaqtcDBy8v50qq809TYtcTXytS0o6zA/cjnc2S/qLXEzsS8/r3iwuu/ycrHyrnTw2RlY29kZUZpbGW3vbeoo6zN+MLnyc+1xM28xqy/ydLUyrnTw2RlY29kZVN0cmVhbbe9t6ijrNfK1LTOxLz+1tC1xM28xqy/ydLUyrnTw2RlY29kZVJlc291cmNlt723qKOstbHIu9XiuPayu8rHucy2qM6o0ru1xKOs0vLOqseww+bBvbj2t723qLa8ysfNqLn9ttS12sj9uPa3vbeotcSw/NewyrXP1rXEPC9wPg0KPHA+zqrBy7fA1rnNvMasT09No6zL/Lu5zOG5qcHLT3B0aW9uc9XiuPayzsr9PC9wPg0KaW5KdXN0RGVjb2RlQm91bmRzIGluU2FtcGxlU2l6ZSBvdXRXaWR0aCBvdXRIZWlnaHQgaW5QcmVmZXJyZWRDb25maWcgb3V0TWltZVR5cGUNCjxwPiZoZWxsaXA7PC9wPg0KPHA+yOe5+2luSnVzdERlY29kZUJvdW5kc8no1sPOqnRydWWjrNTK0O2y6dGvzrvNvKOstavKx7K7t9bF5MTatOa3tbvY1rXOqm51bGyjrLWryse/ydLUtsHIoc28xqy1xLPftOe6zcDg0M3Qxc+ioaM8YnIgLz4NCmluU2FtcGxlU2l6ZbXE1rXU2r3izvbNvMaszqpCaXRtYXDKsdTas6S/7cG9uPa3vc/yyc/P8cvYy/XQobXEsbbK/aOsxKzIz9a1us3X7tCh1rXOqjGjqLWx0KHT2jHKsaOssLTV1TG0psDto6mjrMfS1Nq089PaMcqxo6y4w9a11rvE3M6qMrXEw92jqLWxsrvOqjK1xMPdyrGjrL3iwuvG97vhyKHT67jD1rXX7r3Tvfy1xDK1xMPdo6k8YnIgLz4NCmluUHJlZmVycmVkQ29uZmlnxKzIz86qQVJHQl84ODg4o6y1sci70rK/ydLUuLPG5Mv71rWjrMD9yOejukFMUEhBXzgsIFJHQl81NjUsIEFSR0JfNDQ0NKOsy8TW1s/xy9jA4NDNo6zDv7j2z/HL2NW808PL+dW8tcTX1r3ayv2yu82sPC9wPg0KPHA+z8LD5sC0yrXP1s28xqy1xLHku7s8L3A+DQo8cD7L9dCho7o8L3A+DQo8cHJlIGNsYXNzPQ=="brush:java;">public void scalePic(int reqWidth,int reqHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.mipmap.demo, options); options.inSampleSize = PhotoUtil.calculateInSampleSize(options, reqWidth,reqHeight); options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.demo, options); postInvalidate(); }

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) {            final int heightRatio = Math.round((float) height / (float) reqHeight);            final int widthRatio = Math.round((float) width / (float) reqWidth);            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;        }        return inSampleSize;    }

主要使用通過屬性inSampleSize實現圖片縮小,當然這個方法不是為了實現縮放的,我們這裡只是說明一下,它的主要目的還是預防OOM,當載入一張圖片時,當對於圖片的大小不確定時,要做一下限制,以防出現OOM,下面給出真正放大,縮小的代碼

public void scalePicByMatrix(float scaleWidth, float scaleHeight){        Matrix matrix = new Matrix();        matrix.setScale(scaleWidth,scaleHeight);        bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,false);        postInvalidate();    }

旋轉:

public void rotatePic(int angle) {        Matrix matrix = new Matrix();        matrix.setRotate(angle);        bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,false);        postInvalidate();    }

剪裁:

public void cutPic(int reqWidth, int reqHeight) {        if (bitmap.getWidth() > reqWidth && bitmap.getHeight() > reqHeight) {            bitmap = Bitmap.createBitmap(bitmap, 0, 0, reqWidth, reqHeight);        }else {            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight());        }        postInvalidate();    }

圖片儲存:

public void savePic(String path) {        File file = new File(path);        FileOutputStream fileOutputStream = null;        try {            file.createNewFile();            fileOutputStream = new FileOutputStream(file);            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);            fileOutputStream.flush();        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                if (fileOutputStream != null) {                    fileOutputStream.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }    }

以上給出的只是其中的一種方法,具體操作看應用情境

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.