Android為TV端助力:自訂view之太陽

來源:互聯網
上載者:User

標籤:orm   dimen   efault   ble   protect   dimens   des   ret   inf   

先看



package com.hhzt.iptv.lvb_w8.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

/**
* 自訂燈光選擇View
*
* @author Chuwe1
*/
public class LightView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

// 預設半徑
private static final int DEFAULT_RADIUS = 170;

private SurfaceHolder mHolder;
private Canvas mCanvas;
private boolean flag;
/**
* 當前進度
*/
private int mCurrentCount = 0;

// 圓和刻度的畫筆
private Paint mPaint;
// 指標畫筆
private Paint mPointerPaint;

// 畫布的寬高
private int mCanvasWidth, mCanvasHeight;
// 時鐘半徑
private int mRadius = DEFAULT_RADIUS;


public LightView(Context context) {
this(context, null);
}

public LightView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public LightView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

mHolder = getHolder();
mHolder.addCallback(this);

mPaint = new Paint();
mPointerPaint = new Paint();

mPaint.setColor(Color.WHITE);
mPaint.setAntiAlias(true); // 消除鋸齒
mPaint.setStrokeWidth(10); // 設定圓環的寬度
mPaint.setStrokeCap(Paint.Cap.ROUND); // 定義線段斷電形狀為圓頭
mPaint.setAntiAlias(true); // 消除鋸齒



mPointerPaint.setColor(Color.BLACK);
mPointerPaint.setAntiAlias(true);
mPointerPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPointerPaint.setTextSize(22);
mPointerPaint.setTextAlign(Paint.Align.CENTER);

setZOrderOnTop(true);
getHolder().setFormat(PixelFormat.TRANSLUCENT);

setFocusable(true);
setFocusableInTouchMode(true);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int desiredWidth, desiredHeight;
if (widthMode == MeasureSpec.EXACTLY) {
desiredWidth = widthSize;
} else {
desiredWidth = mRadius * 2 + getPaddingLeft() + getPaddingRight();
if (widthMode == MeasureSpec.AT_MOST) {
desiredWidth = Math.min(widthSize, desiredWidth);
}
}

if (heightMode == MeasureSpec.EXACTLY) {
desiredHeight = heightSize;
} else {
desiredHeight = mRadius * 2 + getPaddingTop() + getPaddingBottom();
if (heightMode == MeasureSpec.AT_MOST) {
desiredHeight = Math.min(heightSize, desiredHeight);
}
}

// +4是為了設定預設的2px的內邊距,因為繪製時鐘的圓的畫筆設定的寬度是2px
setMeasuredDimension(mCanvasWidth = desiredWidth + 20, mCanvasHeight = desiredHeight + 20);

mRadius = (int) (Math.min(desiredWidth - getPaddingLeft() - getPaddingRight(),
desiredHeight - getPaddingTop() - getPaddingBottom()) * 1.0f / 2);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
flag = true;
new Thread(this).start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}

@Override
public void run() {
draw();
}

private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
return false;
}
});


/**
* 繪製
*/
private void draw() {
try {
mCanvas = mHolder.lockCanvas();
if (mCanvas != null) {
//去掉背景顏色,使其透明
mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

mPaint.setColor(Color.WHITE);
//將座標系原點移至去除內邊距後的畫布中心
mCanvas.translate(mCanvasWidth * 1.0f / 2 + getPaddingLeft() - getPaddingRight(),
mCanvasHeight * 1.0f / 2 + getPaddingTop() - getPaddingBottom());
//繪製圓盤
mCanvas.drawCircle(0, 0, mRadius-30, mPaint);
mPaint.setColor(Color.WHITE);
//繪製時刻度
for (int i = 0; i < 10; i++) {
mCanvas.drawLine(0, mRadius, 0, mRadius - 10, mPaint);
mCanvas.rotate(36);
}
mPaint.setColor(Color.parseColor("#FEF37A"));
//繪製時刻度
for (int i = 0; i < mCurrentCount; i++) {
mCanvas.drawLine(0, -mRadius, 0, -(mRadius - 10), mPaint);
mCanvas.rotate(36);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (mCanvas != null) {
mHolder.unlockCanvasAndPost(mCanvas);
}
}
}

public void setCurrentCount(int count){
this.mCurrentCount = count;
invalidate();
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
this.setBackgroundColor(Color.TRANSPARENT);
}


}

Android為TV端助力:自訂view之太陽

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.