improved progress wheel color

This commit is contained in:
Mariotaku Lee 2016-12-23 10:26:44 +08:00
parent 4c792e87a8
commit 3e84e296b8
5 changed files with 88 additions and 15 deletions

View File

@ -18,6 +18,7 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.DecorToolbar;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
@ -36,16 +37,23 @@ public class Chameleon {
private final Activity activity;
private final Theme theme;
@Nullable
private final AppearanceCreator creator;
private final ArrayMap<ChameleonView, ChameleonView.Appearance> postApplyViews;
private Chameleon(Activity activity) {
private Chameleon(Activity activity, @Nullable AppearanceCreator creator) {
this.activity = activity;
this.creator = creator;
this.theme = getOverrideTheme(activity, activity);
this.postApplyViews = new ArrayMap<>();
}
public static Chameleon getInstance(Activity activity) {
return new Chameleon(activity);
return getInstance(activity, null);
}
public static Chameleon getInstance(Activity activity, AppearanceCreator creator) {
return new Chameleon(activity, creator);
}
@SuppressWarnings("WeakerAccess")
@ -57,7 +65,7 @@ public class Chameleon {
delegate = ((AppCompatActivity) activity).getDelegate();
}
final ChameleonInflationFactory factory = new ChameleonInflationFactory(inflater, activity,
delegate, theme, postApplyViews);
creator, delegate, theme, postApplyViews);
LayoutInflaterCompat.setFactory(inflater, factory);
}
@ -146,10 +154,19 @@ public class Chameleon {
DrawableCompat.setTint(icon, color);
}
public interface AppearanceCreator {
@Nullable
ChameleonView.Appearance createAppearance(@NonNull View view,
@NonNull Context context,
@NonNull AttributeSet attributeSet,
@NonNull Chameleon.Theme theme);
void applyAppearance(@NonNull View view, @NonNull ChameleonView.Appearance appearance);
}
/**
* Created by mariotaku on 2016/12/18.
*/
public static class Theme {
private int colorBackground;
private int colorForeground;

View File

@ -15,7 +15,7 @@ public class ChameleonActivity extends AppCompatActivity implements Chameleon.Th
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
mChameleon = Chameleon.getInstance(this);
mChameleon = Chameleon.getInstance(this, onCreateAppearanceCreator());
mChameleon.preApply();
super.onCreate(savedInstanceState);
}
@ -59,4 +59,9 @@ public class ChameleonActivity extends AppCompatActivity implements Chameleon.Th
public Chameleon.Theme getOverrideTheme() {
return null;
}
@Nullable
protected Chameleon.AppearanceCreator onCreateAppearanceCreator() {
return null;
}
}

View File

@ -36,6 +36,7 @@ public class ChameleonInflationFactory implements LayoutInflaterFactory {
private final LayoutInflater mInflater;
@Nullable
private final Activity mActivity;
private final Chameleon.AppearanceCreator mCreator;
@Nullable
private final AppCompatDelegate mDelegate;
@NonNull
@ -45,11 +46,13 @@ public class ChameleonInflationFactory implements LayoutInflaterFactory {
public ChameleonInflationFactory(@NonNull LayoutInflater inflater,
@Nullable Activity activity,
Chameleon.AppearanceCreator creator,
@Nullable AppCompatDelegate delegate,
@NonNull Chameleon.Theme theme,
@NonNull ArrayMap<ChameleonView, ChameleonView.Appearance> postApplyViews) {
this.mInflater = inflater;
this.mActivity = activity;
this.mCreator = creator;
this.mDelegate = delegate;
this.mTheme = theme;
this.mPostApplyViews = postApplyViews;
@ -166,18 +169,28 @@ public class ChameleonInflationFactory implements LayoutInflaterFactory {
cv.applyAppearance(appearance);
}
}
} else {
ChameleonTypedArray a = ChameleonTypedArray.obtain(context, attrs,
R.styleable.ChameleonView, mTheme);
Drawable background = a.getDrawable(R.styleable.ChameleonView_android_background);
if (background != null) {
int tint = a.getColor(R.styleable.ChameleonView_backgroundTint, 0);
if (tint != 0) {
DrawableCompat.setTint(background, tint);
} else if (view != null) {
boolean handled = false;
if (mCreator != null) {
ChameleonView.Appearance appearance = mCreator.createAppearance(view, context, attrs, mTheme);
if (appearance != null) {
mCreator.applyAppearance(view, appearance);
handled = true;
}
SupportMethods.setBackground(view, background);
}
a.recycle();
if (!handled) {
ChameleonTypedArray a = ChameleonTypedArray.obtain(context, attrs,
R.styleable.ChameleonView, mTheme);
Drawable background = a.getDrawable(R.styleable.ChameleonView_android_background);
if (background != null) {
int tint = a.getColor(R.styleable.ChameleonView_backgroundTint, 0);
if (tint != 0) {
DrawableCompat.setTint(background, tint);
}
SupportMethods.setBackground(view, background);
}
a.recycle();
}
}
return view;
}

View File

@ -53,6 +53,7 @@ import org.mariotaku.twidere.preference.iface.IDialogPreference
import org.mariotaku.twidere.util.*
import org.mariotaku.twidere.util.KeyboardShortcutsHandler.KeyboardShortcutCallback
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper
import org.mariotaku.twidere.util.theme.TwidereAppearanceCreator
import org.mariotaku.twidere.view.iface.IExtendedView.OnFitSystemWindowsListener
import java.lang.reflect.InvocationTargetException
import java.util.*
@ -359,8 +360,13 @@ open class BaseActivity : ChameleonActivity(), IExtendedActivity, IThemedActivit
return userTheme
}
override fun onCreateAppearanceCreator(): Chameleon.AppearanceCreator? {
return TwidereAppearanceCreator
}
companion object {
private val sClassPrefixList = arrayOf("android.widget.", "android.view.", "android.webkit.")
}
}

View File

@ -0,0 +1,32 @@
package org.mariotaku.twidere.util.theme
import android.content.Context
import android.util.AttributeSet
import android.view.View
import com.pnikosis.materialishprogress.ProgressWheel
import org.mariotaku.chameleon.Chameleon
import org.mariotaku.chameleon.ChameleonView
object TwidereAppearanceCreator : Chameleon.AppearanceCreator {
override fun createAppearance(view: View, context: Context, attributeSet: AttributeSet, theme: Chameleon.Theme): ChameleonView.Appearance? {
when (view) {
is ProgressWheel -> {
return BasicColorAppearance(theme.colorAccent)
}
}
return null
}
override fun applyAppearance(view: View, appearance: ChameleonView.Appearance) {
when (view) {
is ProgressWheel -> {
appearance as BasicColorAppearance
view.barColor = appearance.color
}
}
}
data class BasicColorAppearance(var color: Int) : ChameleonView.Appearance
}