Android front-end rendering image,
Android front-end rendering, mainly to override the ondraw method of the view, operate in the canvas
Custom MyView class
Package com. ssln; import android. annotation. suppressLint; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. matrix; import android. graphics. paint; import android. util. attributeSet; import android. view. view; public class MyView extends View {private Bitmap bitmap; // picture private Paint paint; // brush public MyView (Context context, AttributeSet attrs) {super (context, attrs ); initBitmap ();}/*** initialization information */public void initBitmap () {// instantiate paint Paint = new paint (); // load bitmap = BitmapFactory from the resource. decodeResource (getResources (), R. drawable. img) ;}@ SuppressLint ("DrawAllocation") @ Override protected void onDraw (Canvas canvas Canvas) {super. onDraw (canvas); paint. setAntiAlias (true); // enable anti-sawtooth paint. setColor (Color. BLACK); // set the paint color to paint. setTextScaleX (15); // set the text size canvas. drawBitmap (bitmap, 10, 10, paint); // draw the image canvas at 10x10. save (); // save the canvas State Matrix m1 = new Matrix (); // Matrix m1.setTranslate (500, 10); // translate X500 Y10 Matrix m2 = new Matrix (); m2.setRotate (15); // rotate 15 ° Matrix m3 = new Matrix (); m3.setConcat (m1, m2); // merge Matrix m1.setScale (0.8f, 0.8f ); // set the zoom ratio m2.setConcat (m3, m1); // merge the canvas. drawBitmap (bitmap, m2, paint); // draws a picture, which is translated, rotated, and scaled to the canvas. restore (); // restore canvas status canvas. save (); paint. setAlpha (180); // set the transparency m1.setTranslate (200,100); m2.setScale (1.3f, 1.3f); m3.setConcat (m1, m2); canvas. drawBitmap (bitmap, m3, paint); // draws a picture. After translation, the painting is scaled. reset (); // reset the paint brush canvas. restore (); paint. setTextSize (24); paint. setColor (Color. BLACK); canvas. drawText ("Image Width:" + bitmap. getWidth (), 20,240, paint); // write text, image width canvas. drawText ("Image Height:" + bitmap. getHeight (), 20,270, paint); paint. reset ();}}
Modify activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.ssln.MainActivity" > <com.ssln.MyView android:layout_width="fill_parent" android:layout_height="fill_parent" /></RelativeLayout>
The running effect is as follows: