Last active
August 29, 2015 13:57
-
-
Save 75py/9707445 to your computer and use it in GitHub Desktop.
アプリアイコン読み込み時、画像がでかい場合は縮小するXposedモジュール。 ダウンロード: http://ux.getuploader.com/75py/download/34/XIconSizeFixer.apk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nagopy.android.xposed.iconsizefixer; | |
import android.content.pm.PackageItemInfo; | |
import android.content.pm.PackageManager; | |
import android.content.res.Resources; | |
import android.content.res.XModuleResources; | |
import android.graphics.Bitmap; | |
import android.graphics.Matrix; | |
import android.graphics.drawable.BitmapDrawable; | |
import android.graphics.drawable.Drawable; | |
import de.robv.android.xposed.IXposedHookZygoteInit; | |
import de.robv.android.xposed.XC_MethodHook; | |
import de.robv.android.xposed.XC_MethodReplacement; | |
import de.robv.android.xposed.XposedBridge; | |
import de.robv.android.xposed.XposedHelpers; | |
/** | |
* Created by nagopy on 14/03/22. | |
*/ | |
public class ModIconSizeFixer implements IXposedHookZygoteInit { | |
private static final String TAG = "ModIconSizeFixer"; | |
@Override | |
public void initZygote(StartupParam startupParam) throws Throwable { | |
// log("hook start!"); | |
final Resources moduleResources = XModuleResources.createInstance(startupParam.modulePath, null); | |
final int ICON_SIZE = moduleResources.getDimensionPixelSize(R.dimen.app_icon_size); | |
XposedHelpers.findAndHookMethod(PackageItemInfo.class, "loadIcon", PackageManager.class, new XC_MethodReplacement() { | |
@Override | |
protected Object replaceHookedMethod(MethodHookParam methodHookParam) throws Throwable { | |
// log("loadIcon"); | |
Drawable original = (Drawable) invokeOriginalMethod(methodHookParam); | |
try { | |
if (original.getIntrinsicWidth() > ICON_SIZE || original.getIntrinsicHeight() > ICON_SIZE) { | |
// resize | |
// log("resize"); | |
return toDrawable(moduleResources, resizeBitmap(toBitmap(original), ICON_SIZE, ICON_SIZE)); | |
} else { | |
// original | |
// log("original"); | |
return original; | |
} | |
} catch (Throwable t) { | |
XposedBridge.log(t); | |
// log("error:" + t); | |
return original; | |
} | |
} | |
} | |
); | |
// log("hook success!"); | |
} | |
/** | |
* invoke original method. | |
* | |
* @param param {@link de.robv.android.xposed.XC_MethodHook.MethodHookParam} | |
* @return オリジナルのメソッドの戻り値 | |
*/ | |
public static Object invokeOriginalMethod(XC_MethodHook.MethodHookParam param) throws Exception { | |
return XposedBridge.invokeOriginalMethod(param.method, param.thisObject, param.args); | |
} | |
/** | |
* Bitmap -> Drawable | |
* | |
* @param bitmap original bitmap | |
* @return drawable | |
*/ | |
public static Drawable toDrawable(Resources res, Bitmap bitmap) { | |
Drawable drawable = new BitmapDrawable(res, bitmap); | |
return drawable; | |
} | |
/** | |
* Bitmap -> Drawable | |
* | |
* @param bitmap original bitmap | |
* @return drawable | |
*/ | |
public static Drawable toDrawable(Bitmap bitmap) { | |
return toDrawable(null, bitmap); | |
} | |
/** | |
* Drawable -> Bitmap | |
* | |
* @param drawable original drawable | |
* @return bitmap | |
*/ | |
public static Bitmap toBitmap(Drawable drawable) { | |
return ((BitmapDrawable) drawable).getBitmap(); | |
} | |
/** | |
* resize image. | |
* | |
* @param originalBitmap original bitmap | |
* @param newWidth width | |
* @param newHeight height | |
* @return resized bitmap | |
*/ | |
public static Bitmap resizeBitmap(Bitmap originalBitmap, int newWidth, int newHeight) { | |
int originalBitmapWidth = originalBitmap.getWidth(); | |
int originalBitmapHeight = originalBitmap.getHeight(); | |
Matrix matrix = new Matrix(); | |
matrix.postScale(((float) newWidth) / originalBitmapWidth, ((float) newHeight) / originalBitmapHeight); | |
Bitmap resizedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmapWidth, originalBitmapHeight, matrix, false); | |
return resizedBitmap; | |
} | |
/** | |
* print debug messages | |
* | |
* @param obj target object | |
*/ | |
private static void log(Object obj) { | |
if (obj == null) { | |
obj = "null"; | |
} | |
XposedBridge.log(TAG + ": " + obj); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment