首页 技术 正文
技术 2022年11月23日
0 收藏 318 点赞 4,209 浏览 4050 个字
先看效果图

Android为TV端助力:自定义view之太阳

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);
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,116
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,588
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,434
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,204
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,840
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,925