/**
* 绘制Icon
* @param tmpAngle
* @param bitmap
*/
private void drawIcon(float tmpAngle, Bitmap bitmap) {
//设置图片的宽度为直径1/8
int imgWidth=mRadius/8;
//PI/180 ->一度
//angle弧度值
float angle = (float) ((tmpAngle+360/mItemCount/2)*Math.PI/180);
int x= (int) (mCenter+mRadius/2/2*Math.cos(angle));
int y= (int) (mCenter+mRadius/2/2*Math.sin(angle));
//确定图片位置
Rect rect=new Rect(x-imgWidth/2,y-imgWidth/2,x+imgWidth,y+imgWidth/2);
mCanvas.drawBitmap(bitmap,null,rect,null);
}
package myapplication3.xt.com.myapplication;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* Created by TONG on 2017/3/22.
*/
public class LuckyPan extends SurfaceView implements SurfaceHolder.Callback,Runnable {
private SurfaceHolder mHolder;
private Canvas mCanvas;
/**
* 用于绘制的线程
*/
private Thread t;
/**
* 线程的控制开关
*/
private boolean isRunning;
/**
* 盘块的奖项
*/
private String[] mStrs=new String[]{"单反相机","IPAD","恭喜发财","IPHONE","服装一套","恭喜发财"};
/**
* 盘块的图片
*/
private int[] mImgs=new int[]{R.mipmap.danfan, R.mipmap.ipad, R.mipmap.f040, R.mipmap.iphone, R.mipmap.meizi, R.mipmap.f040};
/**
* 与图片对应的bitmap数组
*/
private Bitmap[] mImgBitmap;
private Bitmap mBigBitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.bg2);
/**
* 盘块的颜色
*/
private int[] mColors =new int[]{0XFFFFC300, 0XFFF17E01, 0XFFFFC300, 0XFFF17E01, 0XFFFFC300, 0XFFF17E01
};
private int mItemCount=6;
/**
* 绘制盘块的画笔
*/
private Paint mArcPaint;
/**
* 绘制文本的画笔
*/
private Paint mTextPaint;
/**
* 盘块的背景图
* RectF与Rect区别:1、精度不一样,Rect是使用int类型作为数值,RectF是使用float类型作为数值
2、两个类型提供的方法也不是完全一致
*/
private RectF mRange=new RectF();
/**
* 整个盘块的直径
*/
private int mRadius;
/**
* 文字大小 20sp
*/
private float mTextSize= TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,20,getResources().getDisplayMetrics());
/**
* 滚动的速度
*/
private double mSpeed;
/**
*
* 可能有两个线程操作
* 保证线程间变量的可见性
*/
private volatile int mStartAngle=0;
/**
* 是否点击了停止按钮
*/
private boolean isShouldEnd;
/**
* 转盘的中心位置
*/
private int mCenter;
/**
* 这里我们的padding直接以paddingLeft为准
*/
private int mPadding;
public LuckyPan(Context context) {
this(context,null);
}
public LuckyPan(Context context, AttributeSet attrs) {
super(context, attrs);
mHolder=getHolder();
mHolder.addCallback(this);
//获得焦点
setFocusable(true);
setFocusableInTouchMode(true);
//设置常亮
setKeepScreenOn(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width=Math.min(getMeasuredWidth(),getMeasuredHeight());
mPadding=getPaddingLeft();
//直径
mRadius=width-mPadding*2;
//中心点
mCenter=width/2;
setMeasuredDimension(width,width);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
//初始化绘制盘块的画笔
mArcPaint=new Paint();
//防止边缘的锯齿
mArcPaint.setAntiAlias(true);
//设置递色
mArcPaint.setDither(true);
//初始化绘制文本的画笔
mTextPaint=new Paint();
mTextPaint.setColor(0xffffffff);
mTextPaint.setTextSize(mTextSize);
//初始化盘块绘制的范围
mRange=new RectF(mPadding,mPadding,mPadding+mRadius,mPadding+mRadius);
//初始化图片
mImgBitmap=new Bitmap[mItemCount];
for(int i=0;i<mImgBitmap.length;i++){
mImgBitmap[i]=BitmapFactory.decodeResource(getResources(),mImgs[i]);
}
isRunning=true;
t=new Thread(this);
t.start();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
isRunning=false;
}
@Override
public void run() {
//不断进行绘制
while (isRunning){
long start=System.currentTimeMillis();
draw();
long end=System.currentTimeMillis();
if(end-start<50)
{
try {
Thread.sleep(50-(end-start));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void draw() {
try {
mCanvas=mHolder.lockCanvas();
if(mCanvas!=null){
//绘制背景
drawBg();
//绘制盘块
float tmpAngle=mStartAngle;
float sweepAngle=360/mItemCount;
for(int i=0;i<mItemCount;i++)
{
mArcPaint.setColor(mColors[i]);
//绘制盘块
//绘制圆弧
//第一个参数:绘制区域
//第二个参数:起始角度
//第三个参数:每个盘块角度
//第三个参数(useCenter):要不要使用中间原点
//第四个参数:画笔
mCanvas.drawArc(mRange,tmpAngle,sweepAngle,true,mArcPaint);
//绘制文本
drawText(tmpAngle,sweepAngle,mStrs[i]);
//绘制Icon
drawIcon(tmpAngle,mImgBitmap[i]);
tmpAngle+=sweepAngle;
}
}
} catch (Exception e) {
}finally {
if(mCanvas!=null)
{
mHolder.unlockCanvasAndPost(mCanvas);
}
}
}
/**
* 绘制Icon
* @param tmpAngle
* @param bitmap
*/
private void drawIcon(float tmpAngle, Bitmap bitmap) {
//设置图片的宽度为直径1/8
int imgWidth=mRadius/8;
//PI/180 ->一度
//angle弧度值
float angle = (float) ((tmpAngle+360/mItemCount/2)*Math.PI/180);
int x= (int) (mCenter+mRadius/2/2*Math.cos(angle));
int y= (int) (mCenter+mRadius/2/2*Math.sin(angle));
//确定图片位置
Rect rect=new Rect(x-imgWidth/2,y-imgWidth/2,x+imgWidth,y+imgWidth/2);
mCanvas.drawBitmap(bitmap,null,rect,null);
}
/**
* 绘制每个盘块的文本(弧形)
* @param tmpAngle
* @param sweepAngle
* @param mStr
*/
private void drawText(float tmpAngle, float sweepAngle, String mStr) {
Path path=new Path();
path.addArc(mRange,tmpAngle,sweepAngle);
float textWidth=mTextPaint.measureText(mStr);
//水平偏移量 mRadius是直径
int hOffset= (int) (mRadius*Math.PI/mItemCount/2-textWidth/2);
//垂直偏移量
int vOffset=mRadius/2/6;
//第三个参数:水平偏移量
//第四个参数:垂直偏移量
mCanvas.drawTextOnPath(mStr,path,hOffset,vOffset,mTextPaint);
}
/**
* 绘制背景
*/
private void drawBg() {
mCanvas.drawColor(0xFFFFFFFF);
mCanvas.drawBitmap(mBigBitmap,null,
new Rect(mPadding/2,mPadding/2,getMeasuredWidth()-mPadding/2,
getMeasuredHeight()-mPadding/2),null);
}
}