Android uses OnDraw to implement an example of drawing operations in view

Source: Internet
Author: User
The following is an introduction to Android through the OnDraw implementation of the drawing operation in the view of the sample code for detailed analysis, the need for friends can come to refer to the next

The Android drawing operation implements the drawing in the OnDraw function by inheriting the view implementation.
The following is a simple example:

Copy Code code as follows:




public class Androidtest extends activity {


/** called when the "activity is" is a. */


@Override


public void OnCreate (Bundle savedinstancestate) {


super.oncreate (savedinstancestate);





myview mv = new MyView (this);


Setcontentview (MV);


    }





public class MyView extends View {


MyView (Context context) {


super (context);


     }





@Override


protected void OnDraw (Canvas Canvas) {


//TODO auto-generated method stub


Super.ondraw (canvas);





//First define a paint


Paint Paint = new Paint ();


//Draw rectangular area-filled rectangle


//Set color


Paint.setcolor (Color.Blue);


//Set style-fill


Paint.setstyle (Style.fill);


//Draw a rectangle


Canvas.drawrect (New Rect (0, 0, getwidth (), GetHeight ()), paint);


//Painted hollow rectangle


//Set color


Paint.setcolor (color.red);


Set style-Hollow rectangle


Paint.setstyle (Style.stroke);


//Draw a rectangle


Canvas.drawrect (New Rect (a), paint);


//Painting text


//Set color


Paint.setcolor (Color.green);


//Painting text


canvas.drawtext ("Hello", m, paint);


//Drawing


//Generate bitmaps from the resource file


Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.icon);


//Drawing


Canvas.drawbitmap (bitmap, n, 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.