Android實現圖像旋轉
圖像旋轉的基本思想是通過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(); } } } </pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!