Matrix功能很是强大,利用这个类提供的一系列方法,我们可以实现图片的旋转。
下面以一个例子说明实现方法。
首先,我们看下实现的截图:
下面给出具体的实现代码:
1.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/white" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#ff0000" android:text="@string/app_name"/> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/myButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_button1" /> <ImageView android:id="@+id/myImageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <Button android:id="@+id/myButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_button2" /> </LinearLayout> </LinearLayout>
2.主程序的实现
public class EX04_24 extends Activity { private Button mButton1; private Button mButton2; private TextView mTextView1; private ImageView mImageView1; private int ScaleTimes; private int ScaleAngle; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton1 =(Button) findViewById(R.id.myButton1); mButton2 =(Button) findViewById(R.id.myButton2); mTextView1 = (TextView) findViewById(R.id.myTextView1); mImageView1 = (ImageView) findViewById(R.id.myImageView1); //定义旋转的角度和图像转变的比例(大小) ScaleTimes = 1; ScaleAngle = 1; final Bitmap mySourceBmp = BitmapFactory.decodeResource(getResources(), R.drawable.hippo); final int widthOrig = mySourceBmp.getWidth(); final int heightOrig = mySourceBmp.getHeight(); /* 程序刚执行,加载默认的Drawable */ mImageView1.setImageBitmap(mySourceBmp); /* 向左选转按钮 */ mButton1.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ScaleAngle--; if(ScaleAngle<-5) { ScaleAngle = -5; } /* ScaleTimes=1,维持1:1的宽高比例*/ int newWidth = widthOrig * ScaleTimes; int newHeight = heightOrig * ScaleTimes; float scaleWidth = ((float) newWidth) / widthOrig; float scaleHeight = ((float) newHeight) / heightOrig; Matrix matrix = new Matrix(); /* 使用Matrix.postScale设定维度 */ matrix.postScale(scaleWidth, scaleHeight); /* 使用Matrix.postRotate方法旋转Bitmap*/ //matrix.postRotate(5*ScaleAngle); matrix.setRotate(5*ScaleAngle); /* 建立新的Bitmap对象 */ Bitmap resizedBitmap = Bitmap.createBitmap(mySourceBmp, 0, 0, widthOrig, heightOrig, matrix, true); /**/ BitmapDrawable myNewBitmapDrawable = new BitmapDrawable(resizedBitmap); mImageView1.setImageDrawable(myNewBitmapDrawable); mTextView1.setText(Integer.toString(5*ScaleAngle)); } }); /* 向右选转按钮 */ mButton2.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ScaleAngle++; if(ScaleAngle>5) { ScaleAngle = 5; } /* ScaleTimes=1,维持1:1的宽高比例*/ int newWidth = widthOrig * ScaleTimes; int newHeight = heightOrig * ScaleTimes; /* 计算旋转的Matrix比例 */ float scaleWidth = ((float) newWidth) / widthOrig; float scaleHeight = ((float) newHeight) / heightOrig; Matrix matrix = new Matrix(); /* 使用Matrix.postScale设定维度 */ matrix.postScale(scaleWidth, scaleHeight); /* 使用Matrix.postRotate方法旋转Bitmap*/ //matrix.postRotate(5*ScaleAngle); matrix.setRotate(5*ScaleAngle); /* 建立新的Bitmap对象 */ Bitmap resizedBitmap = Bitmap.createBitmap(mySourceBmp, 0, 0, widthOrig, heightOrig, matrix, true); /**/ BitmapDrawable myNewBitmapDrawable = new BitmapDrawable(resizedBitmap); mImageView1.setImageDrawable(myNewBitmapDrawable); mTextView1.setText(Integer.toString(5*ScaleAngle)); } }); } }
作者:DLUTBruceZhang 发表于2013-2-2 23:33:09 原文链接
阅读:112 评论:0 查看评论