Created
December 30, 2015 13:13
-
-
Save ipcjs/a685917ea217b4ce9364 to your computer and use it in GitHub Desktop.
hack edge effect
This file contains hidden or 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.celink.common.ui; | |
import android.content.Context; | |
import android.graphics.PorterDuff; | |
import android.graphics.drawable.Drawable; | |
import android.support.v4.content.ContextCompat; | |
import com.celink.common.util.SystemUtil; | |
import com.googfit.util.L; | |
import com.googfit.util.ThemeUtil; | |
/** | |
* 通过改变Drawable的缓存状态, 实现改变所有图片的效果... | |
* Created by JiangSong on 2015/12/30. | |
*/ | |
public class DrawableColorHacker { | |
private static DrawableColorHacker sEdgeHacker; | |
private static DrawableColorHacker sGlowHacker; | |
public static void hackEdgeEffect(Context context) { | |
if (!SystemUtil.api21()) { | |
try { | |
if (sEdgeHacker == null) { | |
sEdgeHacker = new DrawableColorHacker(context.getResources().getIdentifier("overscroll_edge", "drawable", "android")); | |
sGlowHacker = new DrawableColorHacker(context.getResources().getIdentifier("overscroll_glow", "drawable", "android")); | |
} | |
sEdgeHacker.update(context, ThemeUtil.getThemeSkinColor(context)); | |
sGlowHacker.update(context, ThemeUtil.getThemeSkinColor(context)); | |
} catch (Exception e) {// 为防意外, try一下 | |
e.printStackTrace(); | |
} | |
} | |
} | |
private int mId; | |
private int mColor; | |
private Drawable.ConstantState mState; | |
public DrawableColorHacker(int id) { | |
mId = id; | |
} | |
public void update(Context context, int color) { | |
Drawable drawable = ContextCompat.getDrawable(context, mId); | |
Drawable.ConstantState constantState = drawable.getConstantState(); | |
if (color != mColor || constantState != mState) {// 颜色/状态一个改变了, 则要更新ColorFilter | |
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP); | |
mColor = color; | |
mState = drawable.getConstantState(); | |
} else { | |
L.p("颜色和状态都没改变不需要更新ColorFilter"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment