文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android实现抽奖转盘实例代码

2022-06-06 10:22

关注

本文详述了android抽奖程序的实现方法,程序为一个抽奖大转盘代码,里面定义了很多图形方法和动画。

实现主要功能的SlyderView.java源代码如下:


import android.app.Activity;
import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.EmbossMaskFilter;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff.Mode;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
public class SlyderView extends View{
  public SlyderView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
  }
  public SlyderView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
  }
  public SlyderView(Context context) {
    super(context);
    init(context);
  }
  
  private int screenW;
  
  private int screenH;
  
  private int [] drgrees = {20,50,40,90,70,40,50};
  
  private String [] strs = {"level1","level2","level3","level4","level5","level6","level7"};
  
  private int [] colos = new int[] { 0xfed9c960, 0xfe57c8c8, 0xfe9fe558, 0xfef6b000, 0xfef46212, 0xfecf2911, 0xfe9d3011 };
  
  private Paint paint;
  
  private float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics());
  
  private int textcolor = Color.WHITE;
  
  private float radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 100, getResources().getDisplayMetrics());
  
  private float textdis = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 80, getResources().getDisplayMetrics());
  
  private float roketSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics());
  private float initDegress = 0;
  
  private float centerX;
  
  private float centerY;
  
  private MaskFilter filter = new EmbossMaskFilter(new float[] { 1, 1, 1 },0.4f, 6, 3.5f);
  private MaskFilter outerFilter = new BlurMaskFilter(10, BlurMaskFilter.Blur.OUTER);
  private MaskFilter innerFilter = new BlurMaskFilter(10, BlurMaskFilter.Blur.INNER);
  @SuppressWarnings("deprecation")
  private void init(Context context){
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Style.FILL);
    paint.setColor(Color.WHITE);
    screenW = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
    screenH = ((Activity)context).getWindowManager().getDefaultDisplay().getHeight();
    int[] colores = new int[3];
    colores[0] = Color.rgb(0xfF, 0x99, 0x00);
    colores[1] = Color.rgb(0xff, 0xff, 0x00);
    colores[2] = Color.rgb(0xff, 0x99, 0x00);
    float[] positions = new float[3];
    positions[0] = 0.0f;
    positions[1] = 0.5f;
    positions[2] = 1.0f;
    gradient = new RadialGradient(centerX, centerY, radius/5, colores, positions, TileMode.CLAMP);
  }
  
  private Path path = new Path();
  
  private RectF oval;
  
  private ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(new float[]{
      1,0,0,0,0,
      0,1,0,0,0,
      0,0,1,0,0,
      0,0,0,-1,255
  });
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    centerX = screenW/2;
    centerY = screenH/2;
    oval = new RectF(centerX-radius,centerY-radius,centerX+radius,centerY+radius);
    float start = 0;
    paint.setColor(Color.rgb(0xdd, 0xdd, 0xdd));
    paint.setAlpha(127);
    canvas.drawCircle(centerX, centerY, radius+10, paint);
    paint.setAlpha(255);
    //画扇形
    paint.setAntiAlias(true);
    for(int i=0;i<drgrees.length;i++){
      float sweepAngle = drgrees[i];
      float startAngle = start;
      paint.setColor(colos[i%colos.length]);
      canvas.drawArc(oval, startAngle, sweepAngle, true, paint);
      start += drgrees[i];
    }
    //画文字
    paint.setColor(textcolor);
    paint.setAntiAlias(true);
    paint.setTextSize(textSize);
    paint.setTextAlign(Paint.Align.RIGHT);
    start = 0;
    for(int i=0;i<drgrees.length;i++){
      canvas.save();
      canvas.rotate(start+drgrees[i]/2, centerX, centerY);
      canvas.drawText(strs[i], centerX+textdis, centerY, paint);
      canvas.restore();
      start += drgrees[i];
    }
    int saveCount = canvas.save();
    //画外层立体效果
    paint.setColorFilter(colorFilter);
    canvas.saveLayer(oval,paint,Canvas.ALL_SAVE_FLAG);
    paint.setColorFilter(null);
    canvas.drawARGB(255, 0, 0, 0);
    paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
    canvas.drawCircle(centerX, centerY, radius, paint);
    paint.setXfermode(null);
    paint.setMaskFilter(innerFilter);
    paint.setColor(Color.argb(0xff, 0, 0, 0));
    canvas.drawCircle(centerX, centerY, radius, paint);
    paint.setMaskFilter(null);
    canvas.restoreToCount(saveCount);
    //画内圆和内园效果
    canvas.save();
    paint.setColor(Color.argb(0xff, 0, 0, 0));
    paint.setAntiAlias(true);
    paint.setMaskFilter(outerFilter);
    canvas.rotate(initDegress, centerX, centerY);
    canvas.drawCircle(centerX, centerY, radius/3, paint);
    paint.setMaskFilter(null);
    paint.setColor(Color.WHITE);
    canvas.drawCircle(centerX, centerY, radius/3, paint);
    //画三角型叠加当箭头
    path.moveTo(centerX-radius/3, centerY);
    path.lineTo(centerX, centerY-radius/3-roketSize);
    path.lineTo(centerX+radius/3, centerY);
    path.close();
    canvas.drawPath(path, paint);
    canvas.restore();
    paint.setMaskFilter(filter);
    paint.setColor(Color.GREEN);
    paint.setShader(gradient);
    canvas.drawCircle(centerX, centerY, radius/5, paint);
    paint.setMaskFilter(null);
    paint.setShader(null);
    //重绘调整三角的指向达到滚动的效果,现实项目中可不能这样用的,效率太低下了,拆分View用动画完成滚动才是王道
    if(isRunning){
      if(initDegress>=360){
        initDegress = 0;
      }
      initDegress +=4;
      invalidate();
    }
    if(isStoping){
      if(initDegress<=360){
        initDegress+=4;
        invalidate();
      }else{
        if(initDegress-360<stop_degress){
          initDegress+=2;
          invalidate();
        }
      }
    }
  }
  private boolean isRunning = false;
  private boolean isStoping = false;
  private int stop_degress =90;
  
  private RadialGradient gradient;
  public void play(){
    isRunning = true;
    invalidate();
  }
  public void stop(int count){
    for(int i =0;i<=count;i++){
      if(i == count){
        stop_degress +=drgrees[i]/2;
      }else{
        stop_degress +=drgrees[i];
      }
    }
    isStoping = true;
    isRunning = false;
    invalidate();
  }
}
您可能感兴趣的文章:Android自定义view制作抽奖转盘Android自定义View实现抽奖转盘Android自定义View实现QQ运动积分转盘抽奖功能Android使用surfaceView自定义抽奖大转盘Android中利用SurfaceView制作抽奖转盘的全流程攻略基于Android实现转盘按钮代码Android实现可点击的幸运大转盘


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