闲暇时间写了个小软件,一是留作一个纪念,二是作为碎的知识点学习一下,后期还会加一些小功能进行维护升级,欢迎大家使用并提出宝贵意见。
应用主要截图:
启动页:
日期设置与修改:
首页:
关于:
主要代码与应用技术:
启动页启动动画
引用lottie依赖库:
implementation "com.airbnb.android:lottie:3.2.2"
布局使用(下载json动画文件并引用):
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/animation_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:lottie_autoPlay="true"
app:lottie_fileName="happy-valentines-day.json"
app:lottie_loop="false" />
公农历互转
//公历转农历
//年月日
String ymd = TimeUtils.dateToString(TimeUtils.getTimeStame(), "yyyy-MM-dd");
//年
String y = TimeUtils.dateToString(TimeUtils.getTimeStame(), "yyyy");
//农历年、月、日
String ny, nm, nd;
try {
//公历日期转换为农历日期
ny = ChinaDate2.solarToLunar(ymd, false).substring(0, 4);
nm = ChinaDate2.solarToLunar(ymd, false).substring(5, 7);
nd = ChinaDate2.solarToLunar(ymd, false).substring(8, 10);
} catch (Exception e) {
throw new RuntimeException(e);
}
Calendar today = Calendar.getInstance();
@SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date time = simpleDateFormat.parse(ymd);
if (time != null) {
today.setTime(time);
}
} catch (ParseException e) {
throw new RuntimeException(e);
}
ChinaDate lunar = new ChinaDate(today);
//公历转yyyy-MM-dd格式的农历(传参:公历年月日,是否显示闰字)
ChinaDate2.solarToLunar(ymd, true);
根据背景图状态栏字体动态变色
引入背景颜色识别库
//背景图片颜色识别库
implementation 'androidx.palette:palette:1.0.0'
//----------状态栏字体动态变色start----------//
private void setBgImageByResource(int imageResource) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageResource);
ivBg.setImageBitmap(bitmap);
detectBitmapColor(bitmap);
}
private void setBgImageByResource(Uri mImageUri) {
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(mImageUri));
ivBg.setImageBitmap(bitmap);
detectBitmapColor(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void detectBitmapColor(Bitmap bitmap) {
int colorCount = 5;
int left = 0;
int top = 0;
int right = this.getScreenWidth();
int bottom = this.getStatusBarHeight();
Palette.from(bitmap)
.maximumColorCount(colorCount)
.setRegion(left, top, right, bottom)
.generate(palette -> {
if (palette != null) {
Palette.Swatch mostPopularSwatch = null;
Iterator<Palette.Swatch> iterator = palette.getSwatches().iterator();
while (true) {
Palette.Swatch swatch;
do {
if (!iterator.hasNext()) {
if (mostPopularSwatch != null) {
double luminance = ColorUtils.calculateLuminance(mostPopularSwatch.getRgb());
if (luminance < 0.5D) {
setDarkStatusBar();
} else {
setLightStatusBar();
}
}
return;
}
swatch = (Palette.Swatch) iterator.next();
if (mostPopularSwatch == null) {
break;
}
} while (swatch.getPopulation() <= mostPopularSwatch.getPopulation());
mostPopularSwatch = swatch;
}
}
});
}
private void setLightStatusBar() {
int flags = getWindow().getDecorView().getSystemUiVisibility();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().getDecorView().setSystemUiVisibility(flags | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
private void setDarkStatusBar() {
int flags;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
flags = getWindow().getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
getWindow().getDecorView().setSystemUiVisibility(flags ^ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
private int getScreenWidth() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.widthPixels;
}
private int getStatusBarHeight() {
int result = 0;
@SuppressLint("InternalInsetResource") int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = this.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
//----------状态栏字体动态变色end----------//
使用:
//直接引用图片
setBgImageByResource(R.drawable.we_bg);
//选择图片后引用
setBgImageByResource(Uri.fromFile(new File(eventBG.getUserIconPath())));
ToastUtil工具类
public class ToastUtil {
//显示文本+图片的Toast
public static void showImageToast(Context context, String message, int iv) {
View toastview = LayoutInflater.from(context).inflate(R.layout.toast_style, null);
TextView text = toastview.findViewById(R.id.tv);
ImageView image = toastview.findViewById(R.id.iv);
image.setImageResource(iv);
text.setText(message); //要提示的文本
Toast toast = new Toast(context); //上下文
toast.setGravity(Gravity.CENTER, 0, 0); //位置居中
toast.setDuration(Toast.LENGTH_SHORT); //设置短暂提示
toast.setView(toastview); //把定义好的View布局设置到Toast里面
toast.show();
}
//显示文本的Toast
public static void showTextToast(Context context, String message) {
View toastview = LayoutInflater.from(context).inflate(R.layout.toast_style, null);
TextView text = toastview.findViewById(R.id.tv);
text.setText(message);
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER, 0, 50);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastview);
toast.show();
}
}
TimeUtils日期工具类
public class TimeUtils {
public static String dateFormat_day = "HH:mm";
public static String dateFormat_month = "MM-dd";
/**
* 时间转换成字符串,默认为"yyyy-MM-dd HH:mm:ss"
*
* @param time 时间
*/
public static String dateToString(long time) {
return dateToString(time, "yyyy-MM-dd HH:mm:ss");
}
/**
* 时间转换成字符串,指定格式
*
* @param time 时间
* @param format 时间格式
*/
public static String dateToString(long time, String format) {
Date date = new Date(time);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(date);
}
public static Date parseServerTime(String serverTime, String format) {
if (format == null || format.isEmpty()) {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINESE);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
Date date = null;
try {
date = sdf.parse(serverTime);
} catch (Exception e) {
}
return date;
}
/**
* 将时间转换为时间戳
*/
public static String dateToStamp(String s) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
/**
* 将时间转换为时间戳
*/
public static String dateToStamp2(String s) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
/**
* 获取当前的时间戳
*
* @return
*/
public static long getTimeStame() {
//获取当前的毫秒值
long time = System.currentTimeMillis();
//将毫秒值转换为String类型数据
long time_stamp = time;
//返回出去
return time_stamp;
}
}
更多工具类和源码请参考项目代码。
引用的主要三方库:
//butterknife
implementation 'com.jakewharton:butterknife:10.2.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
//时间选择器
implementation 'com.contrarywind:Android-PickerView:4.1.9'
//解决65535库
implementation 'androidx.multidex:multidex:2.0.0'
//圆角图标库
implementation 'de.hdodenhof:circleimageview:3.1.0'
//图片选择框架
implementation 'io.github.lucksiege:pictureselector:v2.7.3-rc08'
//图片加载框架
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
//网络请求所需
implementation "io.reactivex.rxjava2:rxjava:2.2.8"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'com.squareup.okio:okio:2.2.2'
implementation 'com.google.code.gson:gson:2.8.5'
//防止键盘遮挡按钮的库
implementation 'com.github.yoyoyaobin:PreventKeyboardBlockUtil:1.0.8'
//cardView
implementation 'androidx.cardview:cardview:1.0.0'
/*retrofit、rxjava*/
implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
/*YUtils*/
implementation 'com.github.yechaoa:YUtils:3.2.0'
/*BRVAH*/
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.50'
/*banner*/
implementation 'com.youth.banner:banner:1.4.10'
//刷新库
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.5.1'
//recyclerview
implementation 'androidx.recyclerview:recyclerview:1.0.0'
//下载更新
implementation 'com.github.azhon:AppUpdate:3.0.7'
//bugly异常上报
implementation 'com.tencent.bugly:crashreport:3.2.3'
//滤镜相机
implementation 'com.github.moo611:OpenCamera:1.0.6'
//权限申请
implementation 'com.permissionx.guolindev:permissionx:1.3.0'
//xpopup弹窗
implementation 'com.lxj:xpopup:2.0.8-rc5'
//沉浸状态栏基础依赖包,必须要依赖
implementation 'com.gyf.immersionbar:immersionbar:3.0.0'
// fragment快速实现(可选)
implementation 'com.gyf.immersionbar:immersionbar-components:3.0.0'
//json动画库
implementation "com.airbnb.android:lottie:3.2.2"
//eventbus
implementation 'org.greenrobot:eventbus:3.0.0'
//broccoli占位视图
implementation 'me.samlss:broccoli:1.0.0'
//material
implementation 'com.google.android.material:material:1.3.0'
//RevealLayout点赞动画库
implementation 'com.github.goweii:RevealLayout:1.1.1'
//华为扫码
implementation 'com.huawei.hms:scanplus:1.3.2.300'
//可折叠TextView
implementation 'com.zly.widget:collapsed-textview:1.0.4'
//mmkv数据存储
implementation 'com.tencent:mmkv-static:1.2.10'
//camera
implementation 'androidx.camera:camera-core:1.0.0-rc05'
implementation 'androidx.camera:camera-lifecycle:1.0.0-rc05'
//开关按钮
implementation 'com.github.zcweng:switch-button:0.0.3@aar'
//SwipeDelMenuLayout侧滑删除
implementation 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0'
//Weather_Bg天气背景
implementation 'com.github.Rainvvy:Weather_Bg:v1.1'
//tbssdk腾讯浏览器内核
api 'com.tencent.tbs:tbssdk:44181'
//litepal数据库
implementation 'org.litepal.guolindev:core:3.2.3'
//背景图片颜色识别库
implementation 'androidx.palette:palette:1.0.0'
代码有点多其他不再贴出
项目地址:
应用下载: