Android简单实现圆盘抽奖界面

所属分类: 软件编程 / Android 阅读数: 1479
收藏 0 赞 0 分享

闲来无事,做了一个简单的抽奖转盘的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程序设计有所帮助。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多