android中Matrix之一(图片二维转动) android最酷的就是它的图片切换效果,而其核心是通过Matrix类来实现的,作了个简单的小例子,可以通过点击两个按钮实现图片的左右转动,奉献以下程序代码;页面布局matrixlayout.xml代码:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content" android:layout_height="wrap_content"><TextView android:id="@+id/myTextView1" android:layout_width="fill_parent"android:layout_height="fill_parent" android:text="hello xiaohua"></TextView><Button android:id="@+id/myButton1" android:text="left"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><Button android:id="@+id/myButton2" android:text="right"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><ImageView android:id="@+id/myImageView1"android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView></LinearLayout>主程序代码MatrixTest.javapackage test.shi;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.widget.*;import android.util.Log;import android.view.*;public class MatrixTest extends Activity {private Button myButton1;private Button myButton2;private TextView myTextView1;private ImageView myImageView1;int ScaleTimes=0;int ScaleAngles=0;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.matrixlayout);try{//初始化几个控件myButton1=(Button)findViewById(R.id.myButton1);myButton2=(Button)findViewById(R.id.myButton2);myTextView1=(TextView)findViewById(R.id.myTextView1);myImageView1=(ImageView)findViewById(R.id.myImageView1);ScaleTimes=1;ScaleAngles=1; //生成位图final Bitmap mybitmap=BitmapFactory.decodeResource(getResources(), R.drawable.icon);myImageView1.setImageBitmap(mybitmap);//图片原始大小final int widOrg=mybitmap.getWidth();final int heightOrg=mybitmap.getHeight();myButton1.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubScaleAngles--;if(ScaleAngles<=-5)ScaleAngles=-5;int newWid=ScaleTimes*widOrg;int newHeight=ScaleTimes*heightOrg;float scaleWid=((float)newWid)/ScaleTimes;float scaleHeight=((float)newHeight)/ScaleTimes;//每次都转5度,设定的最大左偏转25度,右偏转25度Matrix matrix=new Matrix();matrix.postScale(scaleWid,scaleHeight);matrix.setRotate(5*ScaleAngles); //产生暂存的Bitmap,matrix参数是关键Bitmap newbitmap=Bitmap.createBitmap(mybitmap, 0,0, widOrg, heightOrg,matrix,true);BitmapDrawable bitmapDrawable=new BitmapDrawable(newbitmap);myImageView1.setImageDrawable(bitmapDrawable);myTextView1.setText(Integer.toString(5*ScaleAngles));}});myButton2.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubScaleAngles++;if(ScaleAngles>=5)ScaleAngles=5;int newWid=ScaleTimes*widOrg;int newHeight=ScaleTimes*heightOrg;float scaleWid=((float)newWid)/ScaleTimes;float scaleHeight=((float)newHeight)/ScaleTimes;Matrix matrix=new Matrix();matrix.postScale(scaleWid,scaleHeight);//postRotate与setRotate在这里效果是一样的。//matrix.postRotate(5*ScaleAngles);matrix.setRotate(5*ScaleAngles);Bitmap newbitmap=Bitmap.createBitmap(mybitmap, 0,0, widOrg, heightOrg,matrix,true);BitmapDrawable bitmapDrawable=new BitmapDrawable(newbitmap);myImageView1.setImageDrawable(bitmapDrawable);myTextView1.setText(Integer.toString(5*ScaleAngles));}});}catch(Exception ex){Log.d("shi",ex.toString());ex.printStackTrace();}}}