安卓学习 之 bitmap用法
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                安卓学习 之 bitmap用法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                1. 獲取資源
從資源文件得到圖片
Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.haha);讀取sd卡(通過文件)
String SDCarePath=Environment.getExternalStorageDirectory().toString(); String filePath=SDCarePath+"/"+"haha.jpg"; Bitmap rawBitmap1 = BitmapFactory.decodeFile(filePath, null);讀取sd卡(通過輸入流)
InputStream inputStream=getBitmapInputStreamFromSDCard("haha.jpg"); Bitmap rawBitmap2 = BitmapFactory.decodeStream(inputStream); //讀取SD卡下的圖片 private InputStream getBitmapInputStreamFromSDCard(String fileName){if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {String SDCarePath=Environment.getExternalStorageDirectory().toString();String filePath=SDCarePath+File.separator+fileName;File file=new File(filePath);try {FileInputStream fileInputStream=new FileInputStream(file);return fileInputStream;} catch (Exception e) {e.printStackTrace();}}return null;}2. 設(shè)置圓角
public Bitmap toRoundCorner(Bitmap bitmap, int pixels) {Bitmap roundCornerBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);Canvas canvas = new Canvas(roundCornerBitmap);int color = 0xff424242;//int color = 0xff424242;Paint paint = new Paint();paint.setColor(color);//防止鋸齒paint.setAntiAlias(true);Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());RectF rectF = new RectF(rect);float roundPx = pixels;//相當(dāng)于清屏canvas.drawARGB(0, 0, 0, 0);//先畫了一個帶圓角的矩形canvas.drawRoundRect(rectF, roundPx, roundPx, paint);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//再把原來的bitmap畫到現(xiàn)在的bitmap!!!注意這個理解canvas.drawBitmap(bitmap, rect, rectF, paint);return roundCornerBitmap;} } /** bitmap 繪制的bitmap對象* src Bitmap對象的矩形區(qū)域* dst bitmap繪制在屏幕的什么地方* paint Paint對象*/ public void drawBitmap(Bitmap bitmap, Rect src, RectF dst,Paint paint)3. 壓縮圖片(通過矩陣)
// 得到圖片原始的高寬 int rawHeight = rawBitmap.getHeight(); int rawWidth = rawBitmap.getWidth(); // 設(shè)定圖片新的高寬 int newHeight = 500; int newWidth = 500; // 計算縮放因子 float heightScale = ((float) newHeight) / rawHeight; float widthScale = ((float) newWidth) / rawWidth; // 新建立矩陣 Matrix matrix = new Matrix(); matrix.postScale(heightScale, widthScale); // 設(shè)置圖片的旋轉(zhuǎn)角度 //matrix.postRotate(-30); // 設(shè)置圖片的傾斜 //matrix.postSkew(0.1f, 0.1f); //將圖片大小壓縮,壓縮后圖片的寬和高以及kb大小均會變化 Bitmap newBitmap = Bitmap.createBitmap(rawBitmap, 0, 0, rawWidth,rawWidth, matrix, true); // 將Bitmap轉(zhuǎn)換為Drawable Drawable newBitmapDrawable = new BitmapDrawable(newBitmap); imageView.setImageDrawable(newBitmapDrawable);傳入原來的bitmap,調(diào)用compressAndSaveBitmapToSDCard使得寬高不變,大小縮小,80代表壓縮的質(zhì)量
this.compressAndSaveBitmapToSDCard(copyRawBitmap1(原bitmap),"0011fa.jpg",80); //壓縮且保存圖片到SDCardprivate void compressAndSaveBitmapToSDCard(Bitmap rawBitmap, String fileName, int quality) {String saveFilePaht = this.getSDCardPath() + File.separator + fileName;File saveFile = new File(saveFilePaht);if (!saveFile.exists()) {try {saveFile.createNewFile();FileOutputStream fileOutputStream = new FileOutputStream(saveFile);if (fileOutputStream != null) {//imageBitmap.compress(format, quality, stream);//把位圖的壓縮信息寫入到一個指定的輸出流中//第一個參數(shù)format為壓縮的格式//第二個參數(shù)quality為圖像壓縮比的值,0-100.0 意味著小尺寸壓縮,100意味著高質(zhì)量壓縮//第三個參數(shù)stream為輸出流rawBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);}fileOutputStream.flush();fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}4. 獲取縮略圖
String SDCarePath2=Environment.getExternalStorageDirectory().toString(); String filePath2=SDCarePath2+"/"+"haha.jpg"; Bitmap tempBitmap=BitmapFactory.decodeFile(filePath2); Bitmap bitmapThumbnail2=ThumbnailUtils.extractThumbnail(tempBitmap, 100, 100); imageView.setImageBitmap(bitmapThumbnail2);其中ThumbnailUtils 是安卓提供的工具類
總結(jié)
以上是生活随笔為你收集整理的安卓学习 之 bitmap用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: kotlin学习目录
 - 下一篇: Kotlin协程简介(一)