Source code introduction: Android image sliding gradient to achieve animation effects. Source code effect: Source code snippet: - package com.example.tz_demo_8_14;
-
- import android.graphics.Canvas;
- import android.graphics.ColorFilter;
- import android.graphics.Rect;
- import android.graphics.drawable.Drawable;
- import android.util.Log;
- import android.view.Gravity;
-
- public class RevealDrawable extends Drawable {
-
- private Drawable mUnSelectedDrawable;
- private Drawable mSelectedDrawable;
- private Rect outRect = new Rect();
-
- public RevealDrawable(Drawable unSelectedDrawable, Drawable selectedDrawable) {
- this .mUnSelectedDrawable = unSelectedDrawable;
- this .mSelectedDrawable = selectedDrawable;
- }
-
-
-
-
- @Override
- public void draw(Canvas canvas) {
-
- int level = getLevel();
- if (level == 0 || level == 10000 ) {
-
- mUnSelectedDrawable.draw(canvas);
- } else if (level == 5000 ) {
-
- mSelectedDrawable.draw(canvas);
- } else {
-
-
- Rect bounds = getBounds();
- Rect r = outRect;
-
- {
-
- float ratio = (level / 5000f) - 1f;
- int w = bounds.width();
- w = ( int ) (w * Math.abs(ratio));
- int h = bounds.height();
- int gravity = ratio < 0 ? Gravity.LEFT : Gravity.RIGHT;
-
- Gravity.apply(gravity,
- w,
- h,
- bounds,
- r);
-
-
- canvas.save();
-
- canvas.clipRect(r);
- mUnSelectedDrawable.draw(canvas);
-
- canvas.restore();
- }
- {
-
- float ratio = (level / 5000f) - 1f;
- int w = bounds.width();
- w -= ( int ) (w * Math.abs(ratio));
- int h = bounds.height();
- int gravity = ratio < 0 ? Gravity.RIGHT : Gravity.LEFT;
-
- Gravity.apply(gravity,
- w,
- h,
- bounds,
- r);
-
-
- canvas.save();
-
- canvas.clipRect(r);
- mSelectedDrawable.draw(canvas);
-
- canvas.restore();
- }
- }
- }
-
- @Override
- protected boolean onLevelChange( int level) {
-
- invalidateSelf();
- return true ;
- }
-
-
-
-
- @Override
- protected void onBoundsChange(Rect bounds) {
-
- mUnSelectedDrawable.setBounds(bounds);
- mSelectedDrawable.setBounds(bounds);
- super .onBoundsChange(bounds);
- }
-
-
-
-
- @Override
- public int getIntrinsicWidth() {
- return mSelectedDrawable.getIntrinsicWidth();
- }
-
- @Override
- public int getIntrinsicHeight() {
- return mSelectedDrawable.getIntrinsicHeight();
- }
-
- @Override
- public void setAlpha( int alpha) {
-
- }
-
- @Override
- public void setColorFilter(ColorFilter cf) {
-
- }
-
- @Override
- public int getOpacity() {
- return 0 ;
- }
-
- }
Download address: http://download..com/data/2096556 |