Image Bitmap of Android custom View
Overview:
Bitmapstands for a map. The extended name can be .bmp or. dib. A bitmap is a Windows Standard Format Image File that defines an image as a point (pixel). Each point can be represented by multiple colors, including 2, 4, 8, 16, 24, and 32-bit colors ..
In the android system, bitmap is one of the most important classes in image processing. It can be used to obtain image file information, perform image cutting, rotation, scaling, symmetry, and other operations, and save image files in a specified format.
Bitmap operations: demo
First, write a custom View:
Public class MyBitmapView extends View {private int width; private int height; private int mBitmapWidth; private int mBitmapHeight; private Paint mPaint; private Bitmap mBitmap; private Matrix matrix; public MyBitmapView (Context context) {super (context);} public MyBitmapView (Context context, AttributeSet attrs) {super (context, attrs); mPaint = new Paint (); mBitmap = BitmapFactory. decodeResource (getResources (), R. mipmap. aa); mBitmapWidth = mBitmap. getWidth (); mBitmapHeight = mBitmap. getHeight (); matrix = new Matrix () ;}@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); width = getDefaultSize (rows (), widthMeasureSpec); height = getDefaultSize (rows (), heightMeasureSpec); setMeasuredDimension (width, height );} @ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); // translated image. Parameter: horizontal phase shifting, vertical phase shifting matrix. postTranslate (mBitmapWidth, mBitmapHeight); canvas. drawBitmap (mBitmap, matrix, mPaint); // resets the matrix. The previous State disappears. reset (); // enlarge the image. Parameter: horizontal magnification and vertical magnification. If the value is smaller than 1, the image is reduced to matrix. postScale (2, 2); canvas. drawBitmap (mBitmap, matrix, mPaint); matrix. reset (); canvas. drawBitmap (mBitmap, 0, 0, mPaint); matrix. reset (); canvas. drawBitmap (mBitmap, 0, 0, mPaint); // select 180 degrees for the image. The position of the image after rotation changes to matrix. postRotate (180); matrix. postTranslate (mBitmapWidth * 2, mBitmapHeight * 2); canvas. drawBitmap (mBitmap, matrix, mPaint); // side-cut canvas. drawBitmap (mBitmap, 0, 0, mPaint); matrix. reset (); // a side slice an image. In fact, the x and y coordinates in the lower right corner of the image are multiplied by parameters. Here, the x coordinates remain unchanged, and the y coordinates are multiplied by matrix. postSkew (0, 1); canvas. drawBitmap (mBitmap, matrix, mPaint); // X axis symmetric matrix. reset (); canvas. drawBitmap (mBitmap, 0, 0, mPaint); float values [] = {1f, 0f, 0f, 0f,-1f, 0f, 0f, 0f, 1f }; // This method is used to multiply the input matrix and the image matrix. setValues (values); matrix. postTranslate (0, mBitmapHeight * 2); canvas. drawBitmap (mBitmap, matrix, mPaint); // y-axis symmetric matrix. reset (); canvas. drawBitmap (mBitmap, 0, 0, mPaint); float values2 [] = {-1f, 0f, 1f, 0f, 1f, 0f, 0f, 0f, 1f}; matrix. setValues (values2); matrix. postTranslate (mBitmapWidth * 2, 0); canvas. drawBitmap (mBitmap, matrix, mPaint );}}
Result demonstration:
Translation:
Scaling: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPjxiciAvPg0KPGltZyBhbHQ9 "here write picture description" src = "http://www.bkjia.com/uploads/allimg/150919/04215234H-1.png" title = "\"/>
Rotation:
Side slice:
X axis symmetry:
Y axis symmetry: