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

Source: Internet
Author: User
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 the activity 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-padding
Paint.setstyle (Style.fill);
Draw a rectangle
Canvas.drawrect (New Rect (0, 0, getwidth (), GetHeight ()), paint);
Draw a hollow rectangle
Set color
Paint.setcolor (color.red);
Set style-Hollow rectangle
Paint.setstyle (Style.stroke);
Draw a rectangle
Canvas.drawrect (New Rect (), paint);
Painting text
Set color
Paint.setcolor (Color.green);
Painting text
Canvas.drawtext ("Hello", m, paint);
Drawing
To generate a bitmap from a resource file
Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.icon);
Drawing
Canvas.drawbitmap (bitmap, 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.