[6.2.4][View点击态] 优化代码逻辑
This commit is contained in:
@@ -32,6 +32,9 @@ public class ViewPressedStateLancet {
|
||||
@TargetMethod(methodName = "setOnClickListener")
|
||||
@ReplaceInvoke
|
||||
public static void setOnClickListener(View view, View.OnClickListener listener) {
|
||||
if (view == null) {
|
||||
return;
|
||||
}
|
||||
Object tag = view.getTag(R.id.click_pressed_attr_id);
|
||||
checkSetBgIfNeed(view, tag == null ? null : (AttributeSet) tag);
|
||||
view.setOnClickListener(listener);
|
||||
@@ -41,6 +44,9 @@ public class ViewPressedStateLancet {
|
||||
@TargetMethod(methodName = "setOnLongClickListener")
|
||||
@ReplaceInvoke
|
||||
public static void setOnLongClickListener(View view, View.OnLongClickListener listener) {
|
||||
if (view == null) {
|
||||
return;
|
||||
}
|
||||
Object tag = view.getTag(R.id.click_pressed_attr_id);
|
||||
checkSetBgIfNeed(view, tag == null ? null : (AttributeSet) tag);
|
||||
view.setOnLongClickListener(listener);
|
||||
@@ -86,8 +92,8 @@ public class ViewPressedStateLancet {
|
||||
return;
|
||||
}
|
||||
alpha = t.getFloat(R.styleable.ClickPressedStyle_pressed_alpha, 0.6f);
|
||||
if (alpha > 1.0f) {
|
||||
throw new AssertionError("view custom attr: alpha > 1.0f, must be in [0.0, 1.0]");
|
||||
if (alpha < 0.0f || alpha > 1.0f) {
|
||||
throw new AssertionError("view custom attr \\'alpha\\' must be in [0.0, 1.0]");
|
||||
}
|
||||
view.setTag(R.id.click_pressed_attr_id, attr);
|
||||
} finally {
|
||||
@@ -107,6 +113,7 @@ public class ViewPressedStateLancet {
|
||||
image.setImageDrawable(replaced);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (view instanceof TextView) {
|
||||
TextView text = (TextView) view;
|
||||
@@ -121,11 +128,38 @@ public class ViewPressedStateLancet {
|
||||
if (pressed != Integer.MIN_VALUE) {
|
||||
return;
|
||||
}
|
||||
int defaultColor = textColor.getDefaultColor();
|
||||
int color = textColor.getColorForState(new int[] { android.R.attr.state_enabled }, Integer.MIN_VALUE);
|
||||
int defaultColor = color;
|
||||
if (color == Integer.MIN_VALUE) {
|
||||
defaultColor = textColor.getDefaultColor();
|
||||
}
|
||||
int pressedColor = Color.argb((int)(Color.alpha(defaultColor) * alpha), Color.red(defaultColor), Color.green(defaultColor), Color.blue(defaultColor));
|
||||
ColorStateList newColor = new ColorStateList(new int[][] { new int[] { -android.R.attr.state_pressed }, new int[] { android.R.attr.state_pressed }}, new int[] { defaultColor, pressedColor });
|
||||
int disableColor = textColor.getColorForState(new int[] { -android.R.attr.state_enabled }, Integer.MIN_VALUE);
|
||||
int size = 2;
|
||||
if (disableColor != Integer.MIN_VALUE) {
|
||||
size += 1;
|
||||
}
|
||||
int[][] states = new int[size][1];
|
||||
int[] colors = new int[size];
|
||||
states[0] = new int[] { android.R.attr.state_pressed };
|
||||
colors[0] = pressedColor;
|
||||
if (size > 2) {
|
||||
states[1] = new int[] { -android.R.attr.state_enabled };
|
||||
colors[1] = disableColor;
|
||||
states[2] = new int[] { android.R.attr.state_enabled };
|
||||
colors[2] = defaultColor;
|
||||
} else {
|
||||
states[1] = new int[] { -android.R.attr.state_pressed };
|
||||
colors[1] = defaultColor;
|
||||
}
|
||||
ColorStateList newColor = new ColorStateList(states, colors);
|
||||
text.setTextColor(newColor);
|
||||
}
|
||||
return;
|
||||
}
|
||||
Drawable replaced = checkAndReplaceDrawable(view.getBackground(), alpha);
|
||||
if (replaced != null) {
|
||||
ViewCompat.setBackground(view, replaced);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user