refactor(deps): 改用私有仓库在线依赖
- 增加 Gitea Maven 仓库,令牌取自 config.gradle - niceimageview、xcrash、ultimatebarx、iconloader 改为在线依赖 - 删除本地 niceimageview、iconloader 模块 - 初始化后显式预热 launcher 图标
This commit is contained in:
1 parent
8f92238497
commit
b850d0e65b
24 files changed
+20
-877
No files matched your search
+5
-5
@@ -217,7 +217,6 @@ ext {
|
||||
dependencies {
|
||||
// implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
compileOnly files('libs/framework.jar')
|
||||
implementation files('libs/xcrash-android-lib-3.1.0.aar')
|
||||
|
||||
//Android 4.4+
|
||||
implementation files('libs/QWeather_Public_Android_V5.1.2.jar')
|
||||
@@ -226,8 +225,7 @@ dependencies {
|
||||
//sherpa-onnx https://github.com/k2-fsa/sherpa-onnx/releases
|
||||
implementation files('libs/sherpa-onnx-2.0.1.aar')
|
||||
|
||||
implementation project(path: ':niceimageview')
|
||||
implementation project(path: ':iconloader')
|
||||
implementation 'com.ttstd:iconloader:1.0.2'
|
||||
|
||||
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.5'
|
||||
|
||||
@@ -320,6 +318,7 @@ dependencies {
|
||||
implementation 'com.tencent:mmkv-static:2.4.3'
|
||||
//bugly
|
||||
implementation 'com.tencent.bugly:crashreport:4.1.9.7'
|
||||
implementation 'com.iqiyi.xcrash:xcrash-android-lib:3.1.0'
|
||||
|
||||
// 替换成最新版本, 需要注意的是api
|
||||
// 要与compiler匹配使用,均使用最新版可以保证兼容
|
||||
@@ -376,8 +375,9 @@ dependencies {
|
||||
implementation 'com.arialyy.aria:core:3.8.15'
|
||||
kapt 'com.arialyy.aria:compiler:3.8.15'
|
||||
//状态栏透明
|
||||
// UltimateBarX-Java :ultimatebarx:assembleRelease 产物;本地 AAR 不携带传递依赖。
|
||||
implementation files('libs/ultimatebarx-0.8.1-release.aar')
|
||||
implementation 'com.zackratos.ultimatebarx:ultimatebarx:0.8.1'
|
||||
//以后替换为ShapeableImageView
|
||||
implementation 'com.shehuan:niceimageview:1.0.5'
|
||||
implementation 'com.opencsv:opencsv:5.12.0'
|
||||
|
||||
// 权限请求框架:https://github.com/getActivity/XXPermissions
|
||||
|
||||
@@ -136,6 +136,7 @@ public class BaseApplication extends Application {
|
||||
|
||||
WeatherManager.init(this);
|
||||
IconCacheManager.init(this);
|
||||
IconCacheManager.getInstance().warmUpLauncherIcons();
|
||||
|
||||
// 初始化 MQTT 并连接 Broker
|
||||
MqttManager.init(this);
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import org.gradle.api.credentials.HttpHeaderCredentials
|
||||
import org.gradle.authentication.http.HttpHeaderAuthentication
|
||||
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
apply from: "config.gradle"
|
||||
apply from: "gradle/screenmatch.gradle"
|
||||
@@ -39,6 +42,16 @@ allprojects {
|
||||
// }
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
url = uri('https://gitea.ttstd.com/api/packages/maven/maven')
|
||||
credentials(HttpHeaderCredentials) {
|
||||
name = 'Authorization'
|
||||
value = "token ${rootProject.ext.giteaToken}"
|
||||
}
|
||||
authentication {
|
||||
header(HttpHeaderAuthentication)
|
||||
}
|
||||
}
|
||||
google()
|
||||
mavenCentral()
|
||||
maven { url = 'https://jitpack.io' }
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
/build
|
||||
@@ -1,41 +0,0 @@
|
||||
apply plugin: 'com.android.library'
|
||||
|
||||
android {
|
||||
namespace = "com.ttstd.iconloader"
|
||||
|
||||
compileSdk = 36
|
||||
// buildToolsVersion "36.0.0"
|
||||
|
||||
defaultConfig {
|
||||
minSdk = 23
|
||||
targetSdk = 36
|
||||
versionCode = 1
|
||||
versionName = "1.0"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles 'consumer-rules.pro'
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
zhanRuiDebug {}
|
||||
zhanRuiRelease {}
|
||||
CrosshatchDebug {}
|
||||
CrosshatchRelease {}
|
||||
debug {}
|
||||
release {}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
|
||||
implementation 'androidx.appcompat:appcompat:1.8.0'
|
||||
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.3.0'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.7.0'
|
||||
|
||||
//磁盘缓存
|
||||
implementation 'com.jakewharton:disklrucache:2.0.2'
|
||||
}
|
||||
Whitespace-only changes.
Vendored
-21
@@ -1,21 +0,0 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.ttstd.iconloader;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import androidx.test.platform.app.InstrumentationRegistry;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ExampleInstrumentedTest {
|
||||
@Test
|
||||
public void useAppContext() {
|
||||
// Context of the app under test.
|
||||
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
|
||||
|
||||
assertEquals("com.ttstd.iconloader.test", appContext.getPackageName());
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
<manifest />
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.ttstd.iconloader;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Intent;
|
||||
|
||||
public class CacheKeyGenerator {
|
||||
// 生成ComponentName的标准字符串格式(包名/类名)
|
||||
public static String generateKey(ComponentName componentName) {
|
||||
return componentName.flattenToShortString(); // 如:com.example.app/.MainActivity
|
||||
}
|
||||
|
||||
// 从Intent中提取ComponentName作为键
|
||||
public static String generateKey(Intent intent) {
|
||||
return generateKey(intent.getComponent());
|
||||
}
|
||||
}
|
||||
@@ -1,180 +0,0 @@
|
||||
package com.ttstd.iconloader;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.AdaptiveIconDrawable;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.collection.LruCache;
|
||||
|
||||
import com.jakewharton.disklrucache.DiskLruCache;
|
||||
import com.ttstd.iconloader.utils.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class IconCacheManager {
|
||||
private static IconCacheManager INSTANCE;
|
||||
|
||||
private static final String TAG = "IconCacheManager";
|
||||
private LruCache<String, Drawable> mMemoryCache;
|
||||
private DiskLruCache mDiskCache;
|
||||
|
||||
public static void init(Context context) {
|
||||
if (context == null) {
|
||||
throw new RuntimeException("Context is NULL");
|
||||
}
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new IconCacheManager(context);
|
||||
}
|
||||
}
|
||||
|
||||
public static IconCacheManager getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
throw new IllegalStateException("You must first initialize the IconCacheManager");
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public IconCacheManager(Context context) {
|
||||
// 内存缓存(最大8MB)
|
||||
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
|
||||
int cacheSize = maxMemory / 8;
|
||||
mMemoryCache = new LruCache<String, Drawable>(cacheSize) {
|
||||
@Override
|
||||
protected int sizeOf(String key, Drawable icon) {
|
||||
if (icon instanceof BitmapDrawable) {
|
||||
return ((BitmapDrawable) icon).getBitmap().getByteCount() / 1024;
|
||||
}
|
||||
return 1; // 非Bitmap对象按1KB计算
|
||||
}
|
||||
};
|
||||
|
||||
// 磁盘缓存(50MB)
|
||||
File cacheDir = new File(FileUtils.getCacheDir(context), "icon_cache");
|
||||
try {
|
||||
mDiskCache = DiskLruCache.open(cacheDir, 1, 1, 50 * 1024 * 1024);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "IconCacheManager: " + e.getMessage());
|
||||
}
|
||||
|
||||
final PackageManager packageManager = context.getPackageManager();
|
||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||
intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
||||
// 查询所有包含CATEGORY_LAUNCHER的Activity
|
||||
List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, PackageManager.MATCH_ALL);
|
||||
for (ResolveInfo resolveInfo : resolveInfoList) {
|
||||
ComponentName componentName = new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
|
||||
String key = componentName.flattenToShortString();
|
||||
try {
|
||||
ActivityInfo info = packageManager.getActivityInfo(componentName, 0);
|
||||
Drawable rawIcon = info.loadIcon(packageManager);
|
||||
putToMemory(key, rawIcon);
|
||||
putToDisk(key, rawIcon);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "run: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Drawable getFromMemory(String key) {
|
||||
return mMemoryCache.get(key);
|
||||
}
|
||||
|
||||
public Drawable getFromDisk(String key) {
|
||||
String md5 = MD5Util.getMD5(key);
|
||||
try {
|
||||
DiskLruCache.Snapshot snapshot = mDiskCache.get(md5);
|
||||
if (snapshot != null) {
|
||||
InputStream in = snapshot.getInputStream(0);
|
||||
return Drawable.createFromStream(in, null);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "getFromDisk Disk read error", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void putToMemory(String key, Drawable icon) {
|
||||
mMemoryCache.put(key, icon);
|
||||
}
|
||||
|
||||
public void putToDisk(String key, Drawable icon) {
|
||||
String md5 = MD5Util.getMD5(key);
|
||||
try {
|
||||
DiskLruCache.Editor editor = mDiskCache.edit(md5);
|
||||
if (icon instanceof BitmapDrawable) {
|
||||
Bitmap bitmap = ((BitmapDrawable) icon).getBitmap();
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, editor.newOutputStream(0));
|
||||
editor.commit();
|
||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
if (icon instanceof AdaptiveIconDrawable) {
|
||||
// 分别获取背景和前景Drawable
|
||||
// Drawable[] layers = new Drawable[2];
|
||||
// layers[0] = ((AdaptiveIconDrawable) icon).getBackground();
|
||||
// layers[1] = ((AdaptiveIconDrawable) icon).getForeground();
|
||||
// LayerDrawable layerDrawable = new LayerDrawable(layers);
|
||||
//
|
||||
// int width = icon.getIntrinsicWidth();
|
||||
// int height = icon.getIntrinsicHeight();
|
||||
//
|
||||
// Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
// Canvas canvas = new Canvas(bitmap);
|
||||
//
|
||||
// layerDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
// layerDrawable.draw(canvas);
|
||||
|
||||
// 创建一个大小与原始Drawable相同的位图
|
||||
Bitmap bitmap = Bitmap.createBitmap(
|
||||
icon.getIntrinsicWidth(),
|
||||
icon.getIntrinsicHeight(),
|
||||
Bitmap.Config.ARGB_8888
|
||||
);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
// 关键步骤:设置Drawable的绘制边界
|
||||
icon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
icon.draw(canvas);
|
||||
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, editor.newOutputStream(0));
|
||||
editor.commit();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "putToDisk Disk write error", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Drawable getIcon(String key) {
|
||||
Drawable memDrawable = getFromMemory(key);
|
||||
if (memDrawable != null) {
|
||||
Log.i(TAG, "getIcon: fromMemory " + key);
|
||||
return memDrawable;
|
||||
}
|
||||
Drawable diskDrawable = getFromDisk(key);
|
||||
if (diskDrawable != null) {
|
||||
putToMemory(key, diskDrawable);
|
||||
Log.i(TAG, "getIcon: fromDisk " + key);
|
||||
return diskDrawable;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
package com.ttstd.iconloader;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.drawable.AdaptiveIconDrawable;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.loader.content.AsyncTaskLoader;
|
||||
|
||||
public class IconLoader extends AsyncTaskLoader<Drawable> {
|
||||
private static final String TAG = "IconLoader";
|
||||
private final IconCacheManager mCache;
|
||||
private ComponentName mComponentName;
|
||||
|
||||
public IconLoader(Context context, ComponentName componentName, IconCacheManager iconCacheManager) {
|
||||
super(context);
|
||||
this.mComponentName = componentName;
|
||||
this.mCache = iconCacheManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStartLoading() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Drawable loadInBackground() {
|
||||
String key = CacheKeyGenerator.generateKey(mComponentName);
|
||||
|
||||
// 1. 检查内存缓存
|
||||
Drawable cachedIcon = mCache.getFromMemory(key);
|
||||
if (cachedIcon != null) {
|
||||
return cachedIcon;
|
||||
}
|
||||
|
||||
// 2. 检查磁盘缓存
|
||||
Drawable diskCached = mCache.getFromDisk(key);
|
||||
if (diskCached != null) {
|
||||
mCache.putToMemory(key, diskCached);
|
||||
Log.e(TAG, "loadInBackground: putToMemory key = " + key);
|
||||
return diskCached;
|
||||
}
|
||||
|
||||
// 3. 从PackageManager加载组件专属图标
|
||||
try {
|
||||
PackageManager pm = getContext().getPackageManager();
|
||||
ActivityInfo info = pm.getActivityInfo(mComponentName, 0); // 获取ActivityInfo
|
||||
Drawable rawIcon = info.loadIcon(pm); // 加载该组件的图标
|
||||
|
||||
// 4. 图标处理(压缩/主题适配)
|
||||
// Drawable processedIcon = processIcon(rawIcon);
|
||||
|
||||
// 5. 更新缓存
|
||||
mCache.putToMemory(key, rawIcon);
|
||||
mCache.putToDisk(key, rawIcon);
|
||||
return rawIcon;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Drawable processIcon(Drawable icon) {
|
||||
// 自适应图标处理(Android 8.0+)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
|
||||
&& icon instanceof AdaptiveIconDrawable) {
|
||||
AdaptiveIconDrawable adaptiveIcon = (AdaptiveIconDrawable) icon;
|
||||
int targetColor = isDarkMode() ? Color.WHITE : Color.BLACK;
|
||||
adaptiveIcon.setColorFilter(targetColor, PorterDuff.Mode.SRC_ATOP); // 根据主题切换单色模式
|
||||
}
|
||||
|
||||
// 图标尺寸压缩(避免内存溢出)
|
||||
if (icon instanceof BitmapDrawable) {
|
||||
Bitmap bitmap = ((BitmapDrawable) icon).getBitmap();
|
||||
int scaledWidth = bitmap.getWidth() / 2;
|
||||
int scaledHeight = bitmap.getHeight() / 2;
|
||||
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
|
||||
return new BitmapDrawable(getContext().getResources(), scaledBitmap);
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
private boolean isDarkMode() {
|
||||
return (getContext().getResources().getConfiguration().uiMode
|
||||
& Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package com.ttstd.iconloader;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class MD5Util {
|
||||
public static String getMD5(String input) {
|
||||
try {
|
||||
// 1. 获取 MD5 消息摘要实例
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
// 2. 将输入字符串转换为字节数组。建议指定编码(如UTF-8),但使用默认平台编码时通常可省略。
|
||||
byte[] inputBytes = input.getBytes(); // 如需指定编码,可使用 input.getBytes("UTF-8")
|
||||
// 3. 计算消息摘要,得到128位(16字节)的哈希值
|
||||
byte[] digest = md.digest(inputBytes);
|
||||
// 4. 将字节数组转换为十六进制字符串表示
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : digest) {
|
||||
// 每个字节转换为两位十六进制数,不足两位前面补零
|
||||
sb.append(String.format("%02x", b & 0xff));
|
||||
}
|
||||
// 5. 返回最终的32位十六进制字符串
|
||||
return sb.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// MD5算法是所有Java标准平台必须支持的,此异常通常不会抛出
|
||||
throw new RuntimeException("MD5 algorithm not available", e);
|
||||
}
|
||||
// 若指定了编码(如UTF-8),需捕获 UnsupportedEncodingException,但UTF-8通常也受支持
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.ttstd.iconloader.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
import java.io.File;
|
||||
|
||||
public class FileUtils {
|
||||
/**
|
||||
* 获取缓存目录
|
||||
* 优先返回外部缓存目录,如果不可用则返回内部缓存目录
|
||||
*/
|
||||
public static String getCacheDir(Context context) {
|
||||
File cacheDir = null;
|
||||
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|
||||
|| !Environment.isExternalStorageRemovable()) {
|
||||
cacheDir = context.getExternalCacheDir();
|
||||
}
|
||||
if (cacheDir == null) {
|
||||
cacheDir = context.getCacheDir();
|
||||
}
|
||||
return cacheDir.getPath();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.ttstd.iconloader;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
/build
|
||||
@@ -1,28 +0,0 @@
|
||||
apply plugin: 'com.android.library'
|
||||
|
||||
android {
|
||||
namespace = "com.shehuan.niv"
|
||||
|
||||
compileSdk = 36
|
||||
|
||||
defaultConfig {
|
||||
minSdk = 23
|
||||
targetSdk = 28
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
zhanRuiDebug {}
|
||||
zhanRuiRelease {}
|
||||
CrosshatchDebug {}
|
||||
CrosshatchRelease {}
|
||||
debug {}
|
||||
release {}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
|
||||
implementation 'androidx.appcompat:appcompat:1.8.0'
|
||||
}
|
||||
Vendored
-21
@@ -1,21 +0,0 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For menu details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
@@ -1 +0,0 @@
|
||||
<manifest />
|
||||
@@ -1,338 +0,0 @@
|
||||
package com.shehuan.niv;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Path;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.PorterDuffXfermode;
|
||||
import android.graphics.RectF;
|
||||
import android.graphics.Xfermode;
|
||||
import android.os.Build;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.annotation.ColorInt;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.AppCompatImageView;
|
||||
|
||||
public class NiceImageView extends AppCompatImageView {
|
||||
private Context context;
|
||||
|
||||
private boolean isCircle; // 是否显示为圆形,如果为圆形则设置的corner无效
|
||||
private boolean isCoverSrc; // border、inner_border是否覆盖图片
|
||||
private int borderWidth; // 边框宽度
|
||||
private int borderColor = Color.WHITE; // 边框颜色
|
||||
private int innerBorderWidth; // 内层边框宽度
|
||||
private int innerBorderColor = Color.WHITE; // 内层边框充色
|
||||
|
||||
private int cornerRadius; // 统一设置圆角半径,优先级高于单独设置每个角的半径
|
||||
private int cornerTopLeftRadius; // 左上角圆角半径
|
||||
private int cornerTopRightRadius; // 右上角圆角半径
|
||||
private int cornerBottomLeftRadius; // 左下角圆角半径
|
||||
private int cornerBottomRightRadius; // 右下角圆角半径
|
||||
|
||||
private int maskColor; // 遮罩颜色
|
||||
|
||||
private Xfermode xfermode;
|
||||
|
||||
private int width;
|
||||
private int height;
|
||||
private float radius;
|
||||
|
||||
private float[] borderRadii;
|
||||
private float[] srcRadii;
|
||||
|
||||
private RectF srcRectF; // 图片占的矩形区域
|
||||
private RectF borderRectF; // 边框的矩形区域
|
||||
|
||||
private Paint paint;
|
||||
private Path path; // 用来裁剪图片的ptah
|
||||
private Path srcPath; // 图片区域大小的path
|
||||
|
||||
public NiceImageView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public NiceImageView(Context context, @Nullable AttributeSet attrs) {
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
public NiceImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
|
||||
this.context = context;
|
||||
|
||||
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.NiceImageView, 0, 0);
|
||||
for (int i = 0; i < ta.getIndexCount(); i++) {
|
||||
int attr = ta.getIndex(i);
|
||||
if (attr == R.styleable.NiceImageView_is_cover_src) {
|
||||
isCoverSrc = ta.getBoolean(attr, isCoverSrc);
|
||||
} else if (attr == R.styleable.NiceImageView_is_circle) {
|
||||
isCircle = ta.getBoolean(attr, isCircle);
|
||||
} else if (attr == R.styleable.NiceImageView_border_width) {
|
||||
borderWidth = ta.getDimensionPixelSize(attr, borderWidth);
|
||||
} else if (attr == R.styleable.NiceImageView_border_color) {
|
||||
borderColor = ta.getColor(attr, borderColor);
|
||||
} else if (attr == R.styleable.NiceImageView_inner_border_width) {
|
||||
innerBorderWidth = ta.getDimensionPixelSize(attr, innerBorderWidth);
|
||||
} else if (attr == R.styleable.NiceImageView_inner_border_color) {
|
||||
innerBorderColor = ta.getColor(attr, innerBorderColor);
|
||||
} else if (attr == R.styleable.NiceImageView_corner_radius) {
|
||||
cornerRadius = ta.getDimensionPixelSize(attr, cornerRadius);
|
||||
} else if (attr == R.styleable.NiceImageView_corner_top_left_radius) {
|
||||
cornerTopLeftRadius = ta.getDimensionPixelSize(attr, cornerTopLeftRadius);
|
||||
} else if (attr == R.styleable.NiceImageView_corner_top_right_radius) {
|
||||
cornerTopRightRadius = ta.getDimensionPixelSize(attr, cornerTopRightRadius);
|
||||
} else if (attr == R.styleable.NiceImageView_corner_bottom_left_radius) {
|
||||
cornerBottomLeftRadius = ta.getDimensionPixelSize(attr, cornerBottomLeftRadius);
|
||||
} else if (attr == R.styleable.NiceImageView_corner_bottom_right_radius) {
|
||||
cornerBottomRightRadius = ta.getDimensionPixelSize(attr, cornerBottomRightRadius);
|
||||
} else if (attr == R.styleable.NiceImageView_mask_color) {
|
||||
maskColor = ta.getColor(attr, maskColor);
|
||||
}
|
||||
}
|
||||
ta.recycle();
|
||||
|
||||
borderRadii = new float[8];
|
||||
srcRadii = new float[8];
|
||||
|
||||
borderRectF = new RectF();
|
||||
srcRectF = new RectF();
|
||||
|
||||
paint = new Paint();
|
||||
path = new Path();
|
||||
|
||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
|
||||
xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
|
||||
} else {
|
||||
xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);
|
||||
srcPath = new Path();
|
||||
}
|
||||
|
||||
calculateRadii();
|
||||
clearInnerBorderWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
super.onSizeChanged(w, h, oldw, oldh);
|
||||
width = w;
|
||||
height = h;
|
||||
|
||||
initBorderRectF();
|
||||
initSrcRectF();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
// 使用图形混合模式来显示指定区域的图片
|
||||
canvas.saveLayer(srcRectF, null, Canvas.ALL_SAVE_FLAG);
|
||||
if (!isCoverSrc) {
|
||||
float sx = 1.0f * (width - 2 * borderWidth - 2 * innerBorderWidth) / width;
|
||||
float sy = 1.0f * (height - 2 * borderWidth - 2 * innerBorderWidth) / height;
|
||||
// 缩小画布,使图片内容不被borders覆盖
|
||||
canvas.scale(sx, sy, width / 2.0f, height / 2.0f);
|
||||
}
|
||||
super.onDraw(canvas);
|
||||
paint.reset();
|
||||
path.reset();
|
||||
if (isCircle) {
|
||||
path.addCircle(width / 2.0f, height / 2.0f, radius, Path.Direction.CCW);
|
||||
} else {
|
||||
path.addRoundRect(srcRectF, srcRadii, Path.Direction.CCW);
|
||||
}
|
||||
|
||||
paint.setAntiAlias(true);
|
||||
paint.setStyle(Paint.Style.FILL);
|
||||
paint.setXfermode(xfermode);
|
||||
//9.0及以上系统图片一次圆一次方的解决办法
|
||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
|
||||
canvas.drawPath(path, paint);
|
||||
} else {
|
||||
srcPath.addRect(srcRectF, Path.Direction.CCW);
|
||||
// 计算tempPath和path的差集
|
||||
srcPath.op(path, Path.Op.DIFFERENCE);
|
||||
canvas.drawPath(srcPath, paint);
|
||||
srcPath.reset();//1
|
||||
}
|
||||
paint.setXfermode(null);
|
||||
|
||||
// 绘制遮罩
|
||||
if (maskColor != 0) {
|
||||
paint.setColor(maskColor);
|
||||
canvas.drawPath(path, paint);
|
||||
}
|
||||
// 恢复画布
|
||||
canvas.restore();
|
||||
// 绘制边框
|
||||
drawBorders(canvas);
|
||||
}
|
||||
|
||||
private void drawBorders(Canvas canvas) {
|
||||
if (isCircle) {
|
||||
if (borderWidth > 0) {
|
||||
drawCircleBorder(canvas, borderWidth, borderColor, radius - borderWidth / 2.0f);
|
||||
}
|
||||
if (innerBorderWidth > 0) {
|
||||
drawCircleBorder(canvas, innerBorderWidth, innerBorderColor, radius - borderWidth - innerBorderWidth / 2.0f);
|
||||
}
|
||||
} else {
|
||||
if (borderWidth > 0) {
|
||||
drawRectFBorder(canvas, borderWidth, borderColor, borderRectF, borderRadii);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drawCircleBorder(Canvas canvas, int borderWidth, int borderColor, float radius) {
|
||||
initBorderPaint(borderWidth, borderColor);
|
||||
path.addCircle(width / 2.0f, height / 2.0f, radius, Path.Direction.CCW);
|
||||
canvas.drawPath(path, paint);
|
||||
}
|
||||
|
||||
private void drawRectFBorder(Canvas canvas, int borderWidth, int borderColor, RectF rectF, float[] radii) {
|
||||
initBorderPaint(borderWidth, borderColor);
|
||||
path.addRoundRect(rectF, radii, Path.Direction.CCW);
|
||||
canvas.drawPath(path, paint);
|
||||
}
|
||||
|
||||
private void initBorderPaint(int borderWidth, int borderColor) {
|
||||
path.reset();
|
||||
paint.setStrokeWidth(borderWidth);
|
||||
paint.setColor(borderColor);
|
||||
paint.setStyle(Paint.Style.STROKE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算外边框的RectF
|
||||
*/
|
||||
private void initBorderRectF() {
|
||||
if (!isCircle) {
|
||||
borderRectF.set(borderWidth / 2.0f, borderWidth / 2.0f, width - borderWidth / 2.0f, height - borderWidth / 2.0f);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算图片原始区域的RectF
|
||||
*/
|
||||
private void initSrcRectF() {
|
||||
if (isCircle) {
|
||||
radius = Math.min(width, height) / 2.0f;
|
||||
srcRectF.set(width / 2.0f - radius, height / 2.0f - radius, width / 2.0f + radius, height / 2.0f + radius);
|
||||
} else {
|
||||
srcRectF.set(0, 0, width, height);
|
||||
if (isCoverSrc) {
|
||||
srcRectF = borderRectF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算RectF的圆角半径
|
||||
*/
|
||||
private void calculateRadii() {
|
||||
if (isCircle) {
|
||||
return;
|
||||
}
|
||||
if (cornerRadius > 0) {
|
||||
for (int i = 0; i < borderRadii.length; i++) {
|
||||
borderRadii[i] = cornerRadius;
|
||||
srcRadii[i] = cornerRadius - borderWidth / 2.0f;
|
||||
}
|
||||
} else {
|
||||
borderRadii[0] = borderRadii[1] = cornerTopLeftRadius;
|
||||
borderRadii[2] = borderRadii[3] = cornerTopRightRadius;
|
||||
borderRadii[4] = borderRadii[5] = cornerBottomRightRadius;
|
||||
borderRadii[6] = borderRadii[7] = cornerBottomLeftRadius;
|
||||
|
||||
srcRadii[0] = srcRadii[1] = cornerTopLeftRadius - borderWidth / 2.0f;
|
||||
srcRadii[2] = srcRadii[3] = cornerTopRightRadius - borderWidth / 2.0f;
|
||||
srcRadii[4] = srcRadii[5] = cornerBottomRightRadius - borderWidth / 2.0f;
|
||||
srcRadii[6] = srcRadii[7] = cornerBottomLeftRadius - borderWidth / 2.0f;
|
||||
}
|
||||
}
|
||||
|
||||
private void calculateRadiiAndRectF(boolean reset) {
|
||||
if (reset) {
|
||||
cornerRadius = 0;
|
||||
}
|
||||
calculateRadii();
|
||||
initBorderRectF();
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 目前圆角矩形情况下不支持inner_border,需要将其置0
|
||||
*/
|
||||
private void clearInnerBorderWidth() {
|
||||
if (!isCircle) {
|
||||
this.innerBorderWidth = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void isCoverSrc(boolean isCoverSrc) {
|
||||
this.isCoverSrc = isCoverSrc;
|
||||
initSrcRectF();
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void isCircle(boolean isCircle) {
|
||||
this.isCircle = isCircle;
|
||||
clearInnerBorderWidth();
|
||||
initSrcRectF();
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void setBorderWidth(int borderWidth) {
|
||||
this.borderWidth = Utils.dp2px(context, borderWidth);
|
||||
calculateRadiiAndRectF(false);
|
||||
}
|
||||
|
||||
public void setBorderColor(@ColorInt int borderColor) {
|
||||
this.borderColor = borderColor;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void setInnerBorderWidth(int innerBorderWidth) {
|
||||
this.innerBorderWidth = Utils.dp2px(context, innerBorderWidth);
|
||||
clearInnerBorderWidth();
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void setInnerBorderColor(@ColorInt int innerBorderColor) {
|
||||
this.innerBorderColor = innerBorderColor;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void setCornerRadius(int cornerRadius) {
|
||||
this.cornerRadius = Utils.dp2px(context, cornerRadius);
|
||||
calculateRadiiAndRectF(false);
|
||||
}
|
||||
|
||||
public void setCornerTopLeftRadius(int cornerTopLeftRadius) {
|
||||
this.cornerTopLeftRadius = Utils.dp2px(context, cornerTopLeftRadius);
|
||||
calculateRadiiAndRectF(true);
|
||||
}
|
||||
|
||||
public void setCornerTopRightRadius(int cornerTopRightRadius) {
|
||||
this.cornerTopRightRadius = Utils.dp2px(context, cornerTopRightRadius);
|
||||
calculateRadiiAndRectF(true);
|
||||
}
|
||||
|
||||
public void setCornerBottomLeftRadius(int cornerBottomLeftRadius) {
|
||||
this.cornerBottomLeftRadius = Utils.dp2px(context, cornerBottomLeftRadius);
|
||||
calculateRadiiAndRectF(true);
|
||||
}
|
||||
|
||||
public void setCornerBottomRightRadius(int cornerBottomRightRadius) {
|
||||
this.cornerBottomRightRadius = Utils.dp2px(context, cornerBottomRightRadius);
|
||||
calculateRadiiAndRectF(true);
|
||||
}
|
||||
|
||||
public void setMaskColor(@ColorInt int maskColor) {
|
||||
this.maskColor = maskColor;
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.shehuan.niv;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public class Utils {
|
||||
public static int dp2px(Context context, float dipValue) {
|
||||
final float scale = context.getResources().getDisplayMetrics().density;
|
||||
return (int) (dipValue * scale + 0.5f);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<declare-styleable name="NiceImageView">
|
||||
<attr name="is_circle" format="boolean" />
|
||||
<attr name="is_cover_src" format="boolean" />
|
||||
<attr name="corner_radius" format="dimension" />
|
||||
<attr name="corner_top_left_radius" format="dimension" />
|
||||
<attr name="corner_top_right_radius" format="dimension" />
|
||||
<attr name="corner_bottom_left_radius" format="dimension" />
|
||||
<attr name="corner_bottom_right_radius" format="dimension" />
|
||||
<attr name="border_width" format="dimension" />
|
||||
<attr name="border_color" format="color" />
|
||||
<attr name="inner_border_width" format="dimension" />
|
||||
<attr name="inner_border_color" format="color" />
|
||||
<attr name="mask_color" format="color" />
|
||||
</declare-styleable>
|
||||
</resources>
|
||||
@@ -1,3 +0,0 @@
|
||||
<resources>
|
||||
<string name="app_name">niceimageview</string>
|
||||
</resources>
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
rootProject.name = '拨号助手'
|
||||
include ':app', ':niceimageview', ':iconloader'
|
||||
include ':app'
|
||||
Reference in new issue
Block a user