Android简单实现圆盘抽奖界面
程序员文章站
2023-12-21 14:01:58
闲来无事,做了一个简单的抽奖转盘的ui实现,供大家参考
package com.microchange.lucky;
import android....
闲来无事,做了一个简单的抽奖转盘的ui实现,供大家参考
package com.microchange.lucky; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.rectf; import android.util.attributeset; import android.util.log; import android.view.motionevent; import android.view.view; public class halfcircle extends view { private paint paint; private rectf oval; private float startangle; private float sweepspeed; private float sweepangle; boolean usecenter; int count;// 等份 @override protected void ondraw(canvas canvas) { setsweepangle(count); while (startangle <= 360) { if (startangle % (count*3) == 0) { paint.setcolor(color.blue); } else if (startangle % (count*2) == 0){ paint.setcolor(color.green); }else { paint.setcolor(color.red); } log.e(""+startangle, paint.getcolor()+""); canvas.drawarc(oval, startangle, sweepangle, usecenter, paint); startangle += count; } float centerx = oval.centerx(); float centery = oval.centery(); paint.setcolor(color.white); // paint.setstrokewidth(5); // paint.setstyle(paint.style.stroke); //设置空心 paint.setantialias(true); //消除锯齿 canvas.drawcircle(centerx, centery, 50, paint); string text = "奖"; paint.settextsize(20 * getcontext().getresources().getdisplaymetrics().density); float measuretext = paint.measuretext(text); float texty = paint.descent() - paint.ascent(); paint.setcolor(color.red); // canvas.drawline(0, centery, 480, centery, paint); // canvas.drawtext(text, centerx-(measuretext/2), centery, paint); canvas.drawtext(text, centerx-(measuretext/2), centery+(texty/4), paint); } private void init() { paint = new paint(); paint.setcolor(color.blue); paint.setantialias(true); paint.setstrokewidth(5); } @override public boolean ontouchevent(motionevent event) { return super.ontouchevent(event); } /** * @return the count */ public int getcount() { return count; } /** * @param count the count to set */ public void setcount(int count) { this.count = 360 / count; } public paint getpaint() { return paint; } public void setpaint(paint paint) { this.paint = paint; } public rectf getoval() { return oval; } public void setoval(rectf oval) { this.oval = oval; } public float getstartangle() { return startangle; } public void setstartangle(float startangle) { this.startangle = startangle; } public float getsweepspeed() { return sweepspeed; } public void setsweepspeed(float sweepspeed) { this.sweepspeed = sweepspeed; } public float getsweepangle() { return sweepangle; } public void setsweepangle(float sweepangle) { this.sweepangle = sweepangle; } public boolean isusecenter() { return usecenter; } public void setusecenter(boolean usecenter) { this.usecenter = usecenter; } public halfcircle(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(); } public halfcircle(context context, attributeset attrs) { this(context, attrs, 0); } public halfcircle(context context) { this(context, null, 0); } }
package com.microchange.lucky; import android.app.activity; import android.graphics.rectf; import android.os.bundle; import android.view.animation.accelerateinterpolator; import android.view.animation.animation; import android.view.animation.decelerateinterpolator; import android.view.animation.interpolator; import android.view.animation.rotateanimation; public class mainactivity extends activity { rectf rect; int radius = 300; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); halfcircle circle = new halfcircle(getapplicationcontext()); circle.setoval(getrectf()); // circle.setstartangle(90); circle.setusecenter(true); circle.setcount(9); animation animation = new rotateanimation(0, 135*10, getrectf().centerx(), getrectf().centery()); animation.setduration(5000); animation.setinterpolator(new decelerateinterpolator()); // animation.setrepeatcount(-1); circle.setanimation(animation ); // animation.start(); setcontentview(circle); } public rectf getrectf(){ if (rect==null){ // getwindow().getdecorview().getwidth() int width = getresources().getdisplaymetrics().widthpixels; int height = getresources().getdisplaymetrics().heightpixels; int top = (height - radius)/2; int left = (width - radius)/2; rect = new rectf(left, top, left+radius, top+radius); } return rect; } }
希望本文所述对大家学习android程序设计有所帮助。