Android front-end rendering image,

Source: Internet
Author: User

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:




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.