/res/layout/main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ff929854">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button
android:id="@+id/rotateLeft"
android:text="左旋"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/rotateRight"
android:text="右旋"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/scaleBig"
android:text="放大"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/scaleSmall"
android:text="缩小"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/p2"
android:layout_gravity="center"/>
</LinearLayout>
Java代码如下:
package com.demo.android.matrix;
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.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MatrixActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
addFunctions();
}
private void addFunctions() {
//左旋
rotateLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//逆时针旋转90度
myMatrix.postRotate(-90);
//创建新的图片,重新绘图
Bitmap bitmap=Bitmap.createBitmap(myBitmap,0,0,width,height,myMatrix,true);
//将Bitmap对象转换为Drawable,使其可以用在ImageView和ImageButton中
BitmapDrawable newbmp=new BitmapDrawable(bitmap);
pic.setImageDrawable(newbmp);
}
});
//右旋
rotateRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myMatrix.postRotate(90);
Bitmap bitmap=Bitmap.createBitmap(myBitmap, 0, 0, width, height, myMatrix, true);
BitmapDrawable newDrawable=new BitmapDrawable(bitmap);
pic.setImageDrawable(newDrawable);
}
});
//放大
scaleBig.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myMatrix.postScale(1.2f, 1.2f);
Bitmap newBitmap=Bitmap.createBitmap(myBitmap, 0, 0, width, height,myMatrix, true);
BitmapDrawable newDrawable=new BitmapDrawable(newBitmap);
pic.setImageDrawable(newDrawable);
}
});
//缩小
scaleSmall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myMatrix.postScale(0.8f, 0.8f);
Bitmap newBitmap=Bitmap.createBitmap(myBitmap, 0, 0, width, height,myMatrix, true);
BitmapDrawable newDrawable=new BitmapDrawable(newBitmap);
pic.setImageDrawable(newDrawable);
}
});
}
private Bitmap myBitmap;
private Matrix myMatrix=new Matrix();
private int width;
private int height;
private Button rotateLeft;
private Button rotateRight;
private Button scaleBig;
private Button scaleSmall;
private ImageView pic;
private void findViews() {
rotateLeft=(Button) findViewById(R.id.rotateLeft);
rotateRight=(Button) findViewById(R.id.rotateRight);
scaleBig=(Button) findViewById(R.id.scaleBig);
scaleSmall=(Button) findViewById(R.id.scaleSmall);
pic=(ImageView) findViewById(R.id.pic);
//获取资源文件的Bitmap
//getResources()获取系统资源文件下的资源
myBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.p2);
width=myBitmap.getWidth();
height=myBitmap.getHeight();
}
}