You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
8.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.example.musicplayer.util;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.example.musicplayer.R;
import com.example.musicplayer.app.Api;
import com.example.musicplayer.app.App;
/**
* Created by 残渊 on 2018/10/26.
* 通用的工具类,包含多个实用方法,可在整个应用中使用
*/
public class CommonUtil {
// 存储 Toast 实例,避免重复创建
private static Toast toast;
// 控制活动的状态栏显示或隐藏
public static void hideStatusBar(Activity activity, boolean isHide) {
// 获取活动的根视图
View decorView = activity.getWindow().getDecorView();
if (isHide) {
if (Build.VERSION.SDK_INT >= 22) {
// 隐藏状态栏并使布局扩展到状态栏区域,状态栏透明
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
}
} else {
// 显示状态栏并设置状态栏颜色
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
activity.getWindow().setStatusBarColor(App.getContext().getResources().getColor(R.color.actionBarColor));
}
}
// 显示 Toast 消息
@SuppressLint("ShowToast")
public static void showToast(Context context, String message) {
if (toast == null) {
// 创建一个新的 Toast 实例
toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
} else {
// 更新 Toast 的消息内容
toast.setText(message);
}
// 显示 Toast
toast.show();
}
// 获取屏幕的宽度
public static int getScreenWidth(Context context) {
if (null == context) {
return 0;
}
// 获取屏幕的宽度像素值
return context.getResources().getDisplayMetrics().widthPixels;
}
// 获取屏幕的高度
public static int getScreenHeight(Context context) {
if (null == context) {
return 0;
}
// 获取屏幕的高度像素值
return context.getResources().getDisplayMetrics().heightPixels;
}
// 使 EditText 获取焦点并显示软键盘
public static void showKeyboard(EditText editText, Context context) {
if (editText!= null) {
// 使 EditText 可获得焦点
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
// 请求获得焦点
editText.requestFocus();
// 获取输入法管理器
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
// 显示软键盘
inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}
// 关闭软键盘
public static void closeKeybord(EditText mEditText, Context context) {
// 清除 EditText 的焦点
mEditText.clearFocus();
// 获取输入法管理器
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
// 隐藏软键盘
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}
// 使指定字符串在 TextView 中显示不同颜色
public static void showStringColor(String appointStr, String originalStr, TextView textView) {
// 将原始字符串中的指定字符串替换为带有颜色标记的 HTML 字符串
originalStr = originalStr.replaceAll(appointStr, "<font color='#FFC66D'>" + appointStr + "</font>");
// 使用 Html.fromHtml 将 HTML 字符串转换为 Spanned 并设置到 TextView 中
textView.setText(Html.fromHtml(originalStr));
}
// 获取状态栏的高度
public static int getStatusHeightPx(Activity act) {
int height = 0;
// 获取状态栏高度资源的标识符
int resourceId = act.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
// 获取状态栏的高度像素值
height = act.getResources().getDimensionPixelSize(resourceId);
}
return height;
}
// 生成前景模糊的 Drawable
public static Drawable getForegroundDrawable(Bitmap bitmap) {
// 计算屏幕宽高比
final float widthHeightSize = (float) (DisplayUtil.getScreenWidth(App.getContext())
* 1.0 / DisplayUtil.getScreenHeight(App.getContext()) * 1.0);
// 计算裁剪图片的宽度
int cropBitmapWidth = (int) (widthHeightSize * bitmap.getHeight());
int cropBitmapWidthX = (int) ((bitmap.getWidth() - cropBitmapWidth) / 2.0);
// 裁剪部分图片
Bitmap cropBitmap = Bitmap.createBitmap(bitmap, cropBitmapWidthX, 0, cropBitmapWidth,
bitmap.getHeight());
// 缩小图片
Bitmap scaleBitmap = Bitmap.createScaledBitmap(cropBitmap, bitmap.getWidth() / 50, bitmap
.getHeight() / 50, false);
// 对缩小后的图片进行模糊处理
final Bitmap blurBitmap = FastBlurUtil.doBlur(scaleBitmap, 3, true);
// 创建模糊后的 Drawable
final Drawable foregroundDrawable = new BitmapDrawable(blurBitmap);
// 可添加灰色遮罩层(未实现)
return foregroundDrawable;
}
// 使用 Glide 加载图片并获取其 Bitmap
public static Bitmap getImgBitmap(Context context, String imgUrl) {
SimpleTarget target = new SimpleTarget<Drawable>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
@Override
public void onResourceReady(@Nullable Drawable resource, Transition<? super Drawable> transition) {
// 当资源加载完成时,将 Drawable 转换为 Bitmap
Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
}
};
// 使用 Glide 加载图片,设置占位符和错误图片
Glide.with(context)
.load(imgUrl)
.apply(RequestOptions.placeholderOf(R.drawable.welcome))
.apply(RequestOptions.errorOf(R.drawable.welcome))
.into(target);
// 此方法始终返回 null可能需要修改
return null;
}
// 使用 Glide 加载图片到 ImageView
public static void setImgWithGlide(Context context, String imgUrl, ImageView view) {
// 使用 Glide 加载图片,设置占位符和错误图片
Glide.with(context)
.load(imgUrl)
.apply(RequestOptions.placeholderOf(R.drawable.welcome))
.apply(RequestOptions.errorOf(R.drawable.love))
.into(view);
}
// 根据歌手信息加载歌手图片到 ImageView
public static void setSingerImg(Context context, String singer, ImageView view) {
if (singer.contains("/")) {
// 处理歌手名字中包含斜杠的情况
String[] s = singer.split("/");
singer = s[0];
}
singer = singer.trim();
// 构建歌手图片的 URL
String imgUrl = Api.STORAGE_IMG_FILE + singer + ".jpg";
// 使用 Glide 加载图片,设置占位符和错误图片
Glide.with(context)
.load(imgUrl)
.apply(RequestOptions.placeholderOf(R.drawable.welcome))
.apply(RequestOptions.errorOf(R.drawable.welcome))
.into(view);
}
}