package com.mercandalli.android.apps.files.common.view; import android.animation.AnimatorSet; import android.animation.ArgbEvaluator; import android.animation.ObjectAnimator; import android.annotation.TargetApi; import android.content.Context; import android.graphics.Canvas; import android.graphics.Outline; import android.graphics.Paint; import android.graphics.drawable.Drawable; import android.os.Build; import android.support.v4.content.ContextCompat; import android.util.AttributeSet; import android.util.Property; import android.view.View; import android.view.ViewOutlineProvider; import android.view.animation.DecelerateInterpolator; import android.widget.FrameLayout; import com.mercandalli.android.apps.files.R; /** * Created by Jonathan on 30/05/2015. */ public class PlayPauseView extends FrameLayout { private static final long PLAY_PAUSE_ANIMATION_DURATION = 200; private final PlayPauseDrawable mDrawable; private final Paint mPaint = new Paint(); private final int mPauseBackgroundColor; private final int mPlayBackgroundColor; private AnimatorSet mAnimatorSet; private int mBackgroundColor; private int mWidth; private int mHeight; public PlayPauseView(final Context context, final AttributeSet attrs) { super(context, attrs); setWillNotDraw(false); mBackgroundColor = ContextCompat.getColor(context, R.color.actionbar_audio); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL); mDrawable = new PlayPauseDrawable(context); mDrawable.setCallback(this); mPauseBackgroundColor = ContextCompat.getColor(context, R.color.actionbar_audio); mPlayBackgroundColor = ContextCompat.getColor(context, R.color.actionbar_audio); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); final int size = Math.min(getMeasuredWidth(), getMeasuredHeight()); setMeasuredDimension(size, size); } @Override protected void onSizeChanged(final int w, final int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mDrawable.setBounds(0, 0, w, h); mWidth = w; mHeight = h; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setOutlineProvider(new ViewOutlineProvider() { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void getOutline(View view, Outline outline) { outline.setOval(0, 0, view.getWidth(), view.getHeight()); } }); setClipToOutline(true); } } private void setColor(int color) { mBackgroundColor = color; invalidate(); } private int getColor() { return mBackgroundColor; } @Override protected boolean verifyDrawable(Drawable who) { return who == mDrawable || super.verifyDrawable(who); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(mBackgroundColor); final float radius = Math.min(mWidth, mHeight) / 2f; canvas.drawCircle(mWidth / 2f, mHeight / 2f, radius, mPaint); mDrawable.draw(canvas); } public void toggle() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { return; } if (mAnimatorSet != null) { mAnimatorSet.cancel(); } mAnimatorSet = new AnimatorSet(); mAnimatorSet.setInterpolator(new DecelerateInterpolator()); mAnimatorSet.setDuration(PLAY_PAUSE_ANIMATION_DURATION); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { final Property<PlayPauseView, Integer> COLOR = new Property<PlayPauseView, Integer>(Integer.class, "color") { @Override public Integer get(PlayPauseView v) { return v.getColor(); } @Override public void set(PlayPauseView v, Integer value) { v.setColor(value); } }; final ObjectAnimator colorAnim = ObjectAnimator.ofInt(this, COLOR, mDrawable.isPlay() ? mPauseBackgroundColor : mPlayBackgroundColor); colorAnim.setEvaluator(new ArgbEvaluator()); mAnimatorSet.playTogether(colorAnim, mDrawable.getPausePlayAnimator()); } mAnimatorSet.start(); } /** * The play button is visible. * * @return True if the play button is visible. */ public boolean isPlay() { return mDrawable.isPlay(); } public boolean isAnimationPlaying() { return mDrawable.isAnimationPlaying(); } }