MainActiviry is as follows:
package com.cn;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity { private DrawBitmapView mDrawBitmapView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDrawBitmapView=new DrawBitmapView(MainActivity.this, null); setContentView(mDrawBitmapView); } }
DrawBitmapView:
Package com.cn; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. matrix; import android. graphics. paint; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; // question 0: // Matrix m1 = new Matrix (); // pay attention to the introduction of this class package, android. graphics. matrix // Question 1: // use of post, set, and pre // both can change the Matrix, but the usage is not the same. // 1.1 If you use set multiple times consecutively, only the last set plays a role. // 1.2 it is commonly used to use set for the first time, post is used later. // It indicates the append. // 1.3pre indicates the first occurrence. it will occur before post and set // problem 2: // Rotate by default is (0, 0) as the reference point // so you often need to set the reference rotation point // m1.postRotate (30, 70, 100); // Question 3: // When Alpha is set // mPaint. setAlpha (30); // value range: [0 .. 255] // remarks: // it is best to separate the following test methods for public class DrawBitmapView extends View {Bitmap mBitmap; Paint mPaint; public DrawBitmapView (Context context, AttributeSet attrs) {super (context, attrs); initView ();} public void initView () {mPaint = new Paint (); mBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. ic_launcher) ;}@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); // -----> Test 1: // draw a graph first /// then draw the image after displacement and rotation, the scaled image (method 1) //// first draw // mPaint. setAntiAlias (true); // canvas. drawBitmap (mBitmap, 50, 50, mPaint); // canvas. save (); //// second draw // Matrix m1 = new Matrix (); // m1.setTranslate (70,100); // m1.postScale (1.5f, 1.5f ); // m1.postRotate (30, 70,100); // canvas. drawBitmap (mBitmap, m1, mPaint); // -----> Test 2: /// draw a graph first /// then draw the image after displacement and rotation, the scaled image (method 2) //// first draw // mPaint. setAntiAlias (true); // canvas. drawBitmap (mBitmap, 50, 50, mPaint); // canvas. save (); // second draw // Matrix m2 = new Matrix (); // m2.setTranslate (70,100); // m2.postScale (1.5f, 1.5f ); // Matrix m3 = new Matrix (); // mPaint. setAlpha (50); // m3.setRotate (30, 70,100); // use setConcat to concatenate two transformations // Matrix m4 = new Matrix (); // m4.setConcat (m2, m3); // canvas. drawBitmap (mBitmap, m4, mPaint); // canvas. restore (); // canvas. save (); // -----> Test 3: The preXXX () method and the xxxSkew () method // This method will occur at the beginning of the current matrix // For example, in this example, it will rotate first and then shift // draw the mPaint for the first time. setAntiAlias (true); canvas. drawBitmap (mBitmap, 50, 50, mPaint); canvas. save (); // The second time Matrix m5 = new Matrix (); m5.setTranslate (100,200); m5.preRotate (30, 50, 50); // skew indicates tilting m5.postSkew (0.2f, 0.2f, 50, 50); canvas. drawBitmap (mBitmap, m5, mPaint);} @ Override public boolean onTouchEvent (MotionEvent event) {return super. onTouchEvent (event );}}