Android custom control (6) Refresh

Source: Internet
Author: User

Three methods to obtain linearinflater

A. layoutinflater Inflater = getlayoutinflater ();

B. layoutinflater localinflater =
(Layoutinflater) Context. getsystemservice
(Context. layout_inflater_service );

C. layoutinflater Inflater = layoutinflater. From (context );

The ondraw method is used for drawing, and the invalidate interface is refreshed.

:

Click to change color

After the ondraw graph is drawn, set the click event for the control, pass the parameter to the control, and refresh the invalidate

1. Draw an image with ondraw and add the changecolor method.

Public class cusview3 extends view {private int color = 0; Public cusview3 (context, attributeset attrs) {super (context, attrs) ;}@ overrideprotected void ondraw (canvas) {super. ondraw (canvas); paint mpaint = new paint (); If (color> 2) {color = 0;} switch (color) {Case 0: mpaint. setcolor (color. green); break; Case 1: mpaint. setcolor (color. red); break; Case 2: mpaint. setcolor (color. blue); break; default: break;} mpaint. setstyle (style. fill); mpaint. settextsize (35366f); canvas. drawtext ("click to refresh", 10, 60, mpaint);} public void changecolor () {// to make the outside call color ++ ;}}

2. Layout

<?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"    ><xue.test.CusView3android:id="@+id/cusview3"android:layout_width="wrap_content"android:layout_height="wrap_content"></xue.test.CusView3></LinearLayout>

3. After drawing, set a click event for the control, upload the parameters to the control, and refresh the invalidate.

Public class testcustomviewactivity extends activity {@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); view3 = (cusview3) findviewbyid (R. id. cusview3); // click the event view3.setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {message = new message (); message. what = 1; myhandler. sendmessage (Message) ;}}) ;}handler myhandler = new handler () {// public void handlemessage (Message MSG) {Switch (MSG. what) {Case 1: // call method view3.changecolor (); // refresh method view3.invalidate (); break;} super. handlemessage (MSG) ;}}; private cusview3 view3 ;}

Layoutparams may be used to occupy the entire screen.

Source code download: http://download.csdn.net/detail/ethan_xue/4152203

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.