Commit 1cbe43ca by Baoxy

build: 临时出版本

parent c875c4a1
...@@ -149,6 +149,7 @@ dependencies { ...@@ -149,6 +149,7 @@ dependencies {
implementation "com.qmuiteam:qmui:1.3.0" implementation "com.qmuiteam:qmui:1.3.0"
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.61" implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.61"
// implementation 'com.dou361.dialogui:jjdxm-dialogui:1.0.2' // implementation 'com.dou361.dialogui:jjdxm-dialogui:1.0.2'
implementation 'com.contrarywind:Android-PickerView:4.1.9'
} }
......
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1px"
android:color="#FFDFDFDF" />
<solid android:color="#FFF2F2F2" />
<corners android:radius="8dp" />
</shape>
\ No newline at end of file
...@@ -56,10 +56,63 @@ ...@@ -56,10 +56,63 @@
android:textSize="15sp" /> android:textSize="15sp" />
</LinearLayout> </LinearLayout>
<com.secspace.lib.common.function.view.TimeProgress <com.secspace.lib.common.function.view.AppUseProgress
android:id="@+id/time_progress" android:id="@+id/time_progress"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@color/bg_white" android:background="@color/bg_white"
android:padding="10dp" /> android:padding="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="@drawable/select_date_select"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="48dp"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="选择日期"
android:textColor="#ACACAC"
android:textSize="15sp" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="#ACACAC"/>
<TextView
android:id="@+id/tv_date_start"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="48dp"
android:gravity="center"
android:text="开始时间"
android:textColor="#ACACAC"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:textColor="#ACACAC"
android:gravity="center"
android:text=" — "/>
<TextView
android:id="@+id/tv_date_end"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="48dp"
android:gravity="center"
android:text="结束时间"
android:textColor="#ACACAC"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout> </LinearLayout>
package com.android.launcher3.function.setting package com.android.launcher3.function.setting
import android.content.Context import android.content.Context
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.View import android.view.View
import android.widget.GridLayout
import android.widget.LinearLayout
import com.android.launcher3.BaseActivity import com.android.launcher3.BaseActivity
import com.android.launcher3.R import com.android.launcher3.R
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.secspace.lib.common.function.database.DatabaseManager import com.secspace.lib.common.function.database.DatabaseManager
import com.secspace.lib.common.function.entity.UseAppEntity import com.secspace.lib.common.function.entity.UseAppEntity
import com.secspace.lib.common.function.model.UseTime import com.secspace.lib.common.function.model.UseTime
import com.secspace.lib.common.utils.*
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.android.synthetic.main.activity_setting.tv_setting_title import kotlinx.android.synthetic.main.activity_setting.tv_setting_title
import kotlinx.android.synthetic.main.activity_use_app.* import kotlinx.android.synthetic.main.activity_use_app.*
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlin.properties.Delegates import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
class AppUseActivity : BaseActivity() { class AppUseActivity : BaseActivity() {
private var TAG: String? = "TagAppUseActivity" private var TAG: String? = "TagAppUseActivity"
private var mContext: Context? = null private var mContext: Context? = null
private var useAppEntitys: ArrayList<UseAppEntity>? = null private var defaultUseAppEntitys: ArrayList<UseAppEntity>? = null
private var startTime: Long = 0 private var defaultStartTime: Long = 0
private var endTime: Long = 0 private var defaultEndTime: Long = 0
private var totalTime = 0L private var defaultTotalTime = 0L
private var selectStartTime: Long = 0
private var selectEndTime: Long = 0
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setAndroidNativeLightStatusBar(true) setAndroidNativeLightStatusBar(true)
setContentView(R.layout.activity_use_app) setContentView(R.layout.activity_use_app)
mContext = this mContext = this
tv_setting_title.setText(R.string.edu_item_app_use_title)
if (useAppEntitys == null) {
loadData()
} else {
initView() initView()
initSelector()
if (defaultUseAppEntitys == null) {
defaultLoadData()
} else {
initChart()
} }
} }
private fun loadData() { private fun defaultLoadData() {
endTime = System.currentTimeMillis() defaultEndTime = System.currentTimeMillis()
startTime = endTime - 24 * 60 * 60 * 1000 defaultStartTime = defaultEndTime - 24 * 60 * 60 * 1000
useAppEntitys = ArrayList() defaultUseAppEntitys = ArrayList()
totalTime = 0L defaultTotalTime = 0L
GlobalScope.launch(Dispatchers.IO) { GlobalScope.launch(Dispatchers.IO) {
mContext?.let { it -> mContext?.let { it ->
val useAppDao = DatabaseManager.getInstance(it).getUseAppDao() val useAppDao = DatabaseManager.getInstance(it).getUseAppDao()
val useTimeDao = DatabaseManager.getInstance(it).getUseTimeDao() val useTimeDao = DatabaseManager.getInstance(it).getUseTimeDao()
val useApps = useAppDao?.queryAll() val useApps = useAppDao?.queryAll()
val useTimes = useTimeDao?.queryByTime(startTime, endTime) val useTimes = useTimeDao?.queryByTime(defaultStartTime, defaultEndTime)
if (useApps == null) { if (useApps == null) {
return@let return@let
} }
Log.i(TAG, "useApps: ${useApps.size} useTimes: ${useTimes?.size}") Log.i(TAG, "useApps: ${useApps.size} useTimes: ${useTimes?.size}")
val tempEntitys = ArrayList<UseAppEntity>() val tempEntitys = ArrayList<UseAppEntity>()
useTimes?.map<UseTime, UseTime> { useTime -> useTimes?.map<UseTime, UseTime> { useTime ->
if (useTime.useStartTime < startTime) { if (useTime.useStartTime < defaultStartTime) {
useTime.useStartTime = startTime useTime.useStartTime = defaultStartTime
} }
if (useTime.useEndTime > endTime) { if (useTime.useEndTime > defaultEndTime) {
useTime.useEndTime = endTime useTime.useEndTime = defaultEndTime
} }
useTime useTime
}?.filter { useTime -> }?.filter { useTime ->
...@@ -74,47 +77,96 @@ class AppUseActivity : BaseActivity() { ...@@ -74,47 +77,96 @@ class AppUseActivity : BaseActivity() {
useApp.appPackage == useTime.appPackage useApp.appPackage == useTime.appPackage
} }
}?.forEach { useTime -> }?.forEach { useTime ->
val useApp = UseAppEntity(useTime.appPackage, "", (useTime.useEndTime - useTime.useStartTime)/1000/60) val useApp = UseAppEntity(useTime.appPackage, "", (useTime.useEndTime - useTime.useStartTime) / 1000 / 60)
tempEntitys.add(useApp) tempEntitys.add(useApp)
} }
tempEntitys.groupBy { tempEntitys.groupBy {
it.appPackage it.appPackage
}.values.flatten().forEach { useAppEntity -> }.values.flatten().forEach { useAppEntity ->
useApps.forEach { useApps.forEach {
if (it.appPackage == useAppEntity.appPackage){ if (it.appPackage == useAppEntity.appPackage) {
useAppEntity.appName = it.appName useAppEntity.appName = it.appName
} }
} }
useAppEntitys?.add(useAppEntity) defaultUseAppEntitys?.add(useAppEntity)
totalTime += useAppEntity.useTime defaultTotalTime += useAppEntity.useTime
} }
// 根据使用时间降序排列 // 根据使用时间降序排列
useAppEntitys!!.sortByDescending { it.useTime } defaultUseAppEntitys!!.sortByDescending { it.useTime }
if(useAppEntitys!!.size > 4){ if (defaultUseAppEntitys!!.size > 4) {
val tempAppEntity = UseAppEntity("", "", 0) val tempAppEntity = UseAppEntity("", "", 0)
for (i in 4..useAppEntitys!!.size){ for (i in 4..defaultUseAppEntitys!!.size) {
tempAppEntity.appPackage = "Other" tempAppEntity.appPackage = "Other"
tempAppEntity.appName = "其他" tempAppEntity.appName = "其他"
tempAppEntity.useTime = tempAppEntity.useTime + useAppEntitys!![i -1].useTime tempAppEntity.useTime = tempAppEntity.useTime + defaultUseAppEntitys!![i - 1].useTime
} }
useAppEntitys!![3] = tempAppEntity defaultUseAppEntitys!![3] = tempAppEntity
useAppEntitys!!.removeAt(4) defaultUseAppEntitys!!.removeAt(4)
} }
loadFinished() loadFinished()
} }
} }
} }
private suspend fun loadFinished() = withContext(Dispatchers.Main){ private suspend fun loadFinished() = withContext(Dispatchers.Main) {
initView() initChart()
} }
private fun initView() { private fun initView() {
tv_app_use_time.text = String.format(getString(R.string.edu_item_app_use_last_day), totalTime) tv_setting_title.setText(R.string.edu_item_app_use_title)
time_progress.setData(useAppEntitys, totalTime)
val startDate = Calendar.getInstance()
startDate.set(2019, 0, 1)
val endDate = Calendar.getInstance()
endDate.set(2030, 11, 31)
val selectedDate = Calendar.getInstance()
val beforeDate = Calendar.getInstance()
beforeDate.set(selectedDate.time.year, selectedDate.time.month, selectedDate.time.day - 1)
}
private fun initChart() {
tv_app_use_time.text = String.format(getString(R.string.edu_item_app_use_last_day), defaultTotalTime)
time_progress.setData(defaultUseAppEntitys, defaultTotalTime)
time_progress.initAnimation() time_progress.initAnimation()
} }
private fun initSelector() {
val format = SimpleDateFormat("yyyy-MM-dd", Locale.US)
selectEndTime = System.currentTimeMillis()
selectStartTime = selectEndTime - 24 * 60 * 60 * 1000
tv_date_start.text = format.format(Date(selectStartTime))
tv_date_end.text = format.format(Date(selectEndTime))
val startDate = Calendar.getInstance()
startDate.set(2019, 0, 1)
val endDate = Calendar.getInstance()
endDate.set(2030, 11, 31)
val startSelectDate = Calendar.getInstance()
startSelectDate.set(SimpleDateFormat("yyyy", Locale.US).format(selectStartTime), SimpleDateFormat("MM", Locale.US).format(selectStartTime), SimpleDateFormat("dd", Locale.US).format(selectStartTime))
val nowDate = Calendar.getInstance()
nowDate.set(SimpleDateFormat("yyyy", Locale.US).format(selectEndTime), SimpleDateFormat("MM", Locale.US).format(selectEndTime), SimpleDateFormat("dd", Locale.US).format(selectEndTime))
tv_date_start.setOnClickListener {
startSelectDate.set(SimpleDateFormat("yyyy", Locale.US).format(selectStartTime), SimpleDateFormat("MM", Locale.US).format(selectStartTime), SimpleDateFormat("dd", Locale.US).format(selectStartTime))
TimePickerBuilder(this, OnTimeSelectListener { date, _ ->
selectStartTime = date.time
tv_date_start.text = format.format(Date(selectStartTime))
Log.e(TAG, "开始选择:${selectStartTime}")
}).setRangDate(startDate, endDate)
.setDate(startSelectDate)
.build().show()
}
tv_date_end.setOnClickListener {
nowDate.set(SimpleDateFormat("yyyy", Locale.US).format(selectEndTime), SimpleDateFormat("MM", Locale.US).format(selectEndTime), SimpleDateFormat("dd", Locale.US).format(selectEndTime))
TimePickerBuilder(this, OnTimeSelectListener { date, _ ->
selectEndTime = date.time
tv_date_end.text = format.format(Date(selectEndTime))
Log.e(TAG, "结束选择:${selectEndTime}")
}).setRangDate(startDate, endDate)
.setDate(nowDate)
.build().show()
}
}
fun close(view: View?) { fun close(view: View?) {
finish() finish()
} }
...@@ -123,3 +175,7 @@ class AppUseActivity : BaseActivity() { ...@@ -123,3 +175,7 @@ class AppUseActivity : BaseActivity() {
} }
} }
private fun Calendar.set(format: String?, format1: String?, format2: String?) {
format?.toInt()?.let { format1?.toInt()?.let { it1 -> format2?.toInt()?.let { it2 -> set(it, it1 - 1, it2) } } }
}
...@@ -10,6 +10,7 @@ import com.android.launcher3.BaseActivity ...@@ -10,6 +10,7 @@ import com.android.launcher3.BaseActivity
import com.android.launcher3.R import com.android.launcher3.R
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView
import com.secspace.lib.common.dialog.other.DialogUIUtils
import com.secspace.lib.common.utils.* import com.secspace.lib.common.utils.*
import kotlinx.android.synthetic.main.activity_setting.* import kotlinx.android.synthetic.main.activity_setting.*
...@@ -56,7 +57,10 @@ class SettingActivity : BaseActivity() { ...@@ -56,7 +57,10 @@ class SettingActivity : BaseActivity() {
dp2px(48f)) dp2px(48f))
QMUIGroupListView.newSection(mContext) QMUIGroupListView.newSection(mContext)
.addItemView(wlan) { IntentUtil.startWifiSetting(mContext) } .addItemView(wlan) { IntentUtil.startWifiSetting(mContext) }
.addItemView(appUse) { startActivity(Intent(mContext, AppUseActivity::class.java)) } .addItemView(appUse) {
// startActivity(Intent(mContext, AppUseActivity::class.java))
DialogUIUtils.showToastCenter("开发中")
}
.addItemView(buy) { startActivity(Intent(mContext, SettingBuyActivity::class.java)) } .addItemView(buy) { startActivity(Intent(mContext, SettingBuyActivity::class.java)) }
.addItemView(about) { startActivity(Intent(mContext, SettingAboutActivity::class.java)) } .addItemView(about) { startActivity(Intent(mContext, SettingAboutActivity::class.java)) }
.addTo(grouplistview) .addTo(grouplistview)
......
...@@ -128,4 +128,5 @@ dependencies { ...@@ -128,4 +128,5 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61"
implementation "com.qmuiteam:qmui:1.3.0" implementation "com.qmuiteam:qmui:1.3.0"
implementation "org.reactivestreams:reactive-streams:1.0.2" implementation "org.reactivestreams:reactive-streams:1.0.2"
implementation 'com.contrarywind:Android-PickerView:4.1.9'
} }
\ No newline at end of file
...@@ -13,26 +13,6 @@ import android.widget.Toast; ...@@ -13,26 +13,6 @@ import android.widget.Toast;
import com.secspace.lib.common.R; import com.secspace.lib.common.R;
/**
* ========================================
* <p>
* 版 权:dou361.com 版权所有 (C) 2015
* <p>
* 作 者:陈冠明
* <p>
* 个人网站:http://www.dou361.com
* <p>
* 版 本:1.0
* <p>
* 创建日期:2016/10/31 22:17
* <p>
* 描 述:一个对外UI管理容器工具类
* <p>
* <p>
* 修订历史:
* <p>
* ========================================
*/
public class DialogUIUtils { public class DialogUIUtils {
/** /**
......
...@@ -23,7 +23,7 @@ import java.util.List; ...@@ -23,7 +23,7 @@ import java.util.List;
/** /**
* 应用使用时间进度条 * 应用使用时间进度条
*/ */
public class TimeProgress extends View { public class AppUseProgress extends View {
private List<UseAppEntity> useAppEntities = new ArrayList<>(); private List<UseAppEntity> useAppEntities = new ArrayList<>();
private Paint bgPaint; private Paint bgPaint;
...@@ -32,7 +32,6 @@ public class TimeProgress extends View { ...@@ -32,7 +32,6 @@ public class TimeProgress extends View {
private int mWidth; private int mWidth;
private int mHeight; private int mHeight;
private List<Integer> times = new ArrayList<>();
private List<Integer> paintColors = new ArrayList<>(); private List<Integer> paintColors = new ArrayList<>();
private float allTime = 0F; private float allTime = 0F;
private float animationValue; private float animationValue;
...@@ -88,25 +87,19 @@ public class TimeProgress extends View { ...@@ -88,25 +87,19 @@ public class TimeProgress extends View {
private int paddingRight; private int paddingRight;
private int paddingBottom; private int paddingBottom;
public TimeProgress(Context context) { public AppUseProgress(Context context) {
this(context, null); this(context, null);
} }
public TimeProgress(Context context, @Nullable AttributeSet attrs) { public AppUseProgress(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0); this(context, attrs, 0);
} }
public TimeProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { public AppUseProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); super(context, attrs, defStyleAttr);
init(); init();
initPaint(); initPaint();
initTextPaint(); initTextPaint();
times.add(10);
times.add(20);
times.add(80);
times.add(50);
Comparator<Integer> reverseComparator = Collections.reverseOrder();
Collections.sort(times, reverseComparator);
paintColors.add(0xFF13A6FA); paintColors.add(0xFF13A6FA);
paintColors.add(0xFF9BFA13); paintColors.add(0xFF9BFA13);
paintColors.add(0xFFFAB613); paintColors.add(0xFFFAB613);
......
package com.secspace.lib.common.function.view;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;
import java.text.DecimalFormat;
/**
* 应用使用时间
*/
public class UseTimeProgress extends View {
private Paint bgPaint;
private Paint progressPaint;
private Paint textPaint;
private int mWidth;
private int mHeight;
private int mViewHeight;
/**
* 进度
*/
private float mProgress;
//描述文字的高度
private float textHeight;
//描述文字的高度
private float textWidth;
/**
* 当前进度
*/
private float currentProgress;
/**
* 进度动画
*/
private ValueAnimator progressAnimator;
/**
* 动画执行时间
*/
private int duration = 1000;
/**
* 动画延时启动时间
*/
private int startDelay = 500;
/**
* 进度条画笔的宽度
*/
private int progressPaintWidth;
private int progressHeight;
/**
* 进度条距离提示框的高度
*/
private int progressMarginTop;
/**
* 进度移动的距离
*/
private float moveDis;
private Rect textRect = new Rect();
private String textString = "已售0%";
/**
* 百分比文字字体大小
*/
private int textPaintSize;
/**
* 进度条背景颜色
*/
private int bgColor = 0xFFeaeef0;
/**
* 进度条颜色
*/
private int progressColor = 0xFF13A6FA;
private RectF bgRectF = new RectF();
private RectF progressRectF = new RectF();
/**
* 圆角矩形的圆角半径
*/
private int roundRectRadius;
/**
* 进度监听回调
*/
private ProgressListener progressListener;
public UseTimeProgress(Context context) {
this(context, null);
}
public UseTimeProgress(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public UseTimeProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
initPaint();
initTextPaint();
}
/**
* 初始化画笔宽度及view大小
*/
private void init() {
progressPaintWidth = dp2px(1);
progressHeight = dp2px(3);
roundRectRadius = dp2px(3);
textPaintSize = sp2px(10);
textHeight = dp2px(10);
progressMarginTop = dp2px(4);
//view真实的高度
mViewHeight = (int) (textHeight + progressMarginTop + progressPaintWidth * 2 + progressHeight);
}
private void initPaint() {
bgPaint = getPaint(progressPaintWidth, bgColor, Paint.Style.FILL);
progressPaint = getPaint(progressPaintWidth, progressColor, Paint.Style.FILL);
}
/**
* 初始化文字画笔
*/
private void initTextPaint() {
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(textPaintSize);
textPaint.setColor(progressColor);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setAntiAlias(true);
}
/**
* 统一处理paint
*
* @param strokeWidth 画笔宽度
* @param color 颜色
* @param style 风格
* @return paint
*/
private Paint getPaint(int strokeWidth, int color, Paint.Style style) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(strokeWidth);
paint.setColor(color);
paint.setAntiAlias(true);
paint.setStyle(style);
return paint;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height));
}
/**
* 测量宽度
*
* @param mode
* @param width
* @return
*/
private int measureWidth(int mode, int width) {
switch (mode) {
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
break;
case MeasureSpec.EXACTLY:
mWidth = width;
break;
}
return mWidth;
}
public void setWidth(int width){
mWidth = width;
}
/**
* 测量高度
*
* @param mode
* @param height
* @return
*/
private int measureHeight(int mode, int height) {
switch (mode) {
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
mHeight = mViewHeight;
break;
case MeasureSpec.EXACTLY:
mHeight = height;
break;
}
return mHeight;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制文字
drawText(canvas, textString);
//背景
drawBgProgress(canvas);
//进度条
drawProgress(canvas);
}
private void drawBgProgress(Canvas canvas) {
bgRectF.left = 0;
bgRectF.top = textHeight + progressMarginTop;
bgRectF.right = this.getMeasuredWidth();
bgRectF.bottom = bgRectF.top + progressHeight;
canvas.drawRoundRect(bgRectF, roundRectRadius, roundRectRadius, bgPaint);
}
private void drawProgress(Canvas canvas) {
progressRectF.left = 0;
progressRectF.top = textHeight + progressMarginTop;
progressRectF.right = currentProgress;
progressRectF.bottom = progressRectF.top + progressHeight;
canvas.drawRoundRect(progressRectF, roundRectRadius, roundRectRadius, progressPaint);
}
/**
* 绘制文字
*
* @param canvas 画布
*/
private void drawText(Canvas canvas, String textString) {
textRect.left = (int) moveDis;
textRect.top = 0;
textRect.right = (int) (textPaint.measureText(textString) + moveDis);
textRect.bottom = (int) textHeight;
Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
int baseline = (textRect.bottom + textRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
//文字绘制到整个布局的中心位置
canvas.drawText(textString, textRect.centerX(), baseline, textPaint);
}
/**
* 进度移动动画 通过插值的方式改变移动的距离
*/
private void initAnimation() {
progressAnimator = ValueAnimator.ofFloat(0, mProgress);
progressAnimator.setDuration(duration);
progressAnimator.setStartDelay(startDelay);
progressAnimator.setInterpolator(new LinearInterpolator());
progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float value = (float) valueAnimator.getAnimatedValue();
textString = "已售" + formatNum((int) value) + "%";
textWidth = textPaint.measureText(textString);
currentProgress = value * mWidth / 100;
if (progressListener != null) {
progressListener.currentProgressListener(value);
}
//移动百分比提示框,只有当前进度到提示框中间位置之后开始移动,当进度框移动到最右边的时候停止移动,但是进度条还可以继续移动
if (currentProgress >= textWidth && currentProgress <= mWidth) {
moveDis = currentProgress - textWidth;
}
invalidate();
}
});
if (!progressAnimator.isStarted()) {
progressAnimator.start();
}
}
/**
* 回调接口
*/
public interface ProgressListener {
void currentProgressListener(float currentProgress);
}
/**
* 回调监听事件
*
* @param listener
* @return
*/
public UseTimeProgress setProgressListener(ProgressListener listener) {
progressListener = listener;
return this;
}
public UseTimeProgress setProgress(float progress) {
mProgress = progress;
initAnimation();
return this;
}
/**
* 格式化数字(保留一位小数)
*
* @param money
* @return
*/
public static String formatNum(int money) {
DecimalFormat format = new DecimalFormat("0");
return format.format(money);
}
/**
* dp 2 px
*
* @param dpVal
*/
protected int dp2px(int dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, getResources().getDisplayMetrics());
}
/**
* sp 2 px
*
* @param spVal
* @return
*/
protected int sp2px(int spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, getResources().getDisplayMetrics());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment