Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Bitmap 用法實戰:讀取 SD 卡圖片與 Matrix 旋轉縮放

編輯:關於 Android 程式設計

專案裡的圖片放在 res/drawable 下,用 R.drawable.id 就能取用;但如果圖片存在記憶卡上呢?本文以「讀取外部圖片並顯示」與「動態旋轉圖片」兩個實例,把 Bitmap 的常用套路講清楚。

一、從檔案路徑載入圖片

核心 API 是 BitmapFactory.decodeFile(),它能把手機裡的圖片檔案轉成 Bitmap 物件。注意路徑要寫成 sdcard/picture/... 這種相對形式,開頭加上斜線反而是錯的。

 import java.io.File; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class A10Activity extends Activity { private Button b; private ImageView iv; private TextView tv; private String fileName="sdcard/picture/紅葉.jpg"; //private String fileName="sdcard/picture/紅葉.jpg";這種寫法是錯誤的,路徑不是以 //設備開頭 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); b=(Button)findViewById(R.id.button); b.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub iv=(ImageView)findViewById(R.id.imageview); tv=(TextView)findViewById(R.id.textview); File f=new File(fileName); //先判斷圖片文件是否存在 if(f.exists()){ //如果存在,通過Bitmap將圖片放入ImageView中顯示出來 /*BitmapFactory(Android.graphics.BitmapFactory)是Android API提供的對象,該對象 *的decodeFile()方法將手機中的圖片文件轉換成Bitmap對象。*/ Bitmap bm=BitmapFactory.decodeFile(fileName); iv.setImageBitmap(bm); tv.setText(fileName); } else{ tv.setText("文件不存在"); } } }); } } 

實務上強烈建議先以 File.exists() 判斷檔案是否存在,再進行解碼,否則空指標與 OOM 會一起找上門。
BitmapFactory 另外還提供 decodeResource()(把 res/drawable 內的資源轉成 Bitmap)與 decodeStream()(把 InputStream 轉成 Bitmap),三者搭配幾乎涵蓋所有來源。

二、用 Matrix 讓 ImageView 旋轉

思路是這樣:先把原圖包成 Bitmap,接著用 Matrix.setRotate() 描述旋轉角度,再交給 Bitmap.createBitmap() 產出一張新的旋轉後位圖,最後塞回 ImageView。範例另外示範了 postScale() 做縮放,並用按鈕累加角度、以 ±20 為上下限。

 import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class A11Activity extends Activity { private ImageView iv; private TextView tv; private Button left,right; private int times; private int angle; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); times=1; angle=1; iv=(ImageView)findViewById(R.id.iv); tv=(TextView)findViewById(R.id.tv); left=(Button)findViewById(R.id.left); left.setText("向左轉"); right=(Button)findViewById(R.id.right); right.setText("向右轉"); final Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.a); //自己引入一張圖片a.png final int width=bmp.getWidth(); final int height=bmp.getHeight(); iv.setImageBitmap(bmp); left.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub angle--; if(angle<-20){ //設置最多旋轉20度 angle=-20; } int width01=width*times; int height01=height*times; float width02=(float)(width01/width); float height02=(float)(width02/height); Matrix m=new Matrix(); m.postScale(width02, height02); m.setRotate(5*angle); Bitmap bmp01=Bitmap.createBitmap(bmp, 0, 0, width, height, m, true); BitmapDrawable bd=new BitmapDrawable(bmp01); iv.setImageDrawable(bd); tv.setText(Integer.toString(5*angle)); } }); right.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub angle++; if(angle>20){ angle=20; } int width01=width*times; int height01=height*times; float width02=(float)(width01/width); float height02=(float)(width02/height); Matrix m=new Matrix(); m.postScale(width02, height02); m.setRotate(5*angle); Bitmap bmp01=Bitmap.createBitmap(bmp, 0, 0, width, height, m, true); BitmapDrawable bd=new BitmapDrawable(bmp01); iv.setImageDrawable(bd); tv.setText(Integer.toString(5*angle)); } }); } } 

三、對應版面

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/left" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/right" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> 

提醒一句:每次旋轉都會產生一張新的 Bitmap,若用於高頻互動,記得妥善回收舊物件並考慮改用 View 層級的 rotation 屬性,以免記憶體吃緊。想深入圖形處理可參考本站《Android 開發入門與進階教程》與《Android 圖形與圖像處理技巧總結》。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved