android自定义抽奖,Android 自定义View 抽奖大转盘(2)

这是转盘的第二个版本,添加了外围的圆圈

第一个demo在这儿可以找到

镇楼图

feec4c35a3cc

S171011-11370203.jpg

项目的本来来源来自于

添加外围的圆圈,主要由两部分组成

1.画小圆。2.圆盘位置的定位

圆盘位置如下

@Override

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

super.onLayout(changed, left, top, right, bottom);

//就是让两个view居中,没有其他的操作

int centerX = (right - left)/2;

int centerY = (bottom - top)/2;

boolean panReady = false;

for(int i=0;i

View child = getChildAt(i);

if(child instanceof RotateView){

rotatePan = (RotateView) child;

int panWidth = child.getWidth();

int panHeight = child.getHeight();

child.layout(centerX - panWidth/2 , centerY - panHeight/2,centerX + panWidth/2, centerY + panHeight/2);

panReady = true;

}else if(child instanceof ImageView){

// if(TextUtils.equals((String) child.getTag(),START_BTN_TAG)){

startBtn = (ImageView) child;

int btnWidth = child.getWidth();

int btnHeight = child.getHeight();

child.layout(centerX - btnWidth/2 , centerY - btnHeight/2 , centerX + btnWidth/2, centerY + btnHeight/2 );

// }

}

}

}

这里一看就能明白,两个View,居中显示,在onLayout方法里面

下面是画圆的方法

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

this.canvas = canvas;

final int paddingLeft = getPaddingLeft();

final int paddingRight = getPaddingRight();

final int paddingTop = getPaddingTop();

final int paddingBottom = getPaddingBottom();

int width = getWidth() - paddingLeft - paddingRight;

int height = getHeight() - paddingTop - paddingBottom;

int MinValue = Math.min(width,height);

radius = MinValue /2;

CircleX = getWidth() /2;

CircleY = getHeight() /2;

//画背景

canvas.drawCircle(CircleX,CircleY,radius,backgroundPaint);

//画圆圈,主要功能也就在这里

drawSmallCircle(isYellow);

}

private void drawSmallCircle(boolean FirstYellow) {

//位置要在内部,每次的半径相应的变短一点

int pointDistance = radius - Util.dip2px(context,10);

//每次增加20度,也就是能够得到18个点

for(int i=0;i<=360;i+=20){

//每次获取到圆心的位置,由i控制位置

int x = (int) (pointDistance * Math.sin(Util.change(i))) + CircleX;

int y = (int) (pointDistance * Math.cos(Util.change(i))) + CircleY;

//两个不同颜色圆

if(FirstYellow)

canvas.drawCircle(x,y,Util.dip2px(context,6),yellowPaint);

else

canvas.drawCircle(x,y,Util.dip2px(context,6),whitePaint);

FirstYellow = !FirstYellow;

}

}

如上所示,在onDraw方法,画出了圆,根据不同的角度变化,获取到了小圆的圆心位置,画出圆

其中View之间的间距是由两个自定义View的位置差值来确定外围小圆的圆环宽度的。

感觉这里可以用一个自定义View来实现,之后会选择用一个自定义View来实现整个抽奖的大转盘。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值