- Android 5從入門到精通
- 李波
- 473字
- 2021-03-19 15:29:03
4.6 Bitmap
Bitmap稱為點陣圖像或繪制圖像,是由稱作像素(圖片元素)的單個點組成的,這些點通過不同的排列和染色以構成圖樣。Bitmap是Android系統中圖像處理最重要的類之一,用它可以獲取圖像文件信息,對圖像進行剪切、旋轉、縮放等操作,并可以將圖像保存成特定格式的文件。Bitmap位于android.graphics包中,Bitmap不提供對外的構造方法,只能通過BitmapFactory類進行實例化。利用BitmapFactory的decodeFile方法可以從特定文件中獲取Bitmap對象,也可以使用decodeResource()從特定的圖片資源中獲取Bitmap對象。
實例BitmapDemo從資源文件中創建Bitmap對象,并對其進行一些操作,運行效果如圖4.40所示。

圖4.40 Bitmap對象的效果
其對應布局文件Main.xml內容如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <SeekBar android:id="@+id/seekBarId" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/im01" /> </LinearLayout>
BitmapDemoActivity.Java代碼如下:
package introduction.android.bitmapDemo; import com.sie.bitmapdemo.R; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; public class BitmapDemoActivity extends Activity { ImageView myImageView; Bitmap myBmp, newBmp; int bmpWidth, bmpHeight; SeekBar seekbarRotate; float rotAngle; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myImageView=(ImageView)findViewById(R.id.imageview); // 由Resource載入圖片 myBmp=BitmapFactory.decodeResource(getResources(), R.drawable.im01); bmpWidth=myBmp.getWidth(); bmpHeight=myBmp.getHeight(); // 實例化matrix Matrix matrix=new Matrix(); //設定Matrix屬性 x,y縮放比例為1.5 matrix.postScale(1.5F, 1.5F); //順時針旋轉45度 matrix.postRotate(45.0F); newBmp=Bitmap.createBitmap(myBmp, 0, 0, bmpWidth, bmpHeight, matrix, true); seekbarRotate=(SeekBar)findViewById(R.id.seekBarId); seekbarRotate.setOnSeekBarChangeListener(onRotate); } private SeekBar.OnSeekBarChangeListener onRotate=new SeekBar.OnSeekBarChangeListener(){ public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub Matrix m=new Matrix(); m.postRotate((float)progress*3.6F); newBmp=Bitmap.createBitmap(myBmp, 0, 0, bmpWidth, bmpHeight, m, true); myImageView.setImageBitmap(newBmp); } }; }
本實例實現了拖動進度條圖片旋轉的效果。使用BitmapFactory從資源中載入圖片,并獲取圖片的寬和高,之后使用Matrix類對圖片進行縮放和旋轉操作。
推薦閱讀
- Implementing VMware Horizon 7(Second Edition)
- Flutter開發實戰詳解
- JavaScript語言精髓與編程實踐(第3版)
- Java Web基礎與實例教程(第2版·微課版)
- 信息可視化的藝術:信息可視化在英國
- The HTML and CSS Workshop
- Expert Data Visualization
- Getting Started with Gulp
- C#應用程序設計教程
- Canvas Cookbook
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- 區塊鏈項目開發指南
- 貫通Tomcat開發
- Apache Solr PHP Integration
- Clojure for Finance