Android自定义SurfaceView实现抽奖转盘实例源代码
程序员文章站
2021-11-22 11:15:24
...
分享一款自定义Surfaceview实现幸运大抽奖的Demo、界面比较简单、代码也不复杂、可以根据自己项目的需求修改界面、下面是Demo的截图
绘制盘块图片代码
private void drawIcon(float tmpAngle, Bitmap bitmap) { //设置图片的宽度为直径的1/8 int imgWidth = mRadius/8; float angle = (float) ((tmpAngle 360/itemCount/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/2, y imgWidth/2); canvas.drawBitmap(bitmap, null, rect,null); }
绘制文本代码
private void drawText(float tmpAngle, float sweepAngle, String string) { Path path = new Path(); path.addArc(mRange, tmpAngle, sweepAngle); // 利用水平偏移量让文字居中 float textWidth = mTextPanit.measureText(string); int hOffset = (int) (mRadius*Math.PI/itemCount/2 - textWidth/2); //垂直偏移量 int vOffset = mRadius/2/6; canvas.drawTextOnPath(string, path, hOffset, vOffset, mTextPanit); }
public void luckyStart(int index){ //计算每一项的角度 float angle = 360/itemCount; //计算每一项中奖范围(当前index) //1-->150~210 //0-->210~270 float from = 270-(index 1)*angle; float end = from angle; //设置停下来需要旋转的距离 float targetFrom = 4*360 from; float targetEnd = 4*360 end; float v1= (float) ((-1 Math.sqrt(1 8*targetFrom))/2); float v2= (float) ((-1 Math.sqrt(1 8*targetEnd))/2); mSpeed = v1 Math.random()*(v2-v1); isShouldEnd = false; }
android抽奖转盘源代码下载链接: android抽奖转盘代码 密码: khwt