Android implements custom View---drawing pictures

Source: Internet
Author: User

The main principle is: Extends view overload ondrow (), need a bitmap, overload view, the canvas to draw temporary BMP, when pressed to determine the time to press

Draw on the real_bmp, the other front surface basically no difference.


Import Com.example.test.r;import Android.app.activity;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.canvas;import Android.os.bundle;import Android.provider.contactscontract.commondatakinds.event;import Android.util.attributeset;import Android.util.Log ; Import Android.view.motionevent;import Android.view.view;public class Bmpeffect extends activity{/* requires a bitmap, overloaded vie W, in the canvas to draw temporary BMP, when pressed to determine the time to press the * draw to Real_bmp, the other front face basically no difference */private static Bitmap src;private static Bitmap HEART;PR  Ivate static Bitmap star;private static Bitmap star2;private static int choose;private int Height;private int Width;public    static float bmp_x;  public static float bmp_y;private Canvas my_canvas;public void OnCreate (Bundle save) {LOG.E ("on Create", "Hello");  Super.oncreate (save);  Src=data.src_bmp;  if (data.src_bmp==null) log.e ("BMP", "null");  My_canvas=new canvas (data.src_bmp);  Height=data.src_bmp.getheight (); Width=data.src_bmp.getwidth (); Heart=data.geTbmp (Getresources (), R.drawable.small_heart); Star=data.getbmp (Getresources (), R.drawable.small_star);  Star2=data.getbmp (Getresources (), R.DRAWABLE.STAR8);  choose=0;  Setcontentview (r.layout.bmptest);  } public void On_star2 (final View v) {//star8;    choose=3;  } public void On_star (final View v) {choose=2;  } public void On_heart (final View v) {choose=1;  public void On_ok (final View v) {drow_bmp (My_canvas);  public static void Drow_bmp (canvas canvas) {switch (choose) {Case 1:canvas.drawbitmap (heart, bmp_x, bmp_y, NULL);  Break  Case 2:canvas.drawbitmap (star, bmp_x,bmp_y, NULL);  Break  Case 3://canvas.drawbitmap (star2, Matrix, Paint) Canvas.drawbitmap (star2, bmp_x, bmp_y, NULL);  }} public static class Bmpview extends View{public Bmpview (Context context,attributeset attrs) {super (CONTEXT,ATTRS);// TODO auto-generated constructor stub} protected void OnDraw (canvas canvas) {canvas.drawcolor (0xff000000); LOG.I ("Canvas", "Drow"); if (src!=null) {Canvas.drawbitmaP (SRC, 0, 0, NULL); } drow_bmp (canvas); } public boolean ontouchevent (Motionevent event) {int action=event.getaction ();//should now be able to not distinguish between different gesture events, only need to get bmp_x= Event.getx (); Bmp_y=event.gety (); Invalidate (); return true; }  } }
The 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.