編輯:關於 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),三者搭配幾乎涵蓋所有來源。
思路是這樣:先把原圖包成 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 圖形與圖像處理技巧總結》。
手把手帶你打造炫酷的Loading成功動畫自定義View
寫在前面: 這可能是手把手自定義View系列的最後一篇了。實際上我也是一週前才真正開始接觸自定義View,經過這幾天的練習,基本上已經能應付一般的View需求,所以就以本
Android ImageView 圓角與圓形圖片實作:Xfermode 與自訂 View
在 Android 介面設計中,頭像、卡片圖示常需要圓形或圓角效果。常見做法有兩種:利用 Xfermode 進行圖層合成,或透過 BitmapShader 搭配自訂 Vi
Android 狀態欄透明化與著色:三種實作方案與場景對照
專案上遇到兩個相關需求:狀態欄要透明,且當使用者拖動、頂部內容滾動時,標題欄顏色要能跟著變化。這類效果常被稱為「沉浸式狀態欄」(這個叫法網路上有不少討論,這裡統一稱之為狀
Android 自訂 Dialog:改主題、換版面、封裝成類別
自訂對話框是開發中很常出現的需求。這裡示範的做法用到了 Android 的 shape 圖形資源,Android 的 drawable 種類相當豐富,後續再另篇整理。本篇