Android custom Controls (vi) Refresh

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.

:

Click to change color

OnDraw after drawing, set the Click event to the control, upload the parameters to the control, and invalidate refresh

1.onDraw drawing, and adding ChangeColor methods

[Java]View Plaincopy
  1. Public class CUSVIEW3 extends View {
  2. private int color = 0;
  3. Public CUSVIEW3 (context context, AttributeSet attrs) {
  4. Super (context, attrs);
  5. }
  6. @Override
  7. protected void OnDraw (canvas canvas) {
  8. Super.ondraw (canvas);
  9. Paint Mpaint = new Paint ();
  10. if (Color > 2) {
  11. color = 0;
  12. }
  13. switch (color) {
  14. Case 0:
  15. Mpaint.setcolor (Color.green);
  16. Break ;
  17. case 1:
  18. Mpaint.setcolor (color.red);
  19. Break ;
  20. Case 2:
  21. Mpaint.setcolor (Color.Blue);
  22. Break ;
  23. Default:
  24. Break ;
  25. }
  26. Mpaint.setstyle (Style.fill);
  27. Mpaint.settextsize (35.0f);
  28. Canvas.drawtext ("Click Me Refresh", ten, mpaint);
  29. }
  30. public void ChangeColor () { //In order to let the outside call
  31. color++;
  32. }
  33. }

2. Layout

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <xue.test.CusView3
  8. android:id="@+id/cusview3"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. >
  12. </xue.test.CusView3>
  13. </linearlayout>



3. After drawing, set the Click event to the control, upload the parameters to the control, then invalidate refresh

[Java]View Plaincopy
  1. Public class Testcustomviewactivity extends Activity {
  2. @Override
  3. public void OnCreate (Bundle savedinstancestate) {
  4. super.oncreate (savedinstancestate);
  5. Setcontentview (R.layout.main);
  6. VIEW3 = (CUSVIEW3) Findviewbyid (R.ID.CUSVIEW3);
  7. //Click events
  8. View3.setonclicklistener (new View.onclicklistener () {
  9. @Override
  10. public void OnClick (View v) {
  11. Message message = new Message ();
  12. Message.what = 1;
  13. Myhandler.sendmessage (message);
  14. }
  15. });
  16. }
  17. Handler MyHandler = new Handler () {
  18. //Receive message after processing
  19. public void Handlemessage (Message msg) {
  20. switch (msg.what) {
  21. case 1:
  22. //Call method
  23. View3.changecolor ();
  24. //Refresh method
  25. View3.invalidate ();
  26. Break ;
  27. }
  28. super.handlemessage (msg);
  29. }
  30. };
  31. private CUSVIEW3 view3;
  32. }

As for the problem with custom controls taking up the entire screen, you may need to use Layoutparams

SOURCE Download: http://download.csdn.net/detail/ethan_xue/4152203

Android custom Controls (vi) Refresh

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.