在Android中,圖像旋轉(zhuǎn)和圖像縮放類似,同樣是通過(guò)Matrix來(lái)實(shí)現(xiàn)的。通過(guò)Matrix的setRotate()方法可以實(shí)現(xiàn)0~360度的旋轉(zhuǎn),下面是圖像旋轉(zhuǎn)的一個(gè)例子:
代碼:圖像旋轉(zhuǎn)的實(shí)現(xiàn)
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);
}
}
下面是相應(yīng)的布局文件:
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://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>
下圖是原圖和經(jīng)旋轉(zhuǎn)后的圖像的對(duì)比效果。

圖像旋轉(zhuǎn)的效果圖