Rotate Canvas with Bitmap Android example

drawPath, canvas.rotate, lineTo basic Android example for your testing.

canvas-rotate-image-android-1

canvas-rotate-image-android

// http://www.apache.org/licenses/LICENSE-2.0
// The Android Open Source Project
public class MainActivity extends Activity {
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}
 
	private static class SampleView extends View {
		private Paint mPaint = new Paint();
		private Path mPath = new Path();
 
		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);
 
			// Construct a wedge-shaped path
			mPath.moveTo(0, -60);
			mPath.lineTo(-20, 80);
			mPath.lineTo(0, 60);
			mPath.lineTo(20, 80);
			mPath.close();
		}
 
		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = mPaint;
 
			canvas.drawColor(Color.WHITE);
 
			paint.setAntiAlias(true);
			paint.setColor(Color.RED);
			paint.setStyle(Paint.Style.FILL);
			Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
					R.drawable.flower_blue);
			canvas.drawBitmap(bitmapOrg, 10, 10, paint);
			int w = canvas.getWidth();
			int h = canvas.getHeight();
			int cx = w / 2;
			int cy = h / 2;
 
			canvas.translate(cx, cy);
         // uncomment next line 
			//canvas.rotate(90.0f);
			canvas.drawPath(mPath, mPaint);
		}
 
	}
}
This entry was posted in Android Examples Code, Bitmap, drawing and tagged , , . Bookmark the permalink.


Comments are closed.



13,046 views