Android倒计时控件 Splash界面5秒自动跳转

现在很多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, attrs, defStyleAttr);
  initialize(context, attrs);
 }

 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 public CircleProgressbar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  super(context, attrs, 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(), bounds.centerY(), 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, 0, 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • android特卖列表倒计时卡顿问题的解决方法

    在Android的开发中,我们经常遇见倒计时的操作,通常使用Timer和Handler共同操作来完成.当然也可以使用Android系统控件CountDownTimer,这里我们封装成一个控件,也方便大家的使用. 首先上一张效果图吧: 说一下造成卡顿的原因,由于滑动的时候,adapter的getView频繁的创建和销毁,就会出现卡顿和数据错位问题,那么我们每一个item的倒计时就需要单独维护,这里我用的Handler与timer及TimerTask结合的方法,我们知道TimerTask运行在自己子

  • Android实现计时与倒计时的常用方法小结

    本文实例总结了Android实现计时与倒计时的常用方法.分享给大家供大家参考,具体如下: 方法一 Timer与TimerTask(Java实现) public class timerTask extends Activity{ private int recLen = 11; private TextView txtView; Timer timer = new Timer(); public void onCreate(Bundle savedInstanceState){ super.onC

  • Android自定义圆环倒计时控件

    本文实例为大家分享了Android自定义圆环倒计时控件的具体代码,供大家参考,具体内容如下 先来一张最终效果图: 主要思路: 在画渐变色圆环的时候,设置一个属性动画,根据属性动画的执行时长,来作为倒计时的时长.监听属性动画的进度,来达到 倒计时的目的. 二话不说,直接贴代码.具体实现思路都在注释上. 自定义属性: <declare-styleable name="CountDownProgressBar"> <attr name="countDown_cir

  • android自定义倒计时控件示例

    自定义TextView控件TimeTextView代码: 复制代码 代码如下: import android.content.Context;import android.content.res.TypedArray;import android.graphics.Paint;import android.text.Html;import android.util.AttributeSet;import android.widget.TextView; import com.new0315.R;

  • Android仿Keep运动休息倒计时圆形控件

    仿Keep运动休息倒计时控件,供大家参考,具体内容如下 源码 控件本身非常非常简单,唯一难点在于倒计时期间动态增减时长,如果说动态增减时长是瞬间完成的,倒也没什么难度,但是如果是需要花一定时间做动画的话(见效果图),考虑的逻辑就变多了,这也是我写这个的目的,对应源码中就是plus这个方法.地址: KeepCountdownView 效果 使用方法 xml: <com.KeepCountdownView.KeepCountdownView android:id="@+id/keep1&quo

  • android实现倒计时功能代码

    效果图,每隔1秒,变换一下时间  xml: 复制代码 代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="mat

  • Android倒计时的开始与停止 剩余时分秒的展示

    本文实例为大家分享了Android倒计时的开始与停止,剩余时分秒的展示效果,供大家参考,具体内容如下 1.声明开启倒计时相关方法 Handler handler = new Handler(); Runnable update_thread = new Runnable() { @Override public void run() { leftTime--; LogUtil.e("leftTime="+leftTime); if (leftTime > 0) { //倒计时效果

  • android实现条目倒计时功能

    网上对于这样的功能已经是泛滥成河了,但是最近遇到这样的一个需求,还是要值得我们学习一下,并将他记录下来. 布局文件: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:

  • Android实现加载广告图片和倒计时的开屏布局

    这是一个android开屏布局的实例,可以用于加载广告图片和倒计时的布局.程序中设置的LayoutParams,划分额外空间比例为6分之5,具体权重比例可根据用户自己需求来自定义,异步加载广告图片,相关的Android代码. 具体实现代码如下: package cn.waps.extend; import android.app.Activity; import android.content.Context; import android.content.res.Configuration;

  • Android利用RecyclerView实现列表倒计时效果

    最近面试时,面试官问了一个列表倒计时效果如何实现,现在记录一下. 运行效果图 实现思路 实现方法主要有两个: 1.为每个开始倒计时的item启动一个定时器,再做更新item处理: 2.只启动一个定时器,然后遍历数据,再做再做更新item处理. 经过思考,包括性能.实现等方面,决定使用第2种方式实现. 实现过程 数据实体 /** * 总共的倒计时的时间(结束时间-开始时间),单位:毫秒 * 例: 2019-02-23 11:00:30 与 2019-02-23 11:00:00 之间的相差的毫秒数

随机推荐