Android倒計(jì)時(shí)控件 Splash界面5秒自動(dòng)跳轉(zhuǎn)
現(xiàn)在很多app的首頁(yè)都有一個(gè)倒計(jì)時(shí)控件,比如說(shuō)3秒或者5秒自動(dòng)跳轉(zhuǎn)界面,或者點(diǎn)擊控件直接跳過(guò)
首先,自定義控件CircleProgressbar(參考網(wǎng)上資料)
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; //內(nèi)部圓的顏色 private ColorStateList inCircleColors = ColorStateList.valueOf(Color.TRANSPARENT); //中心圓的顏色 private int circleColor; //進(jìn)度條的顏色 private int progressLineColor = Color.BLUE; //進(jìn)度條的寬度 private int progressLineWidth = 8; //畫(huà)筆 private Paint mPaint = new Paint(); //進(jìn)度條的矩形區(qū)域 private RectF mArcRect = new RectF(); //進(jìn)度 private int progress = 100; //進(jìn)度條類型 private ProgressType mProgressType = ProgressType.COUNT_BACK; //進(jìn)度倒計(jì)時(shí)時(shí)間 private long timeMillis = 3000; //View的顯示區(qū)域。 final Rect bounds = new Rect(); //進(jìn)度條通知。 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; //畫(huà)內(nèi)部背景 int circleColor = inCircleColors.getColorForState(getDrawableState(), 0); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(circleColor); canvas.drawCircle(bounds.centerX(), bounds.centerY(), outerRadius - outLineWidth, mPaint); //畫(huà)邊框圓 mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(outLineWidth); mPaint.setColor(outLineColor); canvas.drawCircle(bounds.centerX(), bounds.centerY(), outerRadius - outLineWidth / 2, mPaint); //畫(huà)字 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); //畫(huà)進(jìn)度條 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 { /** * 順數(shù)進(jìn)度條,從0-100; */ COUNT, /** * 倒數(shù)進(jìn)度條,從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="跳過(guò)" 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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:一個(gè)簡(jiǎn)單的Android軌跡動(dòng)畫(huà)
欄 目:Android
下一篇:一個(gè)簡(jiǎn)單的Android圓弧刷新動(dòng)畫(huà)
本文標(biāo)題:Android倒計(jì)時(shí)控件 Splash界面5秒自動(dòng)跳轉(zhuǎn)
本文地址:http://mengdiqiu.com.cn/a1/Android/9175.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開(kāi)發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10android異步消息機(jī)制 源碼層面徹底解析(1)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什