在Android中,图像旋转和图像缩放类似,同样是通过Matrix来实现的。通过Matrix的setRotate()方法可以实现0~360度的旋转,下面是图像旋转的一个例子:
代码:图像旋转的实现
package com.miaozl.test;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;
public class RotateActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView dest = (ImageView)findViewById(R.id.dest);
Matrix matrix = new Matrix();
matrix.setRotate(60);
Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rabbit);
Bitmap resizedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),srcBitmap.getHeight(), matrix, true);
dest.setImageBitmap(resizedBitmap);
}
}
下面是相应的布局文件:
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/animlayout">
< ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/rabbit"
android:paddingTop="3dip"
android:paddingBottom="3dip"/>
< ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/dest"/>
< /LinearLayout>
下图是原图和经旋转后的图像的对比效果。

图像旋转的效果图