欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Android实现图像旋转,android图像旋转,图像旋转的基本思想是通过

来源: javaer 分享于  点击 16126 次 点评:45

Android实现图像旋转,android图像旋转,图像旋转的基本思想是通过


图像旋转的基本思想是通过Matrix类的setRotate方法设置旋转的角度,然后使用Bitmap.createBitmap方法创建一个已经旋转了的图像。除此之外,还可以使用Canvas.setMatrix方法设置,并直接使用drawBitmap绘制。```java public class MainActivity extends Activity{

    public static int alpha=100;      private View myView;    @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(new MyView(this));      }    private class MyView extends View{          //十字扳手图像          private Bitmap bitmap1;          //小球图像          private Bitmap bitmap2;          //十字扳手当前角度          private int digree1 = 0;          //小球当前角度          private int digree2 = 360;        public MyView(Context context)          {              super(context);              setBackgroundColor(color.white);              InputStream is = getResources().openRawResource(R.drawable.cross);              bitmap1 = BitmapFactory.decodeStream(is);              is = getResources().openRawResource(R.drawable.ball);              bitmap2 = BitmapFactory.decodeStream(is);          }        @Override          protected void onDraw(Canvas canvas)          {              Matrix matrix = new Matrix();              //讲旋转角度控制在0-360              if (digree1 > 360)                  digree1 = 0;              if(digree2 < 0)                  digree2 = 360;              //设置旋转角度和旋转中心点              matrix.setRotate(digree1++, 160, 240);                                    canvas.setMatrix(matrix);              //绘制图像              canvas.drawBitmap(bitmap1, 88, 169, null);              //设置旋转角度和旋转中心点              matrix.setRotate(digree2--,160 , 240);              canvas.setMatrix(matrix);                //绘制图像              canvas.drawBitmap(bitmap2, 35, 115, null);              //在onDrow中调用invalidate方法,表示不断重绘,即实现动画效果              invalidate();          }      }  }

```

相关栏目:

用户点评