- Android游戲開發技術實戰詳解
- 褚尚軍 張加春編著
- 1015字
- 2018-12-30 05:33:21
4.7 使用圖形變換類Matrix
類Matrix的完整形式是Android.Graphics.Matrix,此類的功能是實現圖形的變換操作,如常見的縮放和旋轉處理。類Matrix有很多“成名絕技”,其中最著名的是如下幾種常用的方法。
(1)void reset():重置一個Matrix對象;
(2)void set(Matrix src):復制一個源矩陣,和本類的構造方法Matrix(Matrix src)一樣;
(3)boolean isIdentity():返回這個矩陣是否定義(已經有意義);
(4)void setRotate(float degrees):指定一個角度,以(0,0)為坐標進行旋轉;
(5)void setRotate(float degrees, float px, float py):指定一個角度,以(px,py)為坐標進行旋轉;
(6)void setScale(float sx, float sy):縮放處理;
(7)void setScale(float sx, float sy, float px, float py):以坐標(px, py)進行縮放;
(8)void setTranslate(float dx, float dy):平移;
(9)void setSkew (float kx, float ky, float px, float py:以坐標(px, py)進行傾斜;
(10)void setSkew (float kx, float ky):傾斜處理。
實例4-5 使用Matrix類實現圖片縮放功能(daima\4\MatrixCH)。
本實例的實現流程如下所示。
step 1 編寫布局文件main.xml,主要代碼如下。
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <ImageView android:id="@+id/myImageView" android:layout_width="200px" android:layout_height="150px" android:src="@drawable/suofang" android:layout_x="0px" android:layout_y="0px" > </ImageView> <Button android:id="@+id/myButton1" android:layout_width="90px" android:layout_height="60px" android:text="@string/str_button1" android:textSize="18sp" android:layout_x="20px" android:layout_y="372px" > </Button> <Button android:id="@+id/myButton2" android:layout_width="90px" android:layout_height="60px" android:text="@string/str_button2" android:textSize="18sp" android:layout_x="210px" android:layout_y="372px" > </Button> </AbsoluteLayout>
step 2 編寫主程序文件MatrixCH.java,實現圖片縮放,縮小按鈕響應mButton01.setOnClick-Listener,放大按鈕響應mButton02.setOnClickListener。文件MatrixCH.java的主要實現代碼如下。
public class MatrixCH extends Activity { /* 相關變量聲明 */ private ImageView mImageView; private Button mButton01; private Button mButton02; private AbsoluteLayout layout1; private Bitmap bmp; private int id=0; private int displayWidth; private int displayHeight; private float scaleWidth=1; private float scaleHeight=1; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 載入main.xml Layout */ setContentView(R.layout.main); /* 取得屏幕分辨率大小 */ DisplayMetrics dm=new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); displayWidth=dm.widthPixels; /* 屏幕高度需扣除下方Button高度 */ displayHeight=dm.heightPixels-80; /* 初始化相關變量 */ bmp=BitmapFactory.decodeResource(getResources(), R.drawable.suofang); mImageView = (ImageView)findViewById(R.id.myImageView); layout1 = (AbsoluteLayout)findViewById(R.id.layout1); mButton01 = (Button)findViewById(R.id.myButton1); mButton02 = (Button)findViewById(R.id.myButton2); /* 縮小按鈕onClickListener */ mButton01.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { small(); } }); /* 放大按鈕onClickListener */ mButton02.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { big(); } }); } /* 圖片縮小的method */ private void small() { int bmpWidth=bmp.getWidth(); int bmpHeight=bmp.getHeight(); /* 設置圖片縮小的比例 */ double scale=0.8; /* 計算出這次要縮小的比例 */ scaleWidth=(float) (scaleWidth*scale); scaleHeight=(float) (scaleHeight*scale); /* 產生reSize后的Bitmap對象 */ Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth, bmpHeight,matrix,true); if(id==0) { /* 如果是第一次按,就刪除原來默認的ImageView */ layout1.removeView(mImageView); } else { /* 如果不是第一次按,就刪除上次放大縮小所產生的ImageView */ layout1.removeView((ImageView)findViewById(id)); } /* 產生新的ImageView,放入reSize的Bitmap對象,再放入Layout中 */ id++; ImageView imageView = new ImageView(suofang.this); imageView.setId(id); imageView.setImageBitmap(resizeBmp); layout1.addView(imageView); setContentView(layout1); /* 因為圖片放到最大時放大按鈕會消失,所以在縮小時把它重設為enable */ mButton02.setEnabled(true); } /* 圖片放大的method */ private void big() { int bmpWidth=bmp.getWidth(); int bmpHeight=bmp.getHeight(); /* 設置圖片放大的比例 */ double scale=1.25; /* 計算這次要放大的比例 */ scaleWidth=(float)(scaleWidth*scale); scaleHeight=(float)(scaleHeight*scale); /* 產生reSize后的Bitmap對象 */ Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth, bmpHeight,matrix,true); if(id==0) { /* 如果是第一次按,就刪除原來設置的ImageView */ layout1.removeView(mImageView); } else { /* 如果不是第一次按,就刪除上次放大縮小所產生的ImageView */ layout1.removeView((ImageView)findViewById(id)); } /* 產生新的ImageView,放入reSize的Bitmap對象,再放入Layout中 */ id++; ImageView imageView = new ImageView(suofang.this); imageView.setId(id); imageView.setImageBitmap(resizeBmp); layout1.addView(imageView); setContentView(layout1); /* 如果再放大會超過屏幕大小,就讓Button消失*/ if(scaleWidth*scale*bmpWidth>displayWidth|| scaleHeight*scale*bmpHeight>displayHeight) { mButton02.setEnabled(false); } }
執行后將顯示一幅圖片和兩個按鈕,如圖4-5所示,分別單擊“縮小”和“放大”按鈕后會實現對圖片的縮小、放大處理。

圖4-5 執行效果
- Modern JavaScript Web Development Cookbook
- Proxmox High Availability
- WordPress 5 Complete
- 智慧光網絡:關鍵技術、應用實踐和未來演進
- CCNP TSHOOT(642-832)認證考試指南
- 6G新技術 新網絡 新通信
- 計算機網絡技術及應用
- 網管第一課:網絡操作系統與配置管理
- 深入理解OpenStack Neutron
- 園區網絡架構與技術
- 網絡互聯技術(理論篇)
- ElasticSearch Server
- 網絡空間作戰:機理與籌劃
- Learn Node.js by Building 6 Projects.
- 智能家庭網絡:技術、標準與應用實踐