Animating Path's Transparency

26 March 2016 ·

At the moment I’m learning Android development and one of the issues that I faced was animating Path transparency.

I couldn’t find a copy&paste solution, what is pretty strange as it should be a straightforward problem/solution.

This is why after making a few tries and finding something that is actually working, I’d like to share my solution.

public class OverlayDrawView extends View {
    private Paint mPaint;
    private Path mPath;
    private float mAlpha;
    private ObjectAnimator mObjectAnimator;

    /**
     * Default constructor
     * @param context
     */
    public OverlayDrawView(Context context) {
        super(context);
        init();
    }

    /**
     * Default constructor
     * @param context
     */
    public OverlayDrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * Default constructor
     * @param context
     */
    public OverlayDrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * Setting up the painter to draw Path
     */
    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.rgb(255, 255, 255));
        mPaint.setStrokeWidth(10);
        mPaint.setStyle(Paint.Style.STROKE);

        // Animating alpha channel from 0 to 255
        // Alpha channel is int, thus ObjectAnimator.ofInt is used
        mObjectAnimator = ObjectAnimator.ofInt(mPaint, "alpha", 0, 255);
        mObjectAnimator.setDuration(600);

        // Just Linear interpolation
        mObjectAnimator.setInterpolator(new LinearInterpolator());

        // Here you can handle value changes
        mObjectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                // Casting changed Object as int to mPaint as a value of alpha
                mPaint.setAlpha((int) animation.getAnimatedValue());
                invalidate();
            }
        });
    }

    /**
     * onDraw event callback
     * @param canvas Canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mPath != null) {
            canvas.drawPath(mPath, mPaint);
        }
    }

    /**
     * Setting path that has to be drawn
     * @param path Path
     */
    public void setPath(Path path) {
        this.mPath = path;
        mObjectAnimator.start();
    }
}

Tags:

Might also be interesting for you

Open RSS Reader

I'm starting a new open-source project "Open RSS Reader" that will be modular app so if you don't like the existing clients, you'll be able to quickly build your own. It's gonna be both open source and free to use.

SotM: pet - Awesome Command-line Snippet Manager

`pet` is a simple command-line snippet manager. It allows you to write and store snippets for the command-line, so you don't need to remember all the commands you frequently use in you terminal. It can sync your snippets to the GitHub Gist, so you can always take your snippets with you.

5 Reasons To Give Linux A Try

This time I want to share 5 reasons why I advocate for using Linux whenever I have a chance, and why you should give it a try too.

SotM: TaskWarrior - Great CLI Task Manager

Taskwarrior is an open-source, cross platform time and task management tool. It has a command-line interface rather than a graphical user interface.

Comment

comments powered by Disqus