package com.android.app_base.utils;
|
|
import android.animation.ValueAnimator;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.net.Uri;
|
import android.os.Build;
|
import android.os.Environment;
|
import android.text.TextUtils;
|
import android.view.View;
|
import android.widget.TextView;
|
|
import androidx.core.content.FileProvider;
|
|
import com.blankj.utilcode.util.LogUtils;
|
|
import java.io.File;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* @author Ljj
|
* @date 2023.05.18. 19:29
|
* @desc
|
*/
|
public class Utils {
|
/**
|
* 下拉展开
|
* @param view 需要展开的view
|
* @param initialHeight 初始高度
|
* @param targetHeight 目标高度
|
*/
|
public static void dropExpand(View view,int initialHeight , int targetHeight) {
|
ValueAnimator animator = ValueAnimator.ofInt(initialHeight,targetHeight);
|
animator.setDuration(500);
|
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
|
@Override
|
public void onAnimationUpdate(ValueAnimator animation) {
|
int value = (int) animation.getAnimatedValue();
|
view.getLayoutParams().height = value;
|
view.requestLayout();
|
}
|
});
|
animator.start();
|
}
|
/**
|
* 上拉收起
|
* @param view 需要收起的view
|
* @param initialHeight 初始高度
|
* @param targetHeight 目标高度
|
*/
|
public static void pullCollapse(View view,int initialHeight, int targetHeight) {
|
ValueAnimator animator = ValueAnimator.ofInt(initialHeight, targetHeight);
|
animator.setDuration(500);
|
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
|
@Override
|
public void onAnimationUpdate(ValueAnimator animation) {
|
int value = (int) animation.getAnimatedValue();
|
view.getLayoutParams().height = value;
|
view.requestLayout();
|
}
|
});
|
animator.start();
|
}
|
|
/**
|
* 判断点击的点是否在view内
|
* @param x x坐标
|
* @param y y坐标
|
* @param view view
|
* @return 是否在view内
|
*/
|
public static boolean isPointInsideView(float x, float y, View view) {
|
int[] location = new int[2];
|
view.getLocationOnScreen(location);
|
|
int left = location[0];
|
int top = location[1];
|
int right = left + view.getWidth();
|
int bottom = top + view.getHeight();
|
|
return x >= left && x <= right && y >= top && y <= bottom;
|
}
|
|
/**
|
* 字符串转日期
|
* @param dateString 日期字符串
|
* @return 日期 Date
|
*/
|
public static Date parseDate(String dateString) {
|
try {
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", java.util.Locale.getDefault());
|
return dateFormat.parse(dateString);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 分割字符串成list
|
*/
|
public static List<String> splitString2List(String str, String splitStr) {
|
if (str == null|| TextUtils.isEmpty(str)){
|
return new ArrayList<>();
|
}
|
String[] split = str.split(splitStr);
|
List<String> list = new ArrayList<>();
|
for (String s : split) {
|
if (!TextUtils.isEmpty(s)){
|
list.add(s);
|
}
|
}
|
return list;
|
}
|
|
/**
|
* list拼接成字符串
|
*/
|
public static String appendList2String(List<String> list,String appendStr) {
|
if (list == null || list.size() == 0) {
|
return "";
|
}
|
StringBuilder sb = new StringBuilder();
|
for (String s : list) {
|
sb.append(s).append(appendStr);
|
}
|
//去掉最后一个拼接符
|
if (sb.length() > 0) {
|
sb.deleteCharAt(sb.length() - appendStr.length());
|
}
|
return sb.toString();
|
}
|
|
/**
|
* 设置 TextView 文本溢出处理方式
|
*/
|
public static void setTextViewEllipsize(TextView textView, TextUtils.TruncateAt where) {
|
if (textView.getEllipsize() == where) {
|
return;
|
}
|
textView.setEllipsize(where);
|
if (where != TextUtils.TruncateAt.MARQUEE) {
|
return;
|
}
|
if (!textView.isSelected()) {
|
// 设置跑马灯之后需要设置选中才能有效果
|
textView.setSelected(true);
|
}
|
if (!textView.isFocusable()) {
|
// 设置跑马灯需要先获取焦点
|
textView.setFocusable(true);
|
}
|
textView.setSingleLine(true);
|
// 设置跑马灯的循环次数
|
textView.setMarqueeRepeatLimit(-1);
|
}
|
|
/**
|
* 判断两个日期大小
|
*/
|
public static int compareDate(String startTime, String endTime) {
|
//比较时间大小
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", java.util.Locale.getDefault());
|
try {
|
Date startDate = dateFormat.parse(startTime);
|
Date endDate = dateFormat.parse(endTime);
|
if (startDate == null || endDate == null) {
|
return 0;
|
}
|
return startDate.compareTo(endDate);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return 0;
|
}
|
|
/**
|
* 计算两个日期相差天数
|
*/
|
public static int calculateDays(String startTime, String endTime) {
|
int days = 0;
|
//计算两个日期相差天数
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", java.util.Locale.getDefault());
|
try {
|
Date startDate = dateFormat.parse(startTime);
|
Date endDate = dateFormat.parse(endTime);
|
if (startDate == null || endDate == null) {
|
return 0;
|
}
|
days = (int) TimeUnit.DAYS.convert(Math.abs(startDate.getTime() - endDate.getTime()), TimeUnit.MILLISECONDS);
|
|
} catch (ParseException e) {
|
throw new RuntimeException(e);
|
}
|
return days + 1;
|
}
|
|
/**
|
* 安装 APK
|
*/
|
public static void installAPK(Context context, String fileName) {
|
fileName = Environment.getExternalStorageDirectory().getPath() + "/" + Environment.DIRECTORY_DOWNLOADS + "/" + fileName;
|
LogUtils.e(fileName);
|
File file = new File(fileName);
|
if (!file.exists()) {
|
return;
|
}
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
Uri contentUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file);
|
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
|
} else {
|
// Uri uri = Uri.parse("file://" + file.toString());
|
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
|
//在服务中开启activity必须设置flag,后面解释
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
}
|
context.startActivity(intent);
|
}
|
|
}
|