Android Matrix

3*3矩阵

public class MatrixExample extends Activity {
	private Bitmap myBitmap;// 声明Bitmap类型变量
	private Matrix myMatrix = new Matrix();// 声明并创建Matrix对象
	private int width;// 声明int类型变量
	private int height;// 声明int类型变量

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);// 加载布局文件

		Button rotateLeftBut = (Button) this.findViewById(R.id.rotateLeft);// 获取XML文件中的左旋Button
		Button rotateRightBut = (Button) this.findViewById(R.id.rotateRight);// 获取XML文件中的右旋Button
		Button scaleBigBut = (Button) this.findViewById(R.id.scaleBig);// 获取XML文件中的放大Button
		Button scaleSmallBut = (Button) this.findViewById(R.id.scaleSmall);// 获取XML文件中的缩小Button
		// 获取资源文件中的p2图片的Bitmap,getResources()方法用来获取应用程序下的系统资源
		myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.p2);
		// 获取图片的原始的大小
		width = myBitmap.getWidth();
		height = myBitmap.getHeight();

		rotateLeftBut.setOnClickListener(new OnClickListener() {// 左旋转Button的单击事件

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						myMatrix.postRotate(-90);// 逆时针旋转90度
						// 创建一个新的图片,重新绘图
						Bitmap newBitmap = Bitmap.createBitmap(myBitmap, 0, 0,
								width, height, myMatrix, true);

						// 创建Bitmap转换为Drawable对象,使其可以使用在ImageView和ImageButton中
						BitmapDrawable newbmp = new BitmapDrawable(newBitmap);

						// 创建ImageView的对象
						ImageView imageView = (ImageView) MatrixExample.this
								.findViewById(R.id.pic);
						// 设置ImageView的背景图片
						imageView.setImageDrawable(newbmp);
					}

				});
		// 右旋转
		rotateRightBut.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				myMatrix.postRotate(90);// 顺时针旋转90度
				// 创建一个新的图片,重新绘图
				Bitmap newBitmap = Bitmap.createBitmap(myBitmap, 0, 0, width,
						height, myMatrix, true);

				// 创建Bitmap转换为Drawable对象,使其可以使用在ImageView和ImageButton中
				BitmapDrawable newbmp = new BitmapDrawable(newBitmap);

				// 创建ImageView的对象
				ImageView imageView = (ImageView) MatrixExample.this
						.findViewById(R.id.pic);
				// 设置ImageView的背景图片
				imageView.setImageDrawable(newbmp);
			}

		});
		// 缩小
		scaleSmallBut.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				// 缩放图片的动作,宽高的缩放比例为0.8
				myMatrix.postScale(0.8f, 0.8f);

				// 创建一个新的图片,重新绘图
				Bitmap newBitmap = Bitmap.createBitmap(myBitmap, 0, 0, width,
						height, myMatrix, true);

				// 创建Bitmap转换为Drawable对象,使其可以使用在ImageView和ImageButton中
				BitmapDrawable newbmp = new BitmapDrawable(newBitmap);

				// 创建ImageView的对象
				ImageView imageView = (ImageView) MatrixExample.this
						.findViewById(R.id.pic);
				// 设置ImageView的背景图片
				imageView.setImageDrawable(newbmp);
			}

		});
		// 放大
		scaleBigBut.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 缩放图片的动作,宽高的缩放比例为0.8
				myMatrix.postScale(1.2f, 1.2f);
			
				// 创建一个新的图片,重新绘图
				Bitmap newBitmap = Bitmap.createBitmap(myBitmap, 0, 0, width,
						height, myMatrix, true);

				// 创建Bitmap转换为Drawable对象,使其可以使用在ImageView和ImageButton中
				BitmapDrawable newbmp = new BitmapDrawable(newBitmap);

				// 创建ImageView的对象
				ImageView imageView = (ImageView) MatrixExample.this
						.findViewById(R.id.pic);
				// 设置ImageView的背景图片
				imageView.setImageDrawable(newbmp);
			}

		});

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值