ViewInvertHelperpublic class ViewInvertHelper extends Object Helper to invert the colors of views and fade between the states. |
Fields Summary |
---|
private final android.graphics.Paint | mDarkPaint | private final android.view.animation.Interpolator | mLinearOutSlowInInterpolator | private final android.view.View | mTarget | private final android.graphics.ColorMatrix | mMatrix | private final android.graphics.ColorMatrix | mGrayscaleMatrix | private final long | mFadeDuration |
Constructors Summary |
---|
public ViewInvertHelper(android.view.View target, long fadeDuration)
mTarget = target;
mLinearOutSlowInInterpolator = AnimationUtils.loadInterpolator(mTarget.getContext(),
android.R.interpolator.linear_out_slow_in);
mFadeDuration = fadeDuration;
|
Methods Summary |
---|
public void | fade(boolean invert, long delay)
float startIntensity = invert ? 0f : 1f;
float endIntensity = invert ? 1f : 0f;
ValueAnimator animator = ValueAnimator.ofFloat(startIntensity, endIntensity);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
updateInvertPaint((Float) animation.getAnimatedValue());
mTarget.setLayerType(View.LAYER_TYPE_HARDWARE, mDarkPaint);
}
});
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (!invert) {
mTarget.setLayerType(View.LAYER_TYPE_NONE, null);
}
}
});
animator.setDuration(mFadeDuration);
animator.setInterpolator(mLinearOutSlowInInterpolator);
animator.setStartDelay(delay);
animator.start();
| public android.view.View | getTarget()
return mTarget;
| public void | update(boolean invert)
if (invert) {
updateInvertPaint(1f);
mTarget.setLayerType(View.LAYER_TYPE_HARDWARE, mDarkPaint);
} else {
mTarget.setLayerType(View.LAYER_TYPE_NONE, null);
}
| private void | updateInvertPaint(float intensity)
float components = 1 - 2 * intensity;
final float[] invert = {
components, 0f, 0f, 0f, 255f * intensity,
0f, components, 0f, 0f, 255f * intensity,
0f, 0f, components, 0f, 255f * intensity,
0f, 0f, 0f, 1f, 0f
};
mMatrix.set(invert);
mGrayscaleMatrix.setSaturation(1 - intensity);
mMatrix.preConcat(mGrayscaleMatrix);
mDarkPaint.setColorFilter(new ColorMatrixColorFilter(mMatrix));
|
|