自定义圆形进度条 自定义倒计时进度条

自定义圆形进度条 自定义倒计时进度条

版权声明:转载必须注明本文转自严振杰的博客: http://blog.csdn.net/yanzhenjie1003

此控件源码已开源到Github:https://github.com/yanzhenjie/CircleTextProgressbar,欢迎Star。

欢迎加入我博客左侧的QQ交流群一起探讨。


效果预览

这里写图片描述

源代码传送门:https://github.com/yanzhenjie/CircleTextProgressbar

实现与原理

这个文字圆形的进度条我们在很多APP中看到过,比如APP欢迎页倒计时,下载文件倒计时等。

分析下原理,可能有的同学一看到这个自定义View就慌了,这个是不是要继承View啊,是不是要绘制啊之类的,答案是:是的。但是我们也不要担心,实现这个效果实在是so easy。下面就跟我一起来看看核心分析和代码吧。

原理分析

首先我们观察上图,需要几个部分组成:
1. 外面逐渐增加/减少的圆形进度条。
2. 圆形进度条中间的展示文字。
3. 圆形进度条外面包裹的圆。
4. 圆形进度条中间的填充色。
5. 字体颜色/填充颜色点击变色:ColorStateList类。

我们分析得出需要四个部分。一看有文字,那么第一个想到的自然是TextView啦,正好可以少做一个字体颜色的记录。中间的填充颜色(原型暂且不考虑)点击时变色,需要ColorStateList类来记录。剩下的进度条、轮廓圆和填充圆是需要我们绘制的。

我封装的CircleTextProgressbar特色

CircleTextProgressbar支持自动倒计时,自动减少进度,自动增加进度等。

如果需要自动走进度的话,设置完你自定义的属性后调用start()方法就可以自动倒计时了,如果想走完后再走一遍自动进度调用一下reStart()就OK了。

如果不想自动走进度,你可以通过setProgress()来像系统的progress一样修改进度值。

// 和系统普通进度条一样,0-100。
progressBar.setProgressType(CircleTextProgressbar.ProgressType.COUNT);

// 改变进度条。
progressBar.setProgressLineWidth(30);// 进度条宽度。

// 设置倒计时时间毫秒,默认3000毫秒。
progressBar.setTimeMillis(3500);

// 改变进度条颜色。
progressBar.setProgressColor(Color.RED);

// 改变外部边框颜色。
progressBar.setOutLineColor(Color.RED);

// 改变圆心颜色。
progressBar.setInCircleColor(Color.RED);

// 如果需要自动倒计时,就会自动走进度。
progressBar.start();

// 如果想自己设置进度,比如100。
progressBar.setProgress(100);

踩坑的过程

其实好久没有写过自定义View了,有些东西还真忘记了,所以写这个View的时候又把之前的坑踩了一遍,为了避免其它同学也被坑,这里把我踩的坑也记录下。

View绘制区域

这里我遇到一个问题,因为我们继承的TextView文字多了就是长的,那么绘制出来的圆长宽是一样的,所以在TextView上绘制出来的圆只能看到一部分或者是椭圆的。所以我们要把View的绘制区域扩大。当时我第一个想到的是layout()方法,因为当View的父布局onLayout()的时候会调用Viewlayout()来让子View布局,我重写了layout方法:

@Override
public void layout(int left, int top, int right, int bottom) {
    int w = right - left;
    int h = bottom - top;
    int size = w > h ? w : h;

    if (w > h) {
        bottom += (size - h);
    } else {
        right += (size - w);
    }
    super.layout(left, top, right, bottom);
}

这段代码的原理就是宽和高,那个大,就把view扩大到这么最大的这个值。

当放了一个ViewLayout时,效果出来没问题,但是我放多个ViewLinearLayout中的时候发现几个View重叠了,哦舍特。我恍然大悟啊,这尼玛人家Layout已经把我绘制区域的宽高指定了,我强行去占领别的View的了。so,我应该重写onMeasure()啊,在测量宽高的时候就告诉父Layout我要多大的地盘:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = getMeasuredWidth();
    int height = getMeasuredHeight();
    int size = width > height ? width : height;
    setMeasuredDimension(size, size);
}

这段代码的意思更容易理解,就是看super.onMeasure测量的时候的宽高哪个大,就把宽高都设置成最大的这个值。告诉父Layout我要多大的地盘,那么等我绘制的时候我想怎么玩就怎么玩。

绘制View的实现

好了,来到了关键的地方,前面的都搞定了就看我们怎么绘制我们的几个圆圈圈了。画圆圈圈就要重写onDraw()方法啦。

首先需要一个画笔:

Paint mPaint = new Paint();
mPaint.setAntiAlias(true);// 抗锯齿

拿到绘制区域

我们可以通过getDrawingRect(Rect)获取到绘制区域,通过绘制区域计算出这个区域可以绘制圆的半径。

Rect bounds = new Rect();

@Override
protected void onDraw(Canvas canvas) {
    getDrawingRect(bounds);//获取view的边界

    int size = bounds.height() > bounds.width() ? bounds.width() : bounds.height();
    float outerRadius = size / 2; // 计算出绘制圆的半径
}

绘制填充圆

那么刚才提到过点击的时候变色,所以我们要用到ColorStateList,这里做一个初始化,并且支持在xml中定义这个属性:

// 默认透明填充。
ColorStateList inCircleColors = ColorStateList.valueOf(Color.TRANSPARENT);

private void initialize(Context ctx, AttributeSet attributeSet) {
    TypedArray typedArray = ctx.obtainStyledAttributes(attributeSet, R.styleable.Progressbar);
    inCircleColors = typedArray.getColorStateList(R.styleable.Progressbar_circle_color);
    typedArray.recycle();
}

不明白如何自定View xml属性的同学请求自行Google。

根据点击、Check、Select状态绘制填充圆的颜色,因为是填充,所以这里PaintStyleFILL

int circleColor = inCircleColors.getColorForState(getDrawableState(), 0);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(circleColor);
canvas.drawCircle(bounds.centerX(), bounds.centerY(), outerRadius - outLineWidth, mPaint);

圆心是绘制区域的圆心,半径是绘制区域圆的半径减去外部轮廓圆线的宽度。这样正好填充圆和外部轮廓圆不重叠。

绘制外部边框圆

这个就简单了,因为是空心的线,所以StyleSTROKE,然后设置线的宽度,画笔的颜色:

mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(outLineWidth);
mPaint.setColor(outLineColor);
canvas.drawCircle(bounds.centerX(), bounds.centerY(), outerRadius - outLineWidth / 2, mPaint);

圆心是绘制区域的圆心,半径是绘制区域圆的半径减去外部轮廓圆线的宽度的一半,这样刚好外部轮廓线和内部填充圆紧靠着。

绘制TextView的字

为了我们的绘制和TextView自身的绘制不重叠,我们干掉了super.onDraw(canvas);,所以这里我们要把TextView的字也要写上去。

首先拿到TextView的默认画笔,设置TextView本身的字体颜色,抗锯齿,为了美观我们强行让文字居中:

//画字
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);

绘制进度条

进度条可不是一个圆了喔,准确的说它是一个圆弧,
画笔使用默认画笔,设置颜色、StyleSTROKE,设置线的宽度,最后是指定绘制区域和圆心,角度:

RectF mArcRect = new RectF();
Rect bounds = new Rect();

@Override
protected void onDraw(Canvas canvas) {
    getDrawingRect(bounds);//获取view的边界
    ...

    // 绘制进度条圆弧。
    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);
}

这里难点在指定绘制区域,因为不能把外部轮廓线覆盖了,所以要贴近外部轮廓线的内部画,所以要最外层绘制圆的区域,所以要减去(外部圆线的宽 + 进度条线的宽) / 2得出来的界线就是进度条的边界。

绘制和测量的完整代码

到这里关键代码都撸完了,你可以自己写一个试试了,我这里把完整的onDraw()onMeasure()的源码贴出来:

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;
final Rect bounds = new Rect();

@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);
}

目前已知的兼容问题修复

  1. 目前CircleTextProgressbarReletiveLayot中高度会变大,导致进度条会有一点点扁。修复方法如下:
    如果你要在ReletiveLayot中使用CircleTextProgressbar,就不要重写onMeasure()方法,然后在xml中指定CircleTextProgressbar的宽高就好,比如都指定为50dp,然后就没有问题啦。

上述完整源码:https://github.com/yanzhenjie/CircleTextProgressbar,欢迎Star。


