博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
绘制一个简单的实现接口盘
阅读量:5758 次
发布时间:2019-06-18

本文共 4436 字,大约阅读时间需要 14 分钟。

闲来无事,做一个简单的抽奖转盘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;	}}

版权声明:本文博客原创文章,博客,未经同意,不得转载(联系信息:QQ312037487 邮箱:andywuchuanlong@sina.cn)。

你可能感兴趣的文章
MachineLearning-Sklearn——环境搭建
查看>>
node学习之路(二)—— Node.js 连接 MongoDB
查看>>
Goroutine是如何工作的?
查看>>
《深入理解java虚拟机》学习笔记系列——垃圾收集器&内存分配策略
查看>>
用grunt搭建自动化的web前端开发环境-完整教程
查看>>
研究人员发现:基于文本的AI模型容易受到改述攻击
查看>>
TriggerMesh开源用于多云环境的Knative Event Sources
查看>>
GitLab联合DigitalOcean为开源社区提供GitLab CI免费托管
查看>>
通过XAML Islands使Windows桌面应用程序现代化
查看>>
区块链现状:从谨慎和批判性思维看待它(第二部分)
查看>>
苹果公司透露Siri新发音引擎的内部原理
查看>>
GCM 3.0采用类似方式向Android、iOS和Chrome发送消息
查看>>
如何成为一家敏捷银行
查看>>
MongoDB主动撤回SSPL的开源许可申请
查看>>
Oracle在JavaOne上宣布Java EE 8将会延期至2017年底
查看>>
使用Prometheus监控Cloudflare的全球网络
查看>>
Javascript 深入浅出原型
查看>>
VS 2019要来了,是时候了解一下C# 8.0新功能
查看>>
简单之极,搭建属于自己的Data Mining环境(Spark版本)
查看>>
Web Storage--HTML5本地存储
查看>>