Android Foreground rendering pictures

Source: Internet
Author: User
Tags drawtext

Android foreground rendering, mainly rewrite the view OnDraw method, in the canvas operation

Custom MyView Class

 PackageCom.ssln;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Matrix;ImportAndroid.graphics.Paint;ImportAndroid.util.AttributeSet;ImportAndroid.view.View; Public classMyViewextendsView {PrivateBitmap Bitmap;//Image    PrivatePaint paint;//Brushes     PublicMyView (Context context, AttributeSet attrs) {Super(context, attrs);    Initbitmap (); }    /*** Initialization Information*/     Public voidInitbitmap () {//Instantiate a brushPaint=NewPaint (); //Load from Resourcebitmap=Bitmapfactory.decoderesource (Getresources (), r.drawable.img); } @SuppressLint ("Drawallocation") @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Paint.setantialias (true);//turn on anti-aliasingPaint.setcolor (Color.Black);//Set Brush ColorPaint.settextscalex (15);//Set Text SizeCanvas.drawbitmap (bitmap, 10,10, paint);//draw pictures in 10x10 's positionCanvas.save ();//Save Canvas StateMatrix M1=NewMatrix ();//MatrixM1.settranslate (500, 10);//Pan X500 Y10Matrix m2=NewMatrix (); M2.setrotate (15);//Rotate 15 °Matrix m3=NewMatrix ();            M3.setconcat (M1, M2); //merging matricesM1.setscale (0.8f, 0.8f);//set the zoom ratioM2.setconcat (M3, M1);//MergingCanvas.drawbitmap (Bitmap, M2, paint);//drawing, panning, rotating, zoomingCanvas.restore ();//Restore Canvas StateCanvas.save (); Paint.setalpha (180);//Set TransparencyM1.settranslate (200, 100); M2.setscale (1.3f, 1.3f);        M3.setconcat (M1, M2);  Canvas.drawbitmap (bitmap, m3, paint); //drawing, panning, zoomingPaint.reset ();//Reset BrushCanvas.restore (); Paint.settextsize (24);        Paint.setcolor (Color.Black); Canvas.drawtext ("Picture width:" +bitmap.getwidth (), 20,240, paint);//write the text, the width of the imageCanvas.drawtext ("Picture height:" +bitmap.getheight (), 20,270, paint);        Paint.reset (); } }

Modify Activity_main.xml

<Relativelayoutxmlns: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.MyViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"         /></Relativelayout>

Run the following effect

Android Foreground rendering pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.