android tv 實現顏色條滾動效果

來源:互聯網
上載者:User

標籤:android   canvas   電視   

直接貼代碼:

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()方法,可以實作類別似於小球移動的效果。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.