直接贴代码:
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; /** * 色彩效果view1 */ public class ColorView extends View { int width; int height; Paint p; int i = 0; int all = 256 * 5;//颜色值变化 int exteraLength = 1; Context context = null; int j = 0; int mLength = 0;//每次重绘时j的增量值 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(); //判断是否是标准的高度 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) { //结束的情况。还是需要绘制的,不然界面会变黑 myDraw(canvas); System.out.println("end"); return; } myDraw(canvas); j+=mLength;//改变j的值 invalidate();//onDraw后,重新绘制view,主观上产生动画效果 } public void myDraw(Canvas canvas) { // 创建画笔 // 红(R:255 G:0 B:0) // 橙(R:255 G:156 B:0) // 黄(R:255 G:255 B:0) // 绿(R:0 G:255 B:0) // 青(R: G:255 B:255) // 蓝(R:0 G:0 B:255) // 紫(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 >= 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)); } //处理不同的分辨率,造成画图的差异性,均分颜色值,屏幕宽度1280,颜色值的范围也是1280刚刚好,如果是小米电视的,屏幕宽度为1920,多出来的,需要均分,下面便是处理方式 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);//画线 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>
在自定义view中的onDraw()方法中,调用invalidate()方法,可以实现类似于小球移动的效果。。
android tv 实现颜色条滚动效果
时间: 2024-12-20 15:18:25