实例介绍
【实例简介】
【实例截图】


【实例截图】



【核心代码】
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; } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论