Android Introduction to the drawing detailed _android

Source: Internet
Author: User
Tags drawtext

The previous common control introduced a lot, now to discuss the development of mobile phone commonly used in the drawing. To master the graphics of Android, first of all, we need to understand the basic use of the following graphic interface:

1.Bitmap, can come from the resources/file, can also be created in the program, the actual function of the image is equivalent to the storage space;

2.Canvas, close contact with bitmap, bitmap figurative content, then Canvas is to provide a number of methods to operate Bitamp platform;

3.Paint, in close contact with canvas, is the brush tool on the artboard, also used to set the style on the view control;

4.drawable, if the first three are invisible in the memory of the drawing, then Drawable is the first three draw the results of the interface. drawable multiple subclasses, such as bitmaps (bitmapdrawable), Graphics (shapedrawable), layers (layerdrawable), and so on.

This article mainly explains how to draw a custom image on the ImageView drawing and directly above the button (inheriting view's control). As shown in the following illustration:

Draw the resource picture directly:

Drawing and painting words on ImageView:

To paint directly on the control background:

Main.xml source code is as follows:

<?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= "44px" android:text= "Show Resource Picture" ></Button>
<button android:id= "@+id/button02" Android:layout_width= "Fill_parent" android:layout_height= "44px" android:text= "show and draw resource picture" ></Button>
<button android:id= "@+id/button03" android:layout_height= "44px" android:layout_width= "Fill_parent" Android:text = "Drawing on controls" ></Button>
<imageview android:id= "@+id/imageview01" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" ></ImageView>

</LinearLayout>

The Java program's source code is as follows:

Package Com.testdraw;
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 Testdraw extends activity {ImageView IV;
 Button Btn1,btn2,btn3,btn4;
 Resources R;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    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 {/// Iv.setbackgroundresource (R.drawable.icon);//Open resource Picture Bitmap Bmp=bitmapfactory.decoderesource (R, R.drawable.icon);
  Open the Resource picture Iv.setimagebitmap (BMP);
     else if (V==BTN2)//Show and paint resource picture {Bitmap Bmp=bitmapfactory.decoderesource (r, R.drawable.icon);/Read Only, can't draw directly on BMP
     
     Bitmap newb = Bitmap.createbitmap (+, config.argb_8888);
     Canvas canvastemp = new Canvas (NEWB);
     
     Canvastemp.drawcolor (color.transparent);
     Paint p = new Paint ();
     String familyname = "Song Body";
     typeface font = typeface.create (familyname,typeface.bold);
     P.setcolor (color.red);
     P.settypeface (font);
     P.settextsize (22); Canvastemp.drawtext ("Writing ...)."
     ", 50,50,p); Canvastemp.drawbitmap (BMP,)//Paint Iv.setimagebitmap (NEWB); else if (V==BTN3)//Directly on the button drawing {Bitmap newb = Bitmap.createbitmap (Btn3.getwidth (), Btn3.getheight (), Config.argb
  _8888);
    Canvas canvastemp = new Canvas (NEWB);
    Canvastemp.drawcolor (Color.White);
  Paint p = new Paint ();
  String familyname = "Song Body";
  typeface font = Typeface.create (familyname, typeface.bold);
  P.setcolor (color.red);
  P.settypeface (font);
  P.settextsize (20); Canvastemp.drawtext ("Writing ...)."
  ",, p);
  drawable drawable = new bitmapdrawable (NEWB);
  Btn3.setbackgrounddrawable (drawable); }
 }
 }
}
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.