国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android中Bitmap用法(顯示,保存,縮放,旋轉)實例分析

2020-04-11 11:25:27
字體:
來源:轉載
供稿:網友

本文實例講述了android中Bitmap用法。分享給大家供大家參考。具體如下:

在Android SDK中可以支持的圖片格式如下:png , jpg , gif和bmp。

1.Bitmap的創建

借助于BitmapFactory。

1)資源中的圖片

使用BitmapFactory獲取位圖

復制代碼 代碼如下:
Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.testImg);

或者是:

Resources res=getResources();//使用BitmapDrawable獲取位圖//使用BitmapDrawable (InputStream is)構造一個BitmapDrawable;//使用BitmapDrawable類的getBitmap()獲取得到位圖;// 讀取InputStream并得到位圖InputStream is=res.openRawResource(R.drawable.testImg); BitmapDrawable bmpDraw=new BitmapDrawable(is);Bitmap bmp=bmpDraw.getBitmap();

2)SD卡中的圖片

復制代碼 代碼如下:
Bitmap bmp = BitmapFactory.decodeFile("/sdcard/testBitmap/testImg.png")

2. 把 Bitmap 保存在sdcard中

File fImage = new File("/sdcard/testBitmap/testImg.png");  fImage.createNewFile();FileOutputStream iStream = new FileOutputStream(fImage); bmp.compress(CompressFormat.PNG, 100, iStream); iStream.close();fImage.close();iStream =null;fImage =null;//寫到輸出流里,就保存到文件了。

3.使用網絡中的圖片

//圖片的鏈接地址  String imgURLStr = "http://tx.bdimg.com/sys/portrait/item/990e6271796a7a6c170c.jpg";  URL imgURL = new URL(imgURLStr);  URLConnection conn = imgURL.openConnection();  conn.connect();  InputStream is = conn.getInputStream();  BufferedInputStream bis = new BufferedInputStream(is);//下載圖片Bitmap bmp = BitmapFactory.decodeStream(bis);//關閉Streambis.close();  is.close(); imgURL =null;

4.顯示圖片

1)轉換為BitmapDrawable對象顯示位圖

// 轉換為BitmapDrawable對象BitmapDrawable bmpDraw=new BitmapDrawable(bmp);// 顯示位圖ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);iv2.setImageDrawable(bmpDraw);

2)使用Canvas類顯示位圖

復制代碼 代碼如下:
canvas.drawBitmap(bmp, 0, 0, null);

5.縮放位圖

1)將一個位圖按照需求重畫一遍,畫后的位圖就是我們需要的了,與位圖的顯示幾乎一樣:

復制代碼 代碼如下:
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)

2)在原有位圖的基礎上,縮放原位圖,創建一個新的位圖:

復制代碼 代碼如下:
CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

3)借助Canvas的scale(float sx, float sy) ,不過要注意此時整個畫布都縮放了。

4)借助Matrix:

Matrix matrix=new Matrix();matrix.postScale(0.2f, 0.2f);Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);canvas.drawBitmap(dstbmp, 10, 10, null); 

6.旋轉位圖

借助Matrix或者Canvas來實現。

Matrix matrix=new Matrix();matrix.postRotate(45);Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(), bmp.getHeight(),matrix,true);canvas.drawBitmap(dstbmp, 10, 10, null);

希望本文所述對大家的Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 靖安县| 四子王旗| 庆元县| 毕节市| 图片| 中西区| 岳普湖县| 长治市| 洪湖市| 类乌齐县| 许昌市| 虹口区| 陕西省| 南川市| 巧家县| 甘德县| 察雅县| 阳西县| 杭州市| 班玛县| 响水县| 福鼎市| 龙里县| 沙雅县| 呼图壁县| 湖口县| 南漳县| 蛟河市| 淮阳县| 洛扎县| 玉溪市| 扎囊县| 宜阳县| 平潭县| 金秀| 永康市| 西充县| 桂林市| 乃东县| 华亭县| 柘荣县|