diff --git a/switchicon/src/main/java/com/github/zagum/switchicon/ReflectionUtils.java b/switchicon/src/main/java/com/github/zagum/switchicon/ReflectionUtils.java deleted file mode 100644 index 2160b39..0000000 --- a/switchicon/src/main/java/com/github/zagum/switchicon/ReflectionUtils.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.github.zagum.switchicon; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; - -public class ReflectionUtils { - public static boolean setValue(Object object, String fieldName, Object fieldValue) { - Class clazz = object.getClass(); - while (clazz != null) { - try { - Field field = clazz.getDeclaredField(fieldName); - field.setAccessible(true); - field.set(object, fieldValue); - return true; - } catch (NoSuchFieldException e) { - clazz = clazz.getSuperclass(); - } catch (Exception e) { - throw new IllegalStateException(e); - } - } - return false; - } - - public static boolean callMethod(Object object, String methodName, Class... params) { - Class clazz = object.getClass(); - while (clazz != null) { - try { - Method method = clazz.getDeclaredMethod(methodName, params); - method.setAccessible(true); - method.invoke(object, null); - return true; - } catch (NoSuchMethodException e) { - clazz = clazz.getSuperclass(); - } catch (Exception e) { - throw new IllegalStateException(e); - } - } - return false; - } -} diff --git a/switchicon/src/main/java/com/github/zagum/switchicon/SwitchIconView.java b/switchicon/src/main/java/com/github/zagum/switchicon/SwitchIconView.java index 8e4f7f5..df0ad8e 100644 --- a/switchicon/src/main/java/com/github/zagum/switchicon/SwitchIconView.java +++ b/switchicon/src/main/java/com/github/zagum/switchicon/SwitchIconView.java @@ -266,23 +266,22 @@ private void updateColor(float fraction) { private void updateAlpha(float fraction) { int alpha = (int) ((disabledStateAlpha + (1f - fraction) * (1f - disabledStateAlpha)) * 255); - updateImageAlphaWithoutInvalidate(alpha); + updateImageAlpha(alpha); dashPaint.setAlpha(alpha); } - private void updateImageAlphaWithoutInvalidate(int alpha) { - alpha &= 0xFF; - ReflectionUtils.setValue(this, "mAlpha", alpha); - ReflectionUtils.setValue(this, "mColorMod", true); - Class noParams[] = {}; - ReflectionUtils.callMethod(this, "applyColorMod", noParams); - } - private void updateImageColor(int color) { colorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN); - ReflectionUtils.setValue(this, "mColorFilter", colorFilter); - Class noParams[] = {}; - ReflectionUtils.callMethod(this, "applyColorMod", noParams); + setColorFilter(colorFilter); + } + + @SuppressWarnings("deprecation") + private void updateImageAlpha(int alpha) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + setImageAlpha(alpha); + } else { + setAlpha(alpha); + } } private void postInvalidateOnAnimationCompat() {