The Android custom view control implements the refresh effect _android

Source: Internet
Author: User

Three ways to get Linearinflater

A. Layoutinflater Inflater = Getlayoutinflater ();

B. layoutinflater Localinflater =

(Layoutinflater) Context.getsystemservice
(Context.layout_inflater_service);

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

OnDraw method drawing, Invalidate refresh interface.

Effect Chart:

Click to change color

OnDraw finish drawing, set click event for control, pass parameter to control, then invalidate refresh

1.onDraw drawing and adding ChangeColor method

public class CusView3 extends View { 
private int color = 0; 
Public CusView3 (context, AttributeSet attrs) { 
Super (context, attrs); 
} 
@Override 
protected void OnDraw (Canvas 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 (35.0f); 
Canvas.drawtext ("Click Me Refresh", "n", Mpaint); 
} 
public void ChangeColor () {//To allow outside calls to 
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.cusview3 
android:id= "@+id/cusview3" 
android:layout_ Width= "Wrap_content" 
android:layout_height= "wrap_content" 
> 
</xue.test.CusView3> 
</LinearLayout>

3. Set the Click event for the control after drawing, pass the parameter to the control, then invalidate refresh

public class Testcustomviewactivity extends activity { 
@Override public 
void OnCreate (Bundle Savedinstancestate) { 
super.oncreate (savedinstancestate); 
Setcontentview (r.layout.main); 
VIEW3 = (CUSVIEW3) Findviewbyid (R.ID.CUSVIEW3); 
Click event 
View3.setonclicklistener (new View.onclicklistener () { 
@Override public 
void OnClick (View v) { Message Message 
= new Message (); 
Message.what = 1; 
Myhandler.sendmessage (message); 
} 
); 
Handler MyHandler = new Handler () { 
//to handle public 
void Handlemessage (msg) { 
switch (msg.what) after receiving a message {Case 
1: 
//Call Method 
View3.changecolor (); 
Refresh Method 
view3.invalidate (); 
break; 
Super.handlemessage (msg); 
} 
; 
Private CusView3 view3; 
}

As for the custom control to occupy the whole screen problem, may need to use the Layoutparams

The above is a small set to introduce the Android Custom view control to achieve the refresh effect, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone!

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.