Android自定義view實現(xiàn)倒計時控件
更新時間:2020年10月27日 14:00:39 作者:迷路國王
這篇文章主要為大家詳細介紹了Android自定義view實現(xiàn)倒計時控件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android自定義view實現(xiàn)倒計時控件的具體代碼,供大家參考,具體內(nèi)容如下


直接上代碼
自定義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); //設(shè)置描邊寬度
tp1.setStyle(Paint.Style.STROKE); //對文字只描邊
//設(shè)置自定義字體
Typeface fromAsset = Typeface.createFromAsset(mContext.getAssets(), "fonts/Alibaba-PuHuiTi-Heavy.ttf");
borderText.setTypeface(fromAsset, Typeface.ITALIC); //自定義字體 ITALIC斜體
borderText.setTextColor(Color.parseColor("#F46059")); //設(shè)置描邊顏色
borderText.setShadowLayer(3.0F, 2F, 2F, Color.parseColor("#ffd44042")); //設(shè)置陰影效果(投影)
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() {
}
});
}
/**
* 設(shè)置倒計時回調(diào)監(jiān)聽
*
* @param onCountDownListener 倒計時監(jiān)聽
*/
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);
//設(shè)置自定義字體
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("", "結(jié)束倒計時");
mCountDownHelper.destory();
//延時跳轉(zhuǎn)
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秒發(fā)送任務(wù)
}
});
}
}
引入依賴
implementation ‘io.reactivex.rxjava2:rxjava:2.0.1' implementation ‘io.reactivex.rxjava2:rxandroid:2.0.1'
歡迎小伙伴們評論
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android實現(xiàn)啟動頁倒計時效果
- Android 實現(xiàn)搶購倒計時功能的示例
- android實現(xiàn)倒計時動態(tài)圈
- android實現(xiàn)圓環(huán)倒計時控件
- android利用handler實現(xiàn)倒計時功能
- 解決Android-RecyclerView列表倒計時錯亂問題
- Android實現(xiàn)自定義倒計時
- Android 倒計時控件 CountDownView的實例代碼詳解
- Android倒計時神器(CountDownTimer)
- Android倒計時功能的實現(xiàn)代碼
- Android 簡單實現(xiàn)倒計時功能
- Android自定義TimeButton實現(xiàn)倒計時按鈕
- Android實現(xiàn)倒計時的按鈕效果
- 利用Android設(shè)計一個倒計時組件
相關(guān)文章
Android通過命令連接wifi的方法(解決usb不能用問題)
這篇文章主要介紹了Android通過命令連接wifi的方法(解決usb不能用的情況),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
Flutter使用stack實現(xiàn)懸浮UI的示例代碼
在Flutter中,你可以使用Stack和Positioned來創(chuàng)建懸浮 UI,這篇文章主要為大家詳細介紹了Flutter使用stack實現(xiàn)懸浮UI的具體代碼,希望對大家有所幫助2024-01-01
利用Android中BitmapShader制作自帶邊框的圓形頭像
這篇文章給大家介紹了一下如何利用BitmapShader制作圓形頭像,可以自定義要顯示的圖片,邊框顏色和邊框?qū)挾鹊?,有需要的朋友們可以參考借鑒。2016-09-09
Android基于Http協(xié)議實現(xiàn)文件上傳功能的方法
這篇文章主要介紹了Android基于Http協(xié)議實現(xiàn)文件上傳功能的方法,結(jié)合實例形式分析了Android的HTTP協(xié)議原理與文件上傳功能實現(xiàn)技巧,需要的朋友可以參考下2016-07-07
AndroidStudio Gradle第三依賴統(tǒng)一管理的實現(xiàn)方法
這篇文章主要介紹了AndroidStudio Gradle第三依賴統(tǒng)一管理的實現(xiàn)方法,需要的朋友可以參考下2017-09-09

