- 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類對圖片進行縮放和旋轉操作。
推薦閱讀
- Designing Machine Learning Systems with Python
- Java 開發從入門到精通(第2版)
- 軟件架構設計:大型網站技術架構與業務架構融合之道
- 劍指JVM:虛擬機實踐與性能調優
- MATLAB 2020 從入門到精通
- Web Application Development with R Using Shiny(Second Edition)
- Building Mobile Applications Using Kendo UI Mobile and ASP.NET Web API
- The DevOps 2.4 Toolkit
- 名師講壇:Java微服務架構實戰(SpringBoot+SpringCloud+Docker+RabbitMQ)
- Hands-On Reinforcement Learning with Python
- Android開發:從0到1 (清華開發者書庫)
- 微信小程序全棧開發技術與實戰(微課版)
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- Image Processing with ImageJ
- Training Systems Using Python Statistical Modeling