版权声明:转载必须注明本文转自严振杰的博客: http://blog.csdn.net/yanzhenjie1003

  • 19
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
应用启动页自定义跳转计时器View Demo: CircleTextProgressbar.java: package com.demo.startpageskiptimerdemo.widget; 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.text.TextUtils; import android.util.AttributeSet; import com.demo.startpageskiptimerdemo.R; /** * Created by Administrator on 2016/8/26. (自定义TextView,用于欢迎页跳过图标) */ public class CircleTextProgressbar extends android.support.v7.widget.AppCompatTextView { /** * 外部轮廓的颜色。 */ private int outLineColor = Color.BLACK; /** * 外部轮廓的宽度。 */ private int outLineWidth = 2; /** * 内部圆的颜色。 */ private ColorStateList inCircleColors = ColorStateList.valueOf(Color.TRANSPARENT); /** * 中心圆的颜色。 */ private int circleColor; /** * 进度条的颜色。 */ private int progressLineColor = Color.WHITE; /** * 进度条的宽度。 */ private int progressLineWidth = 8; /** * 画笔。 */ private Paint mPaint = new Paint(); /** * 进度条的矩形区域。 */ private RectF mArcRect = new RectF(); /** * 进度。 */ private int progress = 100; /** * 进度条类型。 */ private ProgressType mProgressType = ProgressType.COUNT; /** * 进度倒计时时间。 */ private long timeMillis = 5000; /** * View的显示区域。 */ final Rect bounds = new Rect(); /** * 进度条通知。 */ private OnCountdownProgressListener mCountdownProgressListener; /** * Listener what。 */ private int listenerWhat = 0; private String seconds; public CircleTextProgressbar(Context context) { this(context, null); } public CircleTextProgressbar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleTextProgressbar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initialize(context, attrs); } public void setSeconds(String seconds) { this.seconds = seconds; invalidate(); } /** * 初始化。 * * @param context 上下文。 * @param attributeSet 属性。 */ private void initialize(Context context, AttributeSet attributeSet) { mPaint.setAntiAlias(true); TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CircleTextProgressbar); if (typedArray.hasValue(R.styleable.CircleTextProgressbar_in_circle_color)) inCircleColors = typedArray.getColorStateList(R.styleable.CircleTextProgressbar_in_circle_color); else inCircleColors = ColorStateList.valueOf(Color.TRANSPARENT); circleColor = inCircleColors.getColorForState(getDrawableState(), Color.TRANSPARENT); typedArray.recycle(); } /** * 设置外部轮廓的颜色。 * * @param outLineColor 颜色值。 */ // public void setOutLineColor(@ColorInt int outLineColor) { public void setOutLineColor(int outLineColor) { this.outLineColor = outLineColor; invalidate(); } /** * 设置外部轮廓的颜色。 * * @param outLineWidth 颜色值。 */ // public void setOutLineWidth(@ColorInt int outLineWidth) { public void setOutLineWidth(int outLineWidth) { this.outLineWidth = outLineWidth; invalidate(); } /** * 设置圆形的填充颜色。 * * @param inCircleColor 颜色值。 */ // public void setInCircleColor(@ColorInt int inCircleColor) { public void setInCircleColor(int inCircleColor) { this.inCircleColors = ColorStateList.valueOf(inCircleColor); invalidate(); } /** * 是否需要更新圆的颜色。 */ private void validateCircleColor() { int circleColorTemp = inCircleColors.getColorForState(getDrawableState(), Color.TRANSPARENT); if (circleColor != circleColorTemp) { circleColor = circleColorTemp; invalidate(); } } /** * 设置进度条颜色。 * * @param progressLineColor 颜色值。 */ // public void setProgressColor(@ColorInt int progressLineColor) { public void setProgressColor(int progressLineColor) { this.progressLineColor = progressLineColor; invalidate(); } /** * 设置进度条线的宽度。 * * @param progressLineWidth 宽度值。 */ public void setProgressLineWidth(int progressLineWidth) { this.progressLineWidth = progressLineWidth; invalidate(); } /** * 设置进度。 * * @param progress 进度。 */ public void setProgress(int progress) { this.progress = validateProgress(progress); invalidate(); } /** * 验证进度。 * * @param progress 你要验证的进度值。 * @return 返回真正的进度值。 */ private int validateProgress(int progress) { if (progress > 100) progress = 100; else if (progress < 0) progress = 0; return progress; } /** * 拿到此时的进度。 * * @return 进度值,最大100,最小0。 */ public int getProgress() { return progress; } /** * 设置倒计时总时间。 * * @param timeMillis 毫秒。 */ public void setTimeMillis(long timeMillis) { this.timeMillis = timeMillis; invalidate(); } /** * 拿到进度条计时时间。 * * @return 毫秒。 */ public long getTimeMillis() { return this.timeMillis; } /** * 设置进度条类型。 * * @param progressType {@link ProgressType}. */ public void setProgressType(ProgressType progressType) { this.mProgressType = progressType; resetProgress(); invalidate(); } /** * 重置进度。 */ private void resetProgress() { switch (mProgressType) { case COUNT: progress = 0; break; } } /** * 拿到进度条类型。 * * @return */ public ProgressType getProgressType() { return mProgressType; } /** * 设置进度监听。 * * @param mCountdownProgressListener 监听器。 */ 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.centerX(); float textS = (bounds.bottom + paint.ascent() / 2) - 10; if (!TextUtils.isEmpty(seconds)) canvas.drawText(seconds, bounds.centerX(), textS, paint); 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, 270, 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(); } /** * 进度更新task。 */ private Runnable progressChangeTask = new Runnable() { @Override public void run() { removeCallbacks(this); switch (mProgressType) { case COUNT: 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, } /** * 进度监听。 */ public interface OnCountdownProgressListener { /** * 进度通知。 * * @param progress 进度值。 */ void onProgress(int what, int progress); } } attrs.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <!-- 欢迎页跳过按钮样式 --> <declare-styleable name="CircleTextProgressbar"> <attr name="in_circle_color" format="color" /> <attr name="cenerTextSie" format="dimension"></attr> <attr name="secondsTextSize" format="dimension"></attr> <attr name="secondsText" format="string"></attr> </declare-styleable> </resources> StartPageActivity.java: package com.demo.startpageskiptimerdemo.activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import com.bumptech.glide.Glide; import com.bumptech.glide.signature.StringSignature; import com.demo.startpageskiptimerdemo.R; import com.demo.startpageskiptimerdemo.widget.CircleTextProgressbar; import java.util.Timer; import java.util.TimerTask; /** * 启动页面 * * @author chenke * @time 2017/12/7 10:13 * @mail [email protected] */ public class StartPageActivity extends AppCompatActivity implements View.OnTouchListener { private final static String TAG = "StartPageActivity"; CircleTextProgressbar mTvSkip; ImageView mStartPageImag; LinearLayout skipLayout; RelativeLayout welBgRlay; // 倒计时timer private Timer timer; // 启动页图片地址 private String imgUrl = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1851053687,4000575540&fm=27&gp=0.jpg"; private int waitTime = 5;// 时长5秒 Handler mHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message message) { switch (message.what) { case 1: gotoHomeActivity(); break; } return false; } }); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start_page); init(); } private void init() { mTvSkip = (CircleTextProgressbar) findViewById(R.id.tv_red_skip); mStartPageImag = (ImageView) findViewById(R.id.star_page_icon); skipLayout = (LinearLayout) findViewById(R.id.skip_layout); welBgRlay = (RelativeLayout) findViewById(R.id.start_goadvert_lay); mTvSkip.setOutLineColor(Color.TRANSPARENT); mTvSkip.setInCircleColor(Color.parseColor("#AAC6C6C6")); mTvSkip.setProgressColor(Color.WHITE); mTvSkip.setProgressLineWidth(5); // 开始时计 mCountDowntimer(); mTvSkip.reStart(); // 设置skipLayout浮在整个页面的最上层 welBgRlay.bringChildToFront(skipLayout); mTvSkip.setOnTouchListener(this); mStartPageImag.setOnTouchListener(this); // 加载图片 if (!TextUtils.isEmpty(imgUrl)) { Glide.with(this).load(imgUrl).signature(new StringSignature("1")).into (mStartPageImag); } } @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { if (null != timer) { timer.cancel(); timer = null; } switch (view.getId()) { case R.id.star_page_icon: // 广告 //********** 跳转网页 (进入启动页面广告详情)*********** break; case R.id.tv_red_skip: // 跳转计时view if (null != timer) { timer.cancel(); timer = null; } mHandler.sendEmptyMessage(1); return true; } } return false; } /** * 进入首页 */ public void gotoHomeActivity() { Intent homeIntent = new Intent(); homeIntent.setClass(this, MainActivity.class); startActivity(homeIntent); finish(); } /** * 计时器 */ private void mCountDowntimer() { timer = new Timer(); mTvSkip.setSeconds(waitTime + "s"); timer.schedule(task, 1000, 1000); } /** * 计时器线程 */ TimerTask task = new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { // UI thread @Override public void run() { waitTime--; mTvSkip.setSeconds(waitTime + "s"); if (waitTime <= 0) { if (null != timer) { timer.cancel(); timer = null; } mHandler.sendEmptyMessage(1); } } }); } }; } activity_start_page.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/start_goadvert_lay" android:layout_width="match_parent" android:layout_height="match_parent" android: <ImageView android:id="@+id/star_page_icon" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:scaleType="fitXY" /> <LinearLayout android:id="@+id/skip_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:gravity="center" android:orientation="horizontal" android:paddingRight="10dp" android:paddingTop="10dp" android:visibility="visible"> <com.demo.startpageskiptimerdemo.widget.CircleTextProgressbar android:id="@+id/tv_red_skip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳过" android:textColor="@android:color/white" android:textSize="10dp" /> </LinearLayout> </RelativeLayout> styles.xml: <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!--启动页面跳过按钮自定义style--> <style name="CustomProgressStyle" parent="@android:style/Widget.ProgressBar.Large"> <item name="android:minWidth">35dip</item> <item name="android:maxWidth">35dip</item> <item name="android:minHeight">35dip</item> <item name="android:maxHeight">35dip</item> </style> </resources> AndroidManifest.xml中添加请求网络权限: <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> build.gradle中dependencies添加: // 异步加载图片 compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.android.support:support-v4:23.3.0'

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值