Customizing view drawing images and moving fonts

Source: Internet
Author: User

Custom view is often encountered in development, such as the effect of a marquee, or customizing a carousel to show the percentage of the download progress. Write down the implementation today, the following is the source section:

Mainactivity: no change.

Import Android.app.activity;import Android.os.bundle;public class Mainactivity extends Activity {@Override protecte        d void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }}

Main layout: adds only one custom view

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent "android:layout_height=" match_parent "> <com.example.myview.mineview android:layout_width=" Wrap_conten T "android:layout_height=" wrap_content "> </com.example.myview.MineView></RelativeLayout>

Custom View:

Import java.util.random;import android.content.context;import android.graphics.canvas;import  android.graphics.paint;import android.graphics.rectf;import android.os.systemclock;import  android.util.attributeset;import android.view.view;public class mineview extends  View {    /**     *  x coordinate of font movement       */    private int rx = 0;    /**      *  Brush Tool      */    private paint  paint;    /**     *  creating thread objects for looping deferred execution commands       */    private MyThread t;    /**      * 0 means that the left,60 of the circle represents the top,100 of the Circle, which represents the right,160 of the circle, the bottom     */     private  RECTF&NBSP;RECTF&NBSP;=&NBSP;NEW&NBSP;RECTF (0, 60, 100, 160);    /**      *  indicates interval angle      */    private  float sweepangle;    /**     *  creating random Number Objects       */    private Random random;    /**      * isdestroy Indicates whether the activity has ended      */     private boolean isdestroy = false;    public mineview ( Context context, attributeset attrs)  {         Super (CONTEXT,&NBSP;ATTRS);         system.out.println ("constructor");         paint = new paint ();         random = nEw random ();         //anti-aliasing, otherwise the image will be difficult to see          paint.setantialias (True);        if  (t ==  null)  {            t = new  mythread ();             t.start ();         }    }    public  Mineview (Context context)  {        super (Context);         system.out.println ("constructor");         paint = new paint ();         paint.setantialias ( true);        if  (t == null)  {         &nbsP;   t = new mythread ();             t.start ();        }    }      @Override     protected void ondraw (Canvas canvas)  {         super.ondraw (Canvas);         paint.settextsize (+);         // rx represents the x-coordinate of a moving font          canvas.drawtext ("Mobile Font",  rx, 30, paint);   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;RECTF represents the Circle object, 0 represents the starting angle, sweepangle represents the interval angle, and true and false indicate how the process is drawn, Paint represents the Brush object         canvas.drawarc (Rectf, 0, sweepangle,  true, paint);     }    class mythread extends  Thread {         @Override         public void run ()  {            super.run ();             while  (True)  {                 rx += 5;                 //  If the font moves out of the screen, let the font start from scratch                  if  (rx  > getwidth ())  {                     rx =  (int)   (0 - paint.measuretext ("Mobile Font"));                 }                 sweepangle += 5;                 //  if the angle is greater than 360, let the circle start from 0 degrees to redraw                  if  (SweepAngle  > 360)  {                     sweepAngle = 0;                 }                 //  let colors choose from 0-255 randomly                  int r = random.nextint (;  )               int g =  Random.nextint (&NBSP;&NBSP;&NB);sp;             int b =  Random.nextint (;               )  //  respectively: Transparency (0~255), red, green, blue                  paint.setargb (255, r, g, b);                 /**                  *  determines if activity has exited and Isdestroy is true if exiting. Then immediately let the thread object and the Brush object be empty, and break,                  *  Otherwise it will cause a memory overflow, this method must be used here, otherwise it will cause a bug with null pointers.                  */                 if  (Isdestroy)  {                     if  (t != null)  {                          t = null;                     }                     if  (paint != null)  {                          paint = null;                     }                     break;                 }                 //  Sleep                  systemclock.sleep (;           )      //  allows the OnDraw method to be re-executed                  postinvalidate ();                 // invalidate (); sub-thread invalid, to use the main thread              }        }     }    /**     *  This method is called when the window is destroyed to close the resource       */     @Override     protected void ondetachedfromwindow ()  {         super.ondetachedfromwindow ();         isdestroy = true;        system.out.println (" Ondetachedfromwindow ");     }}

running effect: running up is dynamic, I will not upload dynamic effects, want to see the effect of their own run up on it.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/78/02/wKiom1Zzoh_ShUmcAABtzj_3aUE173.jpg "title=" Qq20151218140504.jpg "alt=" Wkiom1zzoh_shumcaabtzj_3aue173.jpg "/>

Note: Here the marquee effect is just a very stupid way, there is a simpler way, I will introduce later!

This article is from the "Mobile Platform Development" blog, make sure to keep this source http://liuxudong1001.blog.51cto.com/10877072/1726020

Customizing view drawing images and moving fonts

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.