Android实现图像旋转
2018-07-20 来源:open-open
图像旋转的基本思想是通过Matrix类的setRotate方法设置旋转的角度,然后使用Bitmap.createBitmap方法创建一个已经旋转了的图像。除此之外,还可以使用Canvas.setMatrix方法设置,并直接使用drawBitmap绘制。
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();
}
}
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:一个php 生成zip文件的类
最新资讯
热门推荐