Android Button与Matrix的使用

点击Button使图片旋转一定的角度,效果图如下:

图片旋转效果图

代码项目:

package zhang.bp;

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;
import android.widget.TextView;

public class bitmap_Matrix 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.button1);
		mButton2 = (Button) findViewById(R.id.button2);
		mImageView1 = (ImageView) findViewById(R.id.imageView1);
		mTextView1 = (TextView) findViewById(R.id.textView1);
		ScaleTimes = 1;
		ScaleAngle = 1;

		final Bitmap mySourBmp = BitmapFactory.decodeResource(getResources(),
				R.drawable.icon);

		final int widthOrig = mySourBmp.getWidth();
		final int heightOrig = mySourBmp.getHeight();

		mImageView1.setImageBitmap(mySourBmp);

		mButton1.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ScaleAngle--;
				if (ScaleAngle < -5)="" {="" scaleangle="-5;" }="" int="" newwidth="widthOrig" *="" scaletimes;="" int="" newheight="heightOrig" *="" scaletimes;="" float="" scalewidth="((float)" newwidth)="" widthorig;="" float="" scaleheight="((float)" newheight)="" heightorig;="" matrix="" matrix="new" matrix();="" matrix.postscale(scalewidth,="" scaleheight);="" matrix.setrotate(5="" *="" scaleangle);="" bitmap="" resizedbitmap="Bitmap.createBitmap(mySourBmp," 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="" arg0)="" {="" todo="" auto-generated="" method="" stub="" scaleangle++;="" if="" (scaleangle="">< 5)="" {="" scaleangle="5;" }="" int="" newwidth="widthOrig" *="" scaletimes;="" int="" newheight="heightOrig" *="" scaletimes;="" float="" scalewidth="((float)" newwidth)="" widthorig;="" float="" scaleheight="((float)" newheight)="" heightorig;="" matrix="" matrix="new" matrix();="" matrix.postscale(scalewidth,="" scaleheight);="" matrix.setrotate(5="" *="" scaleangle);="" bitmap="" resizedbitmap="Bitmap.createBitmap(mySourBmp," 0,="" 0,="" widthorig,="" heightorig,="" matrix,="" true);="" bitmapdrawable="" mynewbitmapdrawable="new" bitmapdrawable(="" resizedbitmap);="" mimageview1.setimagedrawable(mynewbitmapdrawable);="" mtextview1.settext(integer.tostring(5="" *="" scaleangle));="" }="" });="" }="" }="">

源码下载:Bitmap_Matrix.rar

暂无评论
  • 1:请一针见血的评论。
  • 2:评论需要审核通过后才能显示。
  • 3:评论字数限制在1000字以内。
  • 当前字数:0
热门文章
推荐文章
  • 暂无推荐文章
随机文章
关于本站 - 广告服务 - 版权声明 - 联系我们 - 友情链接 - 网站地图 - 帮助中心