在這里將頭兩篇做了一個整合,兩個自定義View變成了一個自定義View
Android 自定義View 抽獎大轉盤(1)
Android 自定義View 抽獎大轉盤(2)
項目github鏈接 https://github.com/yukunkun/RotateView
鎮樓圖
S771011-14425058.jpg
主要是將外圍的圓環在內部設置,增加了轉盤中間的點擊事件,部分修改如下
//大圓
canvas.drawCircle(MinValue/2,MinValue/2,radius,backgroundPaint);
圓的繪制
//繪制中間的圖
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.node);
mRect = new Rect(MinValue/2-bitmap.getWidth()/2,MinValue/2-bitmap.getHeight()/2,MinValue/2+bitmap.getWidth()/2,MinValue/2+bitmap.getHeight()/2);
canvas.drawBitmap(bitmap,null, mRect, null);
drawSmallCircle(canvas,isYellow,MinValue);
小圓
private void drawSmallCircle(Canvas canvas, boolean FirstYellow, int minValue) {
//位置要在內部,每次的半徑相應的變短一點
int pointDistance = radius - Util.dip2px(context,9);
//每次增加20度,也就是能夠得到18個點
for(int i=0;i<=360;i+=20){
//每次獲取到圓心的位置,由i控制位置
int x = (int) (pointDistance * Math.sin(Util.change(i))) + minValue/2;
int y = (int) (pointDistance * Math.cos(Util.change(i))) + minValue/2;
//兩個不同顏色圓
if(FirstYellow)
canvas.drawCircle(x,y,Util.dip2px(context,4),yellowPaint);
else
canvas.drawCircle(x,y,Util.dip2px(context,4),whitePaint);
FirstYellow = !FirstYellow;
}
}
這些方法被抽離出來了,圓盤的內圓半徑有變化,每個圖片的定位有點變化
初始化半徑時變小了
//內邊距
final int paddingLeft = getPaddingLeft()+Util.dip2px(context,ringLength);
final int paddingRight = getPaddingRight()-Util.dip2px(context,ringLength);
final int paddingTop = getPaddingTop()+Util.dip2px(context,ringLength);
final int paddingBottom = getPaddingBottom()-Util.dip2px(context,ringLength);
初始化圖片的中心位置,有點偏移,否者位置不對,有點遮擋文字
//確定圖片在圓弧中 中心點的位置
float x = (float) (xx + (mRadius /2 + mRadius/12-Util.dip2px(context,ringLength))* Math.cos(angle));
float y = (float) (yy + (mRadius /2 +mRadius/12-Util.dip2px(context,ringLength)) * Math.sin(angle));
下面是點擊中間的圖片的觸摸事件
/**
* 點擊事件,點擊中心就開始動畫
* @param event
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
float x = event.getX();
float y = event.getY();
//點擊了中間位置
if(x>mRect.left&&x<mRect.right&&y>mRect.top&&y<mRect.bottom){
startAnimation(pos);
}
break;
case MotionEvent.ACTION_MOVE:
break;
}
return super.onTouchEvent(event);
}