首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android开发-Matrix(2)-实现图片的旋转

2013-02-18 
Android开发--Matrix(二)--实现图片的旋转Matrix功能很是强大,利用这个类提供的一系列方法,我们可以实现图

Android开发--Matrix(二)--实现图片的旋转

      Matrix功能很是强大,利用这个类提供的一系列方法,我们可以实现图片的旋转。

      下面以一个例子说明实现方法。

首先,我们看下实现的截图:

Android开发-Matrix(2)-实现图片的旋转Android开发-Matrix(2)-实现图片的旋转

下面给出具体的实现代码:

1.xml布局文件

public class EX04_24 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.myButton1);    mButton2 =(Button) findViewById(R.id.myButton2);     mTextView1 = (TextView) findViewById(R.id.myTextView1);     mImageView1 = (ImageView) findViewById(R.id.myImageView1);        //定义旋转的角度和图像转变的比例(大小)    ScaleTimes = 1;     ScaleAngle = 1;         final Bitmap mySourceBmp =       BitmapFactory.decodeResource(getResources(), R.drawable.hippo);         final int widthOrig = mySourceBmp.getWidth();     final int heightOrig = mySourceBmp.getHeight();         /* 程序刚执行,加载默认的Drawable */     mImageView1.setImageBitmap(mySourceBmp);        /* 向左选转按钮 */    mButton1.setOnClickListener(new Button.OnClickListener()    {             @Override       public void onClick(View v)       {      // TODO Auto-generated method stub      ScaleAngle--;      if(ScaleAngle<-5)      {       ScaleAngle = -5;        }           /* ScaleTimes=1,维持1:1的宽高比例*/      int newWidth = widthOrig * ScaleTimes;     int newHeight = heightOrig * ScaleTimes;           float scaleWidth = ((float) newWidth) / widthOrig;     float scaleHeight = ((float) newHeight) / heightOrig;           Matrix matrix = new Matrix();      /* 使用Matrix.postScale设定维度 */     matrix.postScale(scaleWidth, scaleHeight);     /* 使用Matrix.postRotate方法旋转Bitmap*/     //matrix.postRotate(5*ScaleAngle);      matrix.setRotate(5*ScaleAngle);      /* 建立新的Bitmap对象 */     Bitmap resizedBitmap =        Bitmap.createBitmap(mySourceBmp, 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 v)       {         // TODO Auto-generated method stub         ScaleAngle++;         if(ScaleAngle>5)         {           ScaleAngle = 5;           }         /* ScaleTimes=1,维持1:1的宽高比例*/         int newWidth = widthOrig * ScaleTimes;         int newHeight = heightOrig * ScaleTimes;         /* 计算旋转的Matrix比例 */         float scaleWidth = ((float) newWidth) / widthOrig;         float scaleHeight = ((float) newHeight) / heightOrig;         Matrix matrix = new Matrix();        /* 使用Matrix.postScale设定维度 */        matrix.postScale(scaleWidth, scaleHeight);         /* 使用Matrix.postRotate方法旋转Bitmap*/        //matrix.postRotate(5*ScaleAngle);        matrix.setRotate(5*ScaleAngle);        /* 建立新的Bitmap对象 */         Bitmap resizedBitmap =           Bitmap.createBitmap(mySourceBmp, 0, 0, widthOrig, heightOrig, matrix, true);        /**/ BitmapDrawable myNewBitmapDrawable =           new BitmapDrawable(resizedBitmap);         mImageView1.setImageDrawable(myNewBitmapDrawable);         mTextView1.setText(Integer.toString(5*ScaleAngle));         }       });     }   }


热点排行
Bad Request.