Added native image loader support for artist images. Automatically recreate activity onrResume when colors changed fixes issues #2 and #39. Also Butterknife should be used now everywhere #40
This commit is contained in:
parent
9c8cba612b
commit
8bdaf08a30
29 changed files with 485 additions and 521 deletions
|
|
@ -3,16 +3,27 @@ package com.kabouzeid.gramophone.util;
|
|||
import android.content.Context;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.support.annotation.AttrRes;
|
||||
import android.support.annotation.ColorInt;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.graphics.Palette;
|
||||
|
||||
import com.kabouzeid.gramophone.R;
|
||||
|
||||
/**
|
||||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
public class ColorUtil {
|
||||
|
||||
public static int generateColor(Context context, Bitmap bitmap) {
|
||||
return Palette.from(bitmap)
|
||||
.resizeBitmapSize(100)
|
||||
.generate()
|
||||
.getVibrantColor(ColorUtil.resolveColor(context, R.attr.default_bar_color));
|
||||
}
|
||||
|
||||
public static int resolveColor(@NonNull Context context, @AttrRes int colorAttr) {
|
||||
TypedArray a = context.obtainStyledAttributes(new int[]{colorAttr});
|
||||
int resId = a.getColor(0, 0);
|
||||
|
|
@ -50,10 +61,10 @@ public class ColorUtil {
|
|||
}
|
||||
|
||||
public static boolean useDarkTextColorOnBackground(int backgroundColor) {
|
||||
return (Color.red(backgroundColor) * 0.299 + Color.green(backgroundColor) * 0.587 + Color.blue(backgroundColor) * 0.114) > 186;
|
||||
return (Color.red(backgroundColor) * 0.299 + Color.green(backgroundColor) * 0.587 + Color.blue(backgroundColor) * 0.114) > (255 / 2);
|
||||
}
|
||||
|
||||
public static int getTextColorForBackground(int backgroundColor) {
|
||||
return (Color.red(backgroundColor) * 0.299 + Color.green(backgroundColor) * 0.587 + Color.blue(backgroundColor) * 0.114) > 186 ? Color.BLACK : Color.WHITE;
|
||||
return useDarkTextColorOnBackground(backgroundColor) ? Color.BLACK : Color.WHITE;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,15 @@ import java.util.List;
|
|||
public class MusicUtil {
|
||||
public static final String TAG = MusicUtil.class.getSimpleName();
|
||||
|
||||
@NonNull
|
||||
public static String getArtistImageLoaderString(@NonNull Artist artist, boolean forceDownload) {
|
||||
if (forceDownload) {
|
||||
return PhonographImageDownloader.SCHEME_ARTIST + "no-cache#" + artist.name;
|
||||
} else {
|
||||
return PhonographImageDownloader.SCHEME_ARTIST + "#" + artist.name;
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static String getAlbumImageLoaderString(@NonNull Album album) {
|
||||
return PhonographImageDownloader.SCHEME_ALBUM + album.id;
|
||||
|
|
|
|||
|
|
@ -71,14 +71,29 @@ public final class PreferenceUtil {
|
|||
mPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||
}
|
||||
|
||||
public int getGeneralTheme() {
|
||||
int value = Integer.parseInt(mPreferences.getString(GENERAL_THEME, "0"));
|
||||
switch (value) {
|
||||
case 0:
|
||||
return R.style.Theme_MaterialMusic_Light;
|
||||
case 1:
|
||||
return R.style.Theme_MaterialMusic;
|
||||
@SuppressLint("CommitPrefEdits")
|
||||
public void setGeneralTheme(Context context, String value) {
|
||||
String[] allowedValues = context.getResources().getStringArray(R.array.pref_general_theme_list_values);
|
||||
for (String allowedValue : allowedValues) {
|
||||
if (value.equals(allowedValue)) {
|
||||
mPreferences.edit().putString(GENERAL_THEME, value).commit();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getGeneralTheme() {
|
||||
try {
|
||||
int value = Integer.parseInt(mPreferences.getString(GENERAL_THEME, "0"));
|
||||
switch (value) {
|
||||
case 0:
|
||||
return R.style.Theme_MaterialMusic_Light;
|
||||
case 1:
|
||||
return R.style.Theme_MaterialMusic;
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
|
||||
return R.style.Theme_MaterialMusic_Light;
|
||||
}
|
||||
|
||||
|
|
@ -86,10 +101,6 @@ public final class PreferenceUtil {
|
|||
return mPreferences.getInt("primary_color", context.getResources().getColor(R.color.indigo_500));
|
||||
}
|
||||
|
||||
public int getThemeColorPrimaryDarker(Context context) {
|
||||
return ColorUtil.shiftColorDown(getThemeColorPrimary(context));
|
||||
}
|
||||
|
||||
@SuppressLint("CommitPrefEdits")
|
||||
public void setThemeColorPrimary(int color) {
|
||||
mPreferences.edit().putInt("primary_color", color).commit();
|
||||
|
|
|
|||
|
|
@ -2,11 +2,9 @@ package com.kabouzeid.gramophone.util;
|
|||
|
||||
import android.animation.ArgbEvaluator;
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.graphics.Palette;
|
||||
import android.support.v7.internal.view.menu.ListMenuItemView;
|
||||
import android.support.v7.internal.view.menu.MenuPopupHelper;
|
||||
import android.view.View;
|
||||
|
|
@ -28,47 +26,6 @@ import java.lang.reflect.Field;
|
|||
public class ViewUtil {
|
||||
public final static int DEFAULT_COLOR_ANIMATION_DURATION = 500;
|
||||
|
||||
public static void applyBackgroundColorFromBitmap(@Nullable Bitmap bitmap, final int defaultBgColor, @Nullable final View[] views, @Nullable final TextView[] textViews, final boolean animate) {
|
||||
if (bitmap != null) {
|
||||
Palette.from(bitmap)
|
||||
.resizeBitmapSize(100)
|
||||
.generate(new Palette.PaletteAsyncListener() {
|
||||
@Override
|
||||
public void onGenerated(@NonNull Palette palette) {
|
||||
applyBackgroundColor(palette.getVibrantColor(defaultBgColor), views, textViews, animate);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
applyBackgroundColor(defaultBgColor, views, textViews, animate);
|
||||
}
|
||||
}
|
||||
|
||||
public static void applyBackgroundColor(int bgColor, @Nullable final View[] views, @Nullable TextView[] textViews, final boolean animate) {
|
||||
if (views != null) {
|
||||
for (View view : views) {
|
||||
if (view != null) {
|
||||
if (animate) {
|
||||
ViewUtil.animateViewColor(view, view.getDrawingCacheBackgroundColor(), bgColor);
|
||||
} else {
|
||||
view.setBackgroundColor(bgColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (textViews != null) {
|
||||
int textColor = ColorUtil.getTextColorForBackground(bgColor);
|
||||
for (TextView textView : textViews) {
|
||||
if (textView != null) {
|
||||
if (animate) {
|
||||
animateTextColor(textView, textView.getCurrentTextColor(), textColor);
|
||||
} else {
|
||||
textView.setTextColor(textColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void animateViewColor(final View v, final int startColor, final int endColor) {
|
||||
animateViewColor(v, startColor, endColor, DEFAULT_COLOR_ANIMATION_DURATION);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue