您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Android使用view實現倒計時控件,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
直接上代碼
自定義TextView
文字展示
public class StrokeTextView extends TextView { private TextView borderText = null;///用于描邊的TextView private Context mContext; public StrokeTextView(Context context) { super(context); mContext = context; borderText = new TextView(context); init(); } public StrokeTextView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; borderText = new TextView(context, attrs); init(); } public StrokeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; borderText = new TextView(context, attrs, defStyle); init(); } public void init() { TextPaint tp1 = borderText.getPaint(); tp1.setStrokeWidth(12); //設置描邊寬度 tp1.setStyle(Paint.Style.STROKE); //對文字只描邊 //設置自定義字體 Typeface fromAsset = Typeface.createFromAsset(mContext.getAssets(), "fonts/Alibaba-PuHuiTi-Heavy.ttf"); borderText.setTypeface(fromAsset, Typeface.ITALIC); //自定義字體 ITALIC斜體 borderText.setTextColor(Color.parseColor("#F46059")); //設置描邊顏色 borderText.setShadowLayer(3.0F, 2F, 2F, Color.parseColor("#ffd44042")); //設置陰影效果(投影) borderText.setGravity(getGravity()); } @Override public void setLayoutParams(ViewGroup.LayoutParams params) { super.setLayoutParams(params); borderText.setLayoutParams(params); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { CharSequence tt = borderText.getText(); //兩個TextView上的文字必須一致 if (tt == null || !tt.equals(this.getText())) { borderText.setText(getText()); this.postInvalidate(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); borderText.measure(widthMeasureSpec, heightMeasureSpec); } protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); borderText.layout(left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { borderText.draw(canvas); super.onDraw(canvas); } }
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:background="#F3B243" android:layout_height="match_parent" tools:context=".countdown.TestCountActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.xiao.test.countdown.StrokeTextView android:layout_marginTop="100dp" android:id="@+id/tv_test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/play_advertising_timer_bg" android:paddingLeft="15dp" android:textColor="#FFFFFF" android:textSize="33sp" android:layout_centerHorizontal="true" android:layout_gravity="center" android:gravity="center_vertical" android:textStyle="italic" android:typeface="monospace" tools:ignore="RtlSymmetry" android:paddingStart="15dp" /> </RelativeLayout> </LinearLayout>
倒計時幫助類
public class CountDownHelper { private OnCountDownListener onCountDownListener; private Disposable disposable; private long remainingTime; public CountDownHelper(long remainingTime) { this.remainingTime = remainingTime; } /** * 回收倒計時 */ public void destory() { if (disposable != null && !disposable.isDisposed()) { disposable.dispose(); } } /** * 開始倒計時 */ public void startCompute() { Observable.interval(1, TimeUnit.SECONDS) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<Long>() { @Override public void onSubscribe(Disposable d) { disposable = d; } @Override public void onNext(Long aLong) { if (onCountDownListener == null) { return; } remainingTime -= 1000; if (remainingTime > 0) { int day = (int) (remainingTime / (1000 * 60 * 60 * 24)); int hour = (int) ((remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); int minute = (int) ((remainingTime % (1000 * 60 * 60)) / (1000 * 60)); int second = (int) ((remainingTime % (1000 * 60)) / 1000); String dayStr = day >= 10 ? String.valueOf(day) : "0" + day; String hourStr = hour >= 10 ? String.valueOf(hour) : "0" + hour; String minuteStr = minute >= 10 ? String.valueOf(minute) : "0" + minute; String secondStr = second >= 10 ? String.valueOf(second) : "0" + second; onCountDownListener.countDown(dayStr, hourStr, minuteStr, secondStr); if (remainingTime <= 0) { onCountDownListener.countDownFinish(); if (disposable != null && !disposable.isDisposed()) { disposable.dispose(); } } } else { onCountDownListener.countDownFinish(); if (disposable != null && !disposable.isDisposed()) { disposable.dispose(); } } } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } /** * 設置倒計時回調監聽 * * @param onCountDownListener 倒計時監聽 */ public void setOnCountDownListener(OnCountDownListener onCountDownListener) { this.onCountDownListener = onCountDownListener; } public interface OnCountDownListener { /** * 倒計時 * * @param day 天 * @param hour 小時 * @param minute 分鐘 * @param second 秒 */ void countDown(String day, String hour, String minute, String second); /** * 倒計時完成 */ void countDownFinish(); } }
TestCountActivity.java
public class TestCountActivity extends AppCompatActivity { private CountDownHelper mCountDownHelper; private StrokeTextView mTvTest; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_count); mTvTest = findViewById(R.id.tv_test); //設置自定義字體 Typeface fromAsset = Typeface.createFromAsset(getAssets(), "fonts/Alibaba-PuHuiTi-Heavy.ttf"); mTvTest.setTypeface(fromAsset, Typeface.ITALIC); //自定義字體 ITALIC斜體 long aLong = 1787; mCountDownHelper = new CountDownHelper(aLong * 1000); mCountDownHelper.startCompute(); mCountDownHelper.setOnCountDownListener(new CountDownHelper.OnCountDownListener() { @SuppressLint("SetTextI18n") @Override public void countDown(String day, String hour, String minute, String second) { mTvTest.setText(hour + ":" + minute + ":" + second); } @Override public void countDownFinish() { Log.d("", "結束倒計時"); mCountDownHelper.destory(); //延時跳轉 new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { Toast.makeText(TestCountActivity.this, "時間到了", Toast.LENGTH_SHORT).show(); return false; } }).sendEmptyMessageDelayed(0, 10000);//表示延遲10秒發送任務 } }); } }
引入依賴
implementation ‘io.reactivex.rxjava2:rxjava:2.0.1' implementation ‘io.reactivex.rxjava2:rxandroid:2.0.1'
關于Android使用view實現倒計時控件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。