基本信息
源码名称:android 转盘抽奖 示例源码
源码大小:2.79M
文件格式:.zip
开发语言:Java
更新时间:2017-12-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍



package com.hr.nipuream.luckpan.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.text.TextUtils; import android.util.AttributeSet; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; import com.hr.nipuream.luckpan.util.Logger; import com.hr.nipuream.luckpan.util.Util; public class LuckPanLayout extends RelativeLayout { private Context context; private Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private Paint whitePaint = new Paint(Paint.ANTI_ALIAS_FLAG); private Paint yellowPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private int radius; private int CircleX,CircleY; private Canvas canvas; private boolean isYellow = false; private int delayTime = 500; private RotatePan rotatePan; private ImageView startBtn; private int screenWidth,screeHeight; private int MinValue; /**  * LuckPan 中间对应的Button必须设置tag为 startbtn.  */  private static final String START_BTN_TAG = "startbtn"; public static final int DEFAULT_TIME_PERIOD = 500; public LuckPanLayout(Context context) { this(context,null);
    } public LuckPanLayout(Context context, AttributeSet attrs) { this(context, attrs,0);
    } public LuckPanLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context;
        Logger.setDebug(true); backgroundPaint.setColor(Color.rgb(255,92,93)); whitePaint.setColor(Color.WHITE); yellowPaint.setColor(Color.YELLOW); screeHeight = getResources().getDisplayMetrics().heightPixels; screenWidth = getResources().getDisplayMetrics().widthPixels;
        startLuckLight();
    } @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); MinValue = Math.min(screenWidth,screeHeight); MinValue -= Util.dip2px(context,10)*2;
        Logger.getLogger().d("screenWidth = " screenWidth  "screenHeight = " screeHeight  "MinValue = " MinValue);
        setMeasuredDimension(MinValue,MinValue);
    } @Override  protected void onDraw(Canvas canvas) { super.onDraw(canvas); this.canvas = canvas; final int paddingLeft = getPaddingLeft(); final int paddingRight = getPaddingRight(); final int paddingTop = getPaddingTop(); final int paddingBottom = getPaddingBottom(); int width = getWidth() - paddingLeft - paddingRight; int height = getHeight() - paddingTop - paddingBottom; int MinValue = Math.min(width,height); radius = MinValue /2; CircleX = getWidth() /2; CircleY = getHeight() /2;
        canvas.drawCircle(CircleX,CircleY,radius,backgroundPaint);
        drawSmallCircle(isYellow);
    } @Override  protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); int centerX = (right - left)/2; int centerY = (bottom - top)/2; boolean panReady = false; for(int i=0;i<getChildCount();i  ){
            View child = getChildAt(i); if(child instanceof RotatePan){ rotatePan = (RotatePan) child; int panWidth = child.getWidth(); int panHeight = child.getHeight();
                 child.layout(centerX - panWidth/2 , centerY - panHeight/2,centerX   panWidth/2 , centerY   panHeight/2);
                 panReady = true;
            }else if(child instanceof ImageView){ if(TextUtils.equals((String) child.getTag(),START_BTN_TAG)){ startBtn = (ImageView) child; int btnWidth = child.getWidth(); int btnHeight = child.getHeight();
                    child.layout(centerX - btnWidth/2 , centerY - btnHeight/2 , centerX   btnWidth/2, centerY   btnHeight/2 );
                }
            }
        } if(!panReady) throw new RuntimeException("Have you add RotatePan in LuckPanLayout element ?");
    } private void drawSmallCircle(boolean FirstYellow){ int pointDistance = radius - Util.dip2px(context,10); for(int i=0;i<=360;i =20){ int x = (int) (pointDistance * Math.sin(Util.change(i))) CircleX; int y = (int) (pointDistance * Math.cos(Util.change(i))) CircleY; if(FirstYellow) canvas.drawCircle(x,y,Util.dip2px(context,4),yellowPaint); else  canvas.drawCircle(x,y,Util.dip2px(context,4),whitePaint);
            FirstYellow = !FirstYellow;
        }
    } /**  * 开始旋转  * @param pos 转到指定的转盘,-1 则随机  * @param delayTime 外围灯光闪烁的间隔时间  */  public void rotate(int pos,int delayTime){ rotatePan.startRotate(pos);
        setDelayTime(delayTime);
        setStartBtnEnable(false);
    } protected void setStartBtnEnable(boolean enable){ if(startBtn != null) startBtn.setEnabled(enable); else throw new RuntimeException("Have you add start button in LuckPanLayout element ?");
    } private void startLuckLight(){
        postDelayed(new Runnable() { @Override  public void run() { isYellow = !isYellow;
                invalidate();
                postDelayed(this,delayTime);
            }
        },delayTime);
    } protected void setDelayTime(int delayTime){ this.delayTime = delayTime;
    } public interface AnimationEndListener{ void endAnimation(int position);
    } private AnimationEndListener l; public void setAnimationEndListener(AnimationEndListener l){ this.l = l;
    } public AnimationEndListener getAnimationEndListener(){ return l;
    }
}