Android TV for color strip scrolling effect

Source: Internet
Author: User

Direct Sticker Code:

Colorview.java

Package Com.xxx.demo;import Android.content.context;import Android.graphics.canvas;import android.graphics.Color; Import Android.graphics.paint;import android.util.attributeset;import android.util.log;import Android.view.View;    Import android.view.windowmanager;/** * Color effect View1 */public class Colorview extends view {int width;    int height;    Paint p;    int i = 0;    int all = 256 * 5;//color value change int exteralength = 1;    Context context = NULL;    int j = 0;        int mlength = 0;//increment value of J at each redraw public Colorview (context context) {super (context);        This.context = context;        WindowManager wm = (WindowManager) getcontext (). Getsystemservice (Context.window_service);        width = Wm.getdefaultdisplay (). GetWidth ();        Height = Wm.getdefaultdisplay (). GetHeight ();        Determine if the standard height is System.out.println ("width==" + width);        System.out.println ("height==" + height);        p = new Paint ();   if (all >= width) {mlength = 80;     } else {mlength = 30;        Exteralength = (int) Math.ceil (1.0 * All/(Width-all));        } this.setfocusable (True);        This.setkeepscreenon (TRUE);    i = 0;        } public Colorview (context context, AttributeSet AttributeSet) {Super (context, attributeset);        This.context = context;        WindowManager wm = (WindowManager) getcontext (). Getsystemservice (Context.window_service);        width = Wm.getdefaultdisplay (). GetWidth ();        Height = Wm.getdefaultdisplay (). GetHeight ();        System.out.println ("width==" + width);        System.out.println ("height==" + height);        p = new Paint ();        P.setantialias (TRUE);        P.setstyle (Paint.Style.FILL);        if (all >= width) {mlength = 80;            } else {mlength = 30;        Exteralength = (int) Math.ceil (1.0 * All/(Width-all));        } this.setfocusable (True);        This.setkeepscreenon (TRUE);    i = 0;   } @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas); if (J > All) {//End case.            Still need to draw, otherwise the interface will darken Mydraw (canvas);            System.out.println ("End");        Return        } mydraw (canvas); j+=mlength;//change the value of J invalidate ();//ondraw, redraw view, subjectively animate} public void Mydraw (canvas canvas) {//Create brush//red ( r:255 g:0 b:0)//Orange (r:255 g:156 b:0)//Yellow (r:255 g:255 b:0)//Green (r:0 g:255 b:0)//Cyan (r:g:255 b:255)//Blue (r:0 g:0 b:255)//Violet (r:255 G:        b:255) System.out.println ("Canvas");        LOG.I ("canvas:x:", "Complexdraw");        i = 0;            while (I <= j) {if (I <= 255) {P.setcolor (Color.rgb (255, I, 0));            } else if (i >= && i <= 511) {P.setcolor (Color.rgb (511-i, 255, 0));            } else if (i >= && i <= 767) {p.setcolor (Color.rgb (0, 255, i-512)); } else if (i >= 768 && i <= 10) {P.setcolor (Color.rgb (0, 1023-i, 255));            } else if (i >= 1024x768 && i <= 1279) {P.setcolor (Color.rgb (i-1024, 0, 255)); }//processing different resolutions, resulting in the difference between the paint, the color value, the screen width 1280, the range of color values is also 1280 just good, if it is millet TV, screen width of 1920, more out, need to evenly, the following is the way to deal with the IF (exter Alength! = 1) {if (i% exteralength = = exteraLength-1) {System.out.println ("Exterale                    ngth== "+ exteralength);                    System.out.println ("current==" + i); Canvas.drawline (i + i/exteralength, 0, i + i/exteralength, height, p);//Draw line Canvas.drawline (i + I/                Exteralength + 1, 0, i + i/exteralength + 1, height, p);                    } else {System.out.println ("exteralength==1--------->" + exteralength);                    System.out.println ("current==" + i);                Canvas.drawline (i + i/exteralength, 0, i + i/exteralength, height, p);        }    } else {canvas.drawline (i, 0, I, height, p);        } i++; }    }}

Mainactivity.java

public class Mainactivity extends Activity {    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Requestwindowfeature (window.feature_no_title);        Setcontentview (R.layout.activity_color);    }}

Activity_color.xml

<?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 ">    <com.xxx.demo.colorview            android:id=" @+id/colorview "            android:layout_width=" fill _parent "            android:layout_height=" fill_parent "            /></linearlayout>

In the OnDraw () method in Custom view, call the Invalidate () method to achieve an effect similar to a small ball movement.

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.