Android Beginner 2016 (16)--Paint

Source: Internet
Author: User
Tags drawtext

This article comes from the legend of the Fat Treasure Road , the reference must be indicated by the source!

Drawing design to the format of the picture, there are free to look at the picture resources in various formats. Learn a piece of formatting that is useful for learning. And I also like to ask this question when I interview others, haha.

Let's look at a picture first.


Look directly at the code, the comments are very detailed.

Activity_hello_world.xml

<?xml      Version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"  android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" > <button android:id= "@+id/button01" android:layout_width= "fill_parent" android:layout_height= "55DP" Android : text= "Show resource Picture" ></Button> <button android:id= "@+id/button02" android:layout_width= "Fill_parent" Androi d:layout_height= "55DP" android:text= "show and draw resource picture" ></Button> <button android:id= "@+id/button03" Android: layout_height= "55DP" android:layout_width= "fill_parent" android:text= "Drawing on Controls" ></Button> <imageview an Droid:id= "@+id/imageview01" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" ></ Imageview> </LinearLayout> 
Helloworldactivity.java

Package Com.fable.helloworld;  Import android.app.Activity;  Import Android.content.res.resources;import Android.graphics.Bitmap; Import Android.graphics.bitmap.config;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.typeface;import Android.graphics.drawable.bitmapdrawable;import Android.graphics.drawable.drawable;import Android.os.Bundle; Import Android.view.view;import Android.widget.button;import Android.widget.imageview;public class      Helloworldactivity extends Activity {ImageView IV;      Button Btn1,btn2,btn3;      Resources R;          @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                  Setcontentview (R.layout.activity_hello_world);          iv= (ImageView) This.findviewbyid (R.ID.IMAGEVIEW01);          btn1= (Button) This.findviewbyid (R.ID.BUTTON01);    Btn2= (Button) This.findviewbyid (R.ID.BUTTON02);      btn3= (Button) This.findviewbyid (R.ID.BUTTON03);          Btn1.setonclicklistener (New Clickevent ());          Btn2.setonclicklistener (New Clickevent ());                    Btn3.setonclicklistener (New Clickevent ());      R = this.getresources (); } class Clickevent implements View.onclicklistener {public void OnClick (View v) {if (v==btn1                  //Display resource picture {//function equivalent//iv.setbackgroundresource (R.DRAWABLE.FABLE5);//Open resource picture  Bitmap Bmp=bitmapfactory.decoderesource (R, R.drawable.fable5);//Open resource Picture Iv.setimagebitmap (BMP); Show Picture} else if (V==BTN2)//display and paint the resource picture {Bitmap Bmp=bitmapfactory.dec Oderesource (R, R.drawable.fable5);//read-only, not directly on BMP Bitmap Newb = Bitmap.createbitmap (+, Config.arg                b_8888);//Create a new bitmap canvas canvastemp = new canvas (NEWB);//canvas, canvas, with newb content to draw on canvasCanvastemp.drawcolor (color.transparent);//Set the background color of the canvas to transparent TRANSPARENT paint p = new paint ();                /brushes, haha, a little meaning, and the canvas and brush String familyname = "song body";//using the Arial, you can use non-system fonts, the game many fonts are special, need to put in assets/fonts/  Typeface font = typeface.create (familyname,typeface.bold);//Bold P.setcolor (Color.Red);  The brush color is red p.settypeface (font);  Set Font p.settextsize (24); Font size Canvastemp.drawtext ("The road of the legend of the Fat Treasure", 50,50,p); Draw a few words on the canvas canvastemp.drawbitmap (BMP, S, p);//Draw on the canvas just read the FABLE5//above is painted on the canvas on the newb map                of the surface.  Iv.setimagebitmap (NEWB); Show this new picture} else if (V==BTN3)//Draw directly on the button {Bitmap newb = bitmap.c Reatebitmap (Btn3.getwidth (), Btn3.getheight (), config.argb_8888); Create a diagram canvas canvastemp = new canvas (NEWB);//Put the picture on the canvas Canvastemp.drawcolor (color.whit            E);//Background color is white      Paint p = new paint ();  Brush String familyname = "Arial";  Use arial Typeface font = Typeface.create (familyname, typeface.bold);  Set Font P.setcolor (Color.Blue);  Blue p.settypeface (font);  Sets the font of the brush p.settextsize (24);  Font size canvastemp.drawtext ("Android starter", +, p); Write a few words, xy do not fill too large, over the button size position can not find the drawable drawable = new Bitmapdrawable (Getapplicationcontext (). Getresources () , NEWB);  In fact, this look is not very clear btn3.setbackgrounddrawable (drawable); Take this image as the background of the Button}}}

The code is uploaded later.



Android Beginner 2016 (16)--Paint

Related Article

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.