New animated PlayPauseDrawable
This commit is contained in:
parent
e39162c126
commit
a27e5c6379
20 changed files with 282 additions and 246 deletions
|
|
@ -1,10 +1,6 @@
|
|||
package com.kabouzeid.gramophone.helper;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.kabouzeid.gramophone.R;
|
||||
|
|
@ -25,41 +21,21 @@ public class CreatePlaylistDialogHelper {
|
|||
}
|
||||
|
||||
public static MaterialDialog getDialog(final Context context, final List<Song> songs) {
|
||||
final EditText editText = new EditText(context);
|
||||
ViewGroup layout = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.dialog_empty_frame, null);
|
||||
if (editText.getParent() != null) {
|
||||
((ViewGroup) editText.getParent()).removeView(editText);
|
||||
}
|
||||
layout.addView(editText, new LinearLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||
return new MaterialDialog.Builder(context)
|
||||
.title(context.getResources().getString(R.string.action_new_playlist))
|
||||
.customView(layout, false)
|
||||
.positiveText(context.getResources().getString(R.string.ok))
|
||||
.negativeText(context.getResources().getString(R.string.cancel))
|
||||
.callback(new MaterialDialog.ButtonCallback() {
|
||||
@Override
|
||||
public void onPositive(MaterialDialog dialog) {
|
||||
super.onPositive(dialog);
|
||||
final String playlistName = editText.getText().toString();
|
||||
if (!playlistName.trim().equals("")) {
|
||||
dialog.dismiss();
|
||||
final int playlistId = PlaylistsUtil.createPlaylist(context, playlistName);
|
||||
if (playlistId != -1) {
|
||||
if (songs != null) {
|
||||
PlaylistsUtil.addToPlaylist(context, songs, playlistId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNegative(MaterialDialog dialog) {
|
||||
super.onNegative(dialog);
|
||||
dialog.dismiss();
|
||||
}
|
||||
}
|
||||
)
|
||||
.title(R.string.action_new_playlist)
|
||||
.positiveText(R.string.ok)
|
||||
.negativeText(R.string.cancel)
|
||||
.input("", "", new MaterialDialog.InputCallback() {
|
||||
@Override
|
||||
public void onInput(MaterialDialog materialDialog, CharSequence charSequence) {
|
||||
if (!charSequence.toString().trim().equals("")) {
|
||||
final int playlistId = PlaylistsUtil.createPlaylist(context, charSequence.toString());
|
||||
if (playlistId != -1 && songs != null) {
|
||||
PlaylistsUtil.addToPlaylist(context, songs, playlistId);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,36 +15,18 @@ import com.kabouzeid.gramophone.util.PlaylistsUtil;
|
|||
*/
|
||||
public class RenamePlaylistDialogHelper {
|
||||
public static MaterialDialog getDialog(final Context context, final int playlistId) {
|
||||
final EditText editText = new EditText(context);
|
||||
ViewGroup layout = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.dialog_empty_frame, null);
|
||||
if (editText.getParent() != null) {
|
||||
((ViewGroup) editText.getParent()).removeView(editText);
|
||||
}
|
||||
editText.setText(PlaylistsUtil.getNameForPlaylist(context, playlistId));
|
||||
layout.addView(editText, new LinearLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||
return new MaterialDialog.Builder(context)
|
||||
.title(context.getResources().getString(R.string.rename_playlist))
|
||||
.customView(layout, false)
|
||||
.positiveText(context.getResources().getString(R.string.ok))
|
||||
.negativeText(context.getResources().getString(R.string.cancel))
|
||||
.callback(new MaterialDialog.ButtonCallback() {
|
||||
@Override
|
||||
public void onPositive(MaterialDialog dialog) {
|
||||
super.onPositive(dialog);
|
||||
final String playlistName = editText.getText().toString();
|
||||
if (!playlistName.trim().equals("")) {
|
||||
PlaylistsUtil.renamePlaylist(context, playlistId, playlistName);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNegative(MaterialDialog dialog) {
|
||||
super.onNegative(dialog);
|
||||
dialog.dismiss();
|
||||
}
|
||||
}
|
||||
)
|
||||
.title(R.string.rename_playlist)
|
||||
.positiveText(R.string.ok)
|
||||
.negativeText(R.string.cancel)
|
||||
.input("", PlaylistsUtil.getNameForPlaylist(context, playlistId), new MaterialDialog.InputCallback() {
|
||||
@Override
|
||||
public void onInput(MaterialDialog materialDialog, CharSequence charSequence) {
|
||||
if (!charSequence.toString().trim().equals("")) {
|
||||
PlaylistsUtil.renamePlaylist(context, playlistId, charSequence.toString());
|
||||
}
|
||||
}
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -163,6 +163,7 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
|
|||
break;
|
||||
case ACTION_QUIT:
|
||||
killEverythingAndReleaseResources();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package com.kabouzeid.gramophone.ui.activities.base;
|
||||
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.util.Pair;
|
||||
import android.util.Log;
|
||||
|
|
@ -14,6 +13,7 @@ import com.kabouzeid.gramophone.R;
|
|||
import com.kabouzeid.gramophone.helper.MusicPlayerRemote;
|
||||
import com.kabouzeid.gramophone.misc.SmallOnGestureListener;
|
||||
import com.kabouzeid.gramophone.model.MusicRemoteEvent;
|
||||
import com.kabouzeid.gramophone.ui.widget.PlayPauseDrawable;
|
||||
import com.kabouzeid.gramophone.util.NavigationUtil;
|
||||
import com.melnykov.fab.FloatingActionButton;
|
||||
import com.squareup.otto.Subscribe;
|
||||
|
|
@ -23,7 +23,9 @@ import com.squareup.otto.Subscribe;
|
|||
*/
|
||||
public abstract class AbsFabActivity extends AbsBaseActivity {
|
||||
public static final String TAG = AbsFabActivity.class.getSimpleName();
|
||||
|
||||
private FloatingActionButton fab;
|
||||
private PlayPauseDrawable playPauseDrawable;
|
||||
private Object busEventListener = new Object() {
|
||||
@Subscribe
|
||||
public void onBusEvent(MusicRemoteEvent event) {
|
||||
|
|
@ -42,9 +44,11 @@ public abstract class AbsFabActivity extends AbsBaseActivity {
|
|||
}
|
||||
|
||||
private void setUpFab() {
|
||||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
|
||||
getFab().setImageResource(R.drawable.ic_pause_resume);
|
||||
if (playPauseDrawable == null) {
|
||||
playPauseDrawable = new PlayPauseDrawable(this);
|
||||
}
|
||||
|
||||
getFab().setImageDrawable(playPauseDrawable);
|
||||
updateFabState();
|
||||
final GestureDetector gestureDetector = new GestureDetector(this, new SmallOnGestureListener() {
|
||||
@Override
|
||||
|
|
@ -80,9 +84,9 @@ public abstract class AbsFabActivity extends AbsBaseActivity {
|
|||
|
||||
private void updateFabState() {
|
||||
if (MusicPlayerRemote.isPlaying()) {
|
||||
setFabPause();
|
||||
playPauseDrawable.setPause();
|
||||
} else {
|
||||
setFabPlay();
|
||||
playPauseDrawable.setPlay();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -150,17 +154,11 @@ public abstract class AbsFabActivity extends AbsBaseActivity {
|
|||
}
|
||||
}
|
||||
|
||||
private void setFabPlay(){
|
||||
getFab().setSelected(true);
|
||||
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
|
||||
getFab().setImageResource(R.drawable.ic_play_arrow_white_24dp);
|
||||
}
|
||||
private void setFabPlay() {
|
||||
playPauseDrawable.animatedPlay();
|
||||
}
|
||||
|
||||
private void setFabPause(){
|
||||
getFab().setSelected(false);
|
||||
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
|
||||
getFab().setImageResource(R.drawable.ic_pause_white_24dp);
|
||||
}
|
||||
private void setFabPause() {
|
||||
playPauseDrawable.animatedPause();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,219 @@
|
|||
package com.kabouzeid.gramophone.ui.widget;
|
||||
|
||||
import android.animation.Animator;
|
||||
import android.animation.AnimatorListenerAdapter;
|
||||
import android.animation.AnimatorSet;
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.ColorFilter;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Path;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.RectF;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.Property;
|
||||
import android.view.animation.DecelerateInterpolator;
|
||||
|
||||
import com.kabouzeid.gramophone.R;
|
||||
|
||||
public class PlayPauseDrawable extends Drawable {
|
||||
private static final long PLAY_PAUSE_ANIMATION_DURATION = 250;
|
||||
|
||||
private static final Property<PlayPauseDrawable, Float> PROGRESS =
|
||||
new Property<PlayPauseDrawable, Float>(Float.class, "progress") {
|
||||
@Override
|
||||
public Float get(PlayPauseDrawable d) {
|
||||
return d.getProgress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(PlayPauseDrawable d, Float value) {
|
||||
d.setProgress(value);
|
||||
}
|
||||
};
|
||||
|
||||
private final Path leftPauseBar = new Path();
|
||||
private final Path rightPauseBar = new Path();
|
||||
private final Paint paint = new Paint();
|
||||
private final float pauseBarWidth;
|
||||
private final float pauseBarHeight;
|
||||
private final float pauseBarDistance;
|
||||
|
||||
private float width;
|
||||
private float height;
|
||||
private float fallBackWidth;
|
||||
private float fallBackHeight;
|
||||
|
||||
private float progress;
|
||||
private boolean isPlay;
|
||||
private boolean isPlaySet;
|
||||
|
||||
private AnimatorSet animatorSet;
|
||||
|
||||
public PlayPauseDrawable(Context context) {
|
||||
final Resources res = context.getResources();
|
||||
paint.setAntiAlias(true);
|
||||
paint.setStyle(Paint.Style.FILL);
|
||||
paint.setColor(Color.WHITE);
|
||||
pauseBarWidth = res.getDimensionPixelSize(R.dimen.pause_bar_width);
|
||||
pauseBarHeight = res.getDimensionPixelSize(R.dimen.pause_bar_height);
|
||||
pauseBarDistance = res.getDimensionPixelSize(R.dimen.pause_bar_distance);
|
||||
fallBackWidth = res.getDimensionPixelSize(R.dimen.fab_icon_bound_width);
|
||||
fallBackHeight = res.getDimensionPixelSize(R.dimen.fab_icon_bound_height);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onBoundsChange(final Rect bounds) {
|
||||
super.onBoundsChange(bounds);
|
||||
if (bounds.width() > 0 && bounds.height() > 0) {
|
||||
width = bounds.width();
|
||||
height = bounds.height();
|
||||
} else {
|
||||
width = fallBackWidth;
|
||||
height = fallBackHeight;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas) {
|
||||
leftPauseBar.rewind();
|
||||
rightPauseBar.rewind();
|
||||
|
||||
// The current distance between the two pause bars.
|
||||
final float barDist = lerp(pauseBarDistance, 0, progress);
|
||||
// The current width of each pause bar.
|
||||
final float barWidth = lerp(pauseBarWidth, pauseBarHeight / 2f, progress);
|
||||
// The current position of the left pause bar's top left coordinate.
|
||||
final float firstBarTopLeft = lerp(0, barWidth, progress);
|
||||
// The current position of the right pause bar's top right coordinate.
|
||||
final float secondBarTopRight = lerp(2 * barWidth + barDist, barWidth + barDist, progress);
|
||||
|
||||
// Draw the left pause bar. The left pause bar transforms into the
|
||||
// top half of the play button triangle by animating the position of the
|
||||
// rectangle's top left coordinate and expanding its bottom width.
|
||||
leftPauseBar.moveTo(0, 0);
|
||||
leftPauseBar.lineTo(firstBarTopLeft, -pauseBarHeight);
|
||||
leftPauseBar.lineTo(barWidth, -pauseBarHeight);
|
||||
leftPauseBar.lineTo(barWidth, 0);
|
||||
leftPauseBar.close();
|
||||
|
||||
// Draw the right pause bar. The right pause bar transforms into the
|
||||
// bottom half of the play button triangle by animating the position of the
|
||||
// rectangle's top right coordinate and expanding its bottom width.
|
||||
rightPauseBar.moveTo(barWidth + barDist, 0);
|
||||
rightPauseBar.lineTo(barWidth + barDist, -pauseBarHeight);
|
||||
rightPauseBar.lineTo(secondBarTopRight, -pauseBarHeight);
|
||||
rightPauseBar.lineTo(2 * barWidth + barDist, 0);
|
||||
rightPauseBar.close();
|
||||
|
||||
canvas.save();
|
||||
|
||||
// Translate the play button a tiny bit to the right so it looks more centered.
|
||||
canvas.translate(lerp(0, pauseBarHeight / 8f, progress), 0);
|
||||
|
||||
// (1) Pause --> Play: rotate 0 to 90 degrees clockwise.
|
||||
// (2) Play --> Pause: rotate 90 to 180 degrees clockwise.
|
||||
final float rotationProgress = isPlay ? 1 - progress : progress;
|
||||
final float startingRotation = isPlay ? 90 : 0;
|
||||
canvas.rotate(lerp(startingRotation, startingRotation + 90, rotationProgress), width / 2f, height / 2f);
|
||||
|
||||
// Position the pause/play button in the center of the drawable's bounds.
|
||||
canvas.translate(width / 2f - ((2 * barWidth + barDist) / 2f), height / 2f + (pauseBarHeight / 2f));
|
||||
|
||||
// Draw the two bars that form the animated pause/play button.
|
||||
canvas.drawPath(leftPauseBar, paint);
|
||||
canvas.drawPath(rightPauseBar, paint);
|
||||
|
||||
canvas.restore();
|
||||
}
|
||||
|
||||
private Animator getPausePlayAnimator() {
|
||||
isPlaySet = !isPlaySet;
|
||||
final Animator anim = ObjectAnimator.ofFloat(this, PROGRESS, isPlay ? 1 : 0, isPlay ? 0 : 1);
|
||||
anim.addListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
isPlay = !isPlay;
|
||||
}
|
||||
});
|
||||
return anim;
|
||||
}
|
||||
|
||||
public boolean isPlay() {
|
||||
return isPlaySet;
|
||||
}
|
||||
|
||||
private void setProgress(float progress) {
|
||||
this.progress = progress;
|
||||
invalidateSelf();
|
||||
}
|
||||
|
||||
private float getProgress() {
|
||||
return progress;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAlpha(int alpha) {
|
||||
paint.setAlpha(alpha);
|
||||
invalidateSelf();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColorFilter(ColorFilter cf) {
|
||||
paint.setColorFilter(cf);
|
||||
invalidateSelf();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOpacity() {
|
||||
return PixelFormat.TRANSLUCENT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Linear interpolate between a and b with parameter t.
|
||||
*/
|
||||
private static float lerp(float a, float b, float t) {
|
||||
return a + (b - a) * t;
|
||||
}
|
||||
|
||||
public void animatedPlay() {
|
||||
if (!isPlaySet) {
|
||||
togglePlayPause();
|
||||
}
|
||||
}
|
||||
|
||||
public void animatedPause() {
|
||||
if (isPlaySet) {
|
||||
togglePlayPause();
|
||||
}
|
||||
}
|
||||
|
||||
public void setPlay() {
|
||||
isPlaySet = true;
|
||||
isPlay = true;
|
||||
setProgress(1);
|
||||
}
|
||||
|
||||
public void setPause() {
|
||||
isPlaySet = false;
|
||||
isPlay = false;
|
||||
setProgress(0);
|
||||
}
|
||||
|
||||
public void togglePlayPause() {
|
||||
if (animatorSet != null) {
|
||||
animatorSet.cancel();
|
||||
}
|
||||
|
||||
animatorSet = new AnimatorSet();
|
||||
final Animator pausePlayAnim = getPausePlayAnimator();
|
||||
animatorSet.setInterpolator(new DecelerateInterpolator());
|
||||
animatorSet.setDuration(PLAY_PAUSE_ANIMATION_DURATION);
|
||||
animatorSet.playTogether(pausePlayAnim);
|
||||
animatorSet.start();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue