现在很多app的首页都有一个倒计时控件,比如说3秒或者5秒自动跳转界面,或者点击控件直接跳过
首先,自定义控件CircleProgressbar(参考网上资料)
package com.zhoujian.mykeep.vIEw;import androID.annotation.TargetAPI;import androID.content.Context;import androID.content.res.colorStateList;import androID.content.res.TypedArray;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Paint;import androID.graphics.Rect;import androID.graphics.RectF;import androID.os.Build;import androID.support.annotation.colorInt;import androID.util.AttributeSet;import androID.Widget.TextVIEw;import com.zhoujian.mykeep.R;public class CircleProgressbar extends TextVIEw{ //外部轮廓的颜色 private int outlinecolor = color.BLACK; //外部轮廓的宽度 private int outlinewidth = 2; //内部圆的颜色 private colorStateList inCirclecolors = colorStateList.valueOf(color.transparent); //中心圆的颜色 private int circlecolor; //进度条的颜色 private int progresslinecolor = color.BLUE; //进度条的宽度 private int progresslinewidth = 8; //画笔 private Paint mPaint = new Paint(); //进度条的矩形区域 private RectF marcRect = new RectF(); //进度 private int progress = 100; //进度条类型 private Progresstype mProgresstype = Progresstype.COUNT_BACK; //进度倒计时时间 private long timeMillis = 3000; //VIEw的显示区域。 final Rect bounds = new Rect(); //进度条通知。 private OnCountdownProgressListener mCountdownProgressListener; private int ListenerWhat = 0; public CircleProgressbar(Context context) { this(context,null); } public CircleProgressbar(Context context,AttributeSet attrs) { this(context,attrs,0); } public CircleProgressbar(Context context,AttributeSet attrs,int defStyleAttr) { super(context,defStyleAttr); initialize(context,attrs); } @TargetAPI(Build.VERSION_CODES.LolliPOP) public CircleProgressbar(Context context,int defStyleAttr,int defStyleRes) { super(context,defStyleAttr,defStyleRes); initialize(context,attrs); } private voID initialize(Context context,AttributeSet attributeSet) { mPaint.setAntiAlias(true); TypedArray typedArray = context.obtainStyledAttributes(attributeSet,R.styleable.CircleProgressbar); if (typedArray.hasValue(R.styleable.CircleProgressbar_in_circle_color)) inCirclecolors = typedArray.getcolorStateList(R.styleable.CircleProgressbar_in_circle_color); else inCirclecolors = colorStateList.valueOf(color.transparent); circlecolor = inCirclecolors.getcolorForState(getDrawableState(),color.transparent); typedArray.recycle(); } public voID setoutlinecolor(@colorInt int outlinecolor) { this.outlinecolor = outlinecolor; invalIDate(); } public voID setoutlinewidth(@colorInt int outlinewidth) { this.outlinewidth = outlinewidth; invalIDate(); } public voID setInCirclecolor(@colorInt int inCirclecolor) { this.inCirclecolors = colorStateList.valueOf(inCirclecolor); invalIDate(); } private voID valIDateCirclecolor() { int circlecolorTemp = inCirclecolors.getcolorForState(getDrawableState(),color.transparent); if (circlecolor != circlecolorTemp) { circlecolor = circlecolorTemp; invalIDate(); } } public voID setProgresscolor(@colorInt int progresslinecolor) { this.progresslinecolor = progresslinecolor; invalIDate(); } public voID setProgresslinewidth(int progresslinewidth) { this.progresslinewidth = progresslinewidth; invalIDate(); } public voID setProgress(int progress) { this.progress = valIDateProgress(progress); invalIDate(); } private int valIDateProgress(int progress) { if (progress > 100) progress = 100; else if (progress < 0) progress = 0; return progress; } public int getProgress() { return progress; } public voID setTimeMillis(long timeMillis) { this.timeMillis = timeMillis; invalIDate(); } public long getTimeMillis() { return this.timeMillis; } public voID setProgresstype(Progresstype progresstype) { this.mProgresstype = progresstype; resetProgress(); invalIDate(); } private voID resetProgress() { switch (mProgresstype) { case COUNT: progress = 0; break; case COUNT_BACK: progress = 100; break; } } public Progresstype getProgresstype() { return mProgresstype; } public voID setCountdownProgressListener(int what,OnCountdownProgressListener mCountdownProgressListener) { this.ListenerWhat = what; this.mCountdownProgressListener = mCountdownProgressListener; } public voID start() { stop(); post(progressChangeTask); } public voID reStart() { resetProgress(); start(); } public voID stop() { removeCallbacks(progressChangeTask); } @OverrIDe protected voID onDraw(Canvas canvas) { //获取vIEw的边界 getDrawingRect(bounds); int size = bounds.height() > bounds.wIDth() ? bounds.wIDth() : bounds.height(); float outerRadius = size / 2; //画内部背景 int circlecolor = inCirclecolors.getcolorForState(getDrawableState(),0); mPaint.setStyle(Paint.Style.FILL); mPaint.setcolor(circlecolor); canvas.drawCircle(bounds.centerX(),bounds.centerY(),outerRadius - outlinewidth,mPaint); //画边框圆 mPaint.setStyle(Paint.Style.stroke); mPaint.setstrokeWIDth(outlinewidth); mPaint.setcolor(outlinecolor); canvas.drawCircle(bounds.centerX(),outerRadius - outlinewidth / 2,mPaint); //画字 Paint paint = getPaint(); paint.setcolor(getCurrentTextcolor()); paint.setAntiAlias(true); paint.setTextAlign(Paint.Align.CENTER); float textY = bounds.centerY() - (paint.descent() + paint.ascent()) / 2; canvas.drawText(getText().toString(),bounds.centerX(),textY,paint); //画进度条 mPaint.setcolor(progresslinecolor); mPaint.setStyle(Paint.Style.stroke); mPaint.setstrokeWIDth(progresslinewidth); mPaint.setstrokeCap(Paint.Cap.ROUND); int deleteWIDth = progresslinewidth + outlinewidth; marcRect.set(bounds.left + deleteWIDth / 2,bounds.top + deleteWIDth / 2,bounds.right - deleteWIDth / 2,bounds.bottom - deleteWIDth / 2); canvas.drawArc(marcRect,360 * progress / 100,false,mPaint); } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { super.onMeasure(wIDthMeasureSpec,heightmeasureSpec); int linewidth = 4 * (outlinewidth + progresslinewidth); int wIDth = getMeasureDWIDth(); int height = getMeasuredHeight(); int size = (wIDth > height ? wIDth : height) + linewidth; setMeasuredDimension(size,size); } @OverrIDe protected voID drawableStateChanged() { super.drawableStateChanged(); valIDateCirclecolor(); } private Runnable progressChangeTask = new Runnable() { @OverrIDe public voID run() { removeCallbacks(this); switch (mProgresstype) { case COUNT: progress += 1; break; case COUNT_BACK: progress -= 1; break; } if (progress >= 0 && progress <= 100) { if (mCountdownProgressListener != null) mCountdownProgressListener.onProgress(ListenerWhat,progress); invalIDate(); postDelayed(progressChangeTask,timeMillis / 100); } else progress = valIDateProgress(progress); } }; public enum Progresstype { /** * 顺数进度条,从0-100; */ COUNT,/** * 倒数进度条,从100-0; */ COUNT_BACK; } public interface OnCountdownProgressListener { voID onProgress(int what,int progress); }}
activity_splash.xml
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="@mipmap/splash"> <ScrollVIEw androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <com.zhoujian.mykeep.vIEw.CircleProgressbar androID:ID="@+ID/tv_red_skip" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="right" androID:layout_marginRight="15dp" androID:layout_margintop="15dp" androID:text="跳过" androID:textcolor="#ffffff" androID:textSize="12sp"/> </ScrollVIEw></relativeLayout>
SplashActivity.java
package com.zhoujian.mykeep.activity;import androID.content.Intent;import androID.graphics.color;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.util.Log;import androID.vIEw.VIEw;import com.zhoujian.mykeep.R;import com.zhoujian.mykeep.vIEw.CircleProgressbar;public class SplashActivity extends AppCompatActivity{ private static final String TAG ="SplashActivity"; private CircleProgressbar mCircleProgressbar; private boolean isClick = false; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_splash); mCircleProgressbar = (CircleProgressbar) findVIEwByID(R.ID.tv_red_skip); mCircleProgressbar.setoutlinecolor(color.transparent); mCircleProgressbar.setInCirclecolor(color.parsecolor("#505559")); mCircleProgressbar.setProgresscolor(color.parsecolor("#1BB079")); mCircleProgressbar.setProgresslinewidth(5); mCircleProgressbar.setProgresstype(CircleProgressbar.Progresstype.COUNT); mCircleProgressbar.setTimeMillis(5000); mCircleProgressbar.reStart(); mCircleProgressbar.setCountdownProgressListener(1,progressListener); mCircleProgressbar.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { isClick = true; Intent intent = new Intent(SplashActivity.this,MainActivity.class); startActivity(intent); finish(); } }); } private CircleProgressbar.OnCountdownProgressListener progressListener = new CircleProgressbar.OnCountdownProgressListener() { @OverrIDe public voID onProgress(int what,int progress) { if(what==1 && progress==100 && !isClick) { Intent intent = new Intent(SplashActivity.this,MainActivity.class); startActivity(intent); finish(); Log.e(TAG,"onProgress: =="+progress ); } } };}
显示效果:
源码下载:MyKeep
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的Android倒计时控件 Splash界面5秒自动跳转全部内容,希望文章能够帮你解决Android倒计时控件 Splash界面5秒自动跳转所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)