Android TV achieves color bar scrolling
Directly paste the 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; // The addition value of j for each re-painting public ColorView (Context context) {super (context ); this. context = context; WindowManager wm = (WindowManager) getContext (). getSystemService (Context. WINDOW_SERVICE); width = wm. getdefadisplay display (). getWidth (); height = wm. getdefadisplay display (). getHeight (); // determines whether it is a standard height System. out. println ("width =" + width); System. out. println ("heig Ht = "+ 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 (Con Text. WINDOW_SERVICE); width = wm. getdefadisplay display (). getWidth (); height = wm. getdefadisplay display (). 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) {// indicates the end. Still need to be drawn, otherwise the interface will become black myDraw (canvas); System. out. println ("end"); return;} myDraw (canvas); j + = mLength; // change the value of j to invalidate (); // redraw the view after onDraw, subjective animation} public void myDraw (Canvas canvas) {// create a paint brush // red (R: 255G: 0 B: 0) // orange (R: 255G: 156 B: 0) // yellow (R: 255G: 255 B: 0) // Green (R: 0G: 255 B: 0) // blue (R: G: 255 B: 255) // blue (R: 0G: 0 B: 255) // purple (R: 255G: 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 >=256 & I <= 511) {p. setColor (Color. rgb (511-I, 255, 0);} else if (I >= 512 & I <= 767) {p. setColor (Color. rgb (0,255, I-512);} else if (I >=768 & I <= 1023) {p. setColor (Color. rgb (0, 1023-I, 255);} else if (I >= 1024 & I <= 1279) {p. setColor (Color. rgb (I-1024, 0,255);} // processing different resolutions, resulting in differences in drawing, evenly divided color values, screen width 12 80. The color value range is also 1280. For Xiaomi TV, the screen width is 1920. if the screen width is exceeded, it needs to be evenly divided. The following is the processing method if (exteraLength! = 1) {if (I % exteraLength = exteraLength-1) {System. out. println ("exteraLength =" + exteraLength); System. out. println ("current =" + I); canvas. drawLine (I + I/exteraLength, 0, I + I/exteraLength, height, p); // draw a 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
In the onDraw () method of the custom view, the invalidate () method can be called to achieve the effect similar to moving a ball ..