Android簡單實現(xiàn)圓盤抽獎界面
更新時間:2016年01月08日 16:40:00 作者:andywuchuanlong
這篇文章主要介紹了Android簡單實現(xiàn)圓盤抽獎界面的相關(guān)資料,需要的朋友可以參考下
閑來無事,做了一個簡單的抽獎轉(zhuǎn)盤的ui實現(xiàn),供大家參考

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); //設(shè)置空心
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;
}
}
希望本文所述對大家學(xué)習(xí)Android程序設(shè)計有所幫助。
您可能感興趣的文章:
相關(guān)文章
Golang+Android基于HttpURLConnection實現(xiàn)的文件上傳功能示例
這篇文章主要介紹了Golang+Android基于HttpURLConnection實現(xiàn)的文件上傳功能,結(jié)合具體實例形式分析了Android基于HttpURLConnection的客戶端結(jié)合Go語言服務(wù)器端實現(xiàn)文件上傳功能的操作技巧,需要的朋友可以參考下2017-03-03
Android進(jìn)度條ProgressBar的實現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android進(jìn)度條ProgressBar的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
Flutter實現(xiàn)漸變弧形進(jìn)度條的示例詳解
在Flutter開發(fā)中,構(gòu)建一個具有視覺吸引力的、反映進(jìn)度的圓形弧形進(jìn)度條是一個常見需求,本文將詳細(xì)介紹如何使用Flutter和Dart語言實現(xiàn)這一功能,需要的可以參考下2023-12-12
Android自定義TextView實現(xiàn)文字圖片居中顯示的方法
下面小編就為大家分享一篇Android自定義TextView實現(xiàn)文字圖片居中顯示的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
超詳細(xì)的Android開發(fā)調(diào)試工具ADB命令及安裝介紹
這篇文章主要介紹了Android調(diào)試工具ADB安裝使用技巧,非常詳細(xì),有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家早日升值加薪2021-09-09

