use new apply tint strategy
This commit is contained in:
parent
dd5fc88381
commit
49593c0c4e
|
@ -47,7 +47,8 @@ import org.mariotaku.twidere.activity.AppCompatPreferenceActivity;
|
|||
import org.mariotaku.twidere.activity.iface.IThemedActivity;
|
||||
import org.mariotaku.twidere.view.ShapedImageView;
|
||||
import org.mariotaku.twidere.view.TwidereToolbar;
|
||||
import org.mariotaku.twidere.view.iface.IThemedView;
|
||||
import org.mariotaku.twidere.view.iface.IThemeAccentView;
|
||||
import org.mariotaku.twidere.view.iface.IThemeBackgroundTintView;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
@ -122,57 +123,65 @@ public class ThemedLayoutInflaterFactory implements LayoutInflaterFactory {
|
|||
}
|
||||
|
||||
private static void initViewTint(View view, IThemedActivity activity) {
|
||||
final int noTintColor, tintColor, actionBarColor;
|
||||
final int noTintColor, accentColor, backgroundTintColor, actionBarColor;
|
||||
final boolean isColorTint;
|
||||
// View context is not derived from ActionBar, apply color tint directly
|
||||
final Resources resources = ((Activity) activity).getResources();
|
||||
final boolean isActionBarContext = isActionBarContext(view.getContext(), getActionBarContext((Activity) activity));
|
||||
final int themeResourceId = activity.getCurrentThemeResourceId();
|
||||
final boolean isDarkTheme = ThemeUtils.isDarkTheme(themeResourceId);
|
||||
if (!isActionBarContext) {
|
||||
tintColor = actionBarColor = activity.getCurrentThemeColor();
|
||||
noTintColor = TwidereColorUtils.getContrastYIQ(tintColor, ThemeUtils.ACCENT_COLOR_THRESHOLD);
|
||||
accentColor = actionBarColor = activity.getCurrentThemeColor();
|
||||
noTintColor = TwidereColorUtils.getContrastYIQ(accentColor, ThemeUtils.ACCENT_COLOR_THRESHOLD);
|
||||
backgroundTintColor = accentColor;
|
||||
isColorTint = true;
|
||||
} else if (ThemeUtils.isDarkTheme(themeResourceId)) {
|
||||
} else if (isDarkTheme) {
|
||||
// View context is derived from ActionBar but is currently dark theme, so we should show
|
||||
// light
|
||||
actionBarColor = resources.getColor(R.color.background_color_action_bar_dark);
|
||||
noTintColor = Color.WHITE;
|
||||
tintColor = activity.getCurrentThemeColor();
|
||||
accentColor = activity.getCurrentThemeColor();
|
||||
backgroundTintColor = noTintColor;
|
||||
isColorTint = true;
|
||||
} else {
|
||||
// View context is derived from ActionBar and it's light theme, so we use contrast color
|
||||
actionBarColor = activity.getCurrentThemeColor();
|
||||
tintColor = TwidereColorUtils.getContrastYIQ(actionBarColor, ThemeUtils.ACCENT_COLOR_THRESHOLD);
|
||||
noTintColor = TwidereColorUtils.getContrastYIQ(tintColor, ThemeUtils.ACCENT_COLOR_THRESHOLD);
|
||||
accentColor = TwidereColorUtils.getContrastYIQ(actionBarColor, ThemeUtils.ACCENT_COLOR_THRESHOLD);
|
||||
noTintColor = TwidereColorUtils.getContrastYIQ(accentColor, ThemeUtils.ACCENT_COLOR_THRESHOLD);
|
||||
backgroundTintColor = accentColor;
|
||||
isColorTint = false;
|
||||
}
|
||||
if (view instanceof TextView) {
|
||||
final TextView textView = (TextView) view;
|
||||
textView.setLinkTextColor(tintColor);
|
||||
textView.setLinkTextColor(accentColor);
|
||||
}
|
||||
if (view instanceof IThemedView) {
|
||||
((IThemedView) view).setThemeTintColor(ColorStateList.valueOf(tintColor));
|
||||
if (view instanceof IThemeAccentView) {
|
||||
((IThemeAccentView) view).setAccentTintColor(ColorStateList.valueOf(accentColor));
|
||||
} else if (view instanceof IThemeBackgroundTintView) {
|
||||
((IThemeBackgroundTintView) view).setBackgroundTintColor(ColorStateList.valueOf(backgroundTintColor));
|
||||
} else if (view instanceof TintableBackgroundView) {
|
||||
final TintableBackgroundView tintable = (TintableBackgroundView) view;
|
||||
if (view instanceof Button) {
|
||||
} else if (view instanceof EditText) {
|
||||
tintable.setSupportBackgroundTintList(ColorStateList.valueOf(tintColor));
|
||||
} else {
|
||||
if (isColorTint) {
|
||||
final int[][] states = {{android.R.attr.state_selected}, {android.R.attr.state_focused},
|
||||
{android.R.attr.state_pressed}, {0}};
|
||||
final int[] colors = {tintColor, tintColor, tintColor, noTintColor};
|
||||
tintable.setSupportBackgroundTintList(new ColorStateList(states, colors));
|
||||
} else {
|
||||
tintable.setSupportBackgroundTintList(ColorStateList.valueOf(tintColor));
|
||||
}
|
||||
}
|
||||
} else if (view instanceof EditText) {
|
||||
ViewCompat.setBackgroundTintList(view, ColorStateList.valueOf(tintColor));
|
||||
applyTintableBackgroundViewTint(tintable, accentColor, noTintColor, backgroundTintColor, isColorTint);
|
||||
} else if (view instanceof TwidereToolbar) {
|
||||
final int itemColor = ThemeUtils.getContrastActionBarItemColor((Context) activity,
|
||||
themeResourceId, actionBarColor);
|
||||
((TwidereToolbar) view).setItemColor(itemColor);
|
||||
} else if (view instanceof EditText) {
|
||||
ViewCompat.setBackgroundTintList(view, ColorStateList.valueOf(accentColor));
|
||||
}
|
||||
}
|
||||
|
||||
private static void applyTintableBackgroundViewTint(TintableBackgroundView tintable, int accentColor, int noTintColor, int backgroundTintColor, boolean isColorTint) {
|
||||
if (tintable instanceof Button) {
|
||||
} else if (tintable instanceof EditText) {
|
||||
tintable.setSupportBackgroundTintList(ColorStateList.valueOf(backgroundTintColor));
|
||||
} else if (isColorTint) {
|
||||
final int[][] states = {{android.R.attr.state_selected}, {android.R.attr.state_focused},
|
||||
{android.R.attr.state_pressed}, {0}};
|
||||
final int[] colors = {accentColor, accentColor, accentColor, noTintColor};
|
||||
tintable.setSupportBackgroundTintList(new ColorStateList(states, colors));
|
||||
} else {
|
||||
tintable.setSupportBackgroundTintList(ColorStateList.valueOf(accentColor));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -24,8 +24,8 @@ import android.content.res.ColorStateList;
|
|||
/**
|
||||
* Created by mariotaku on 14/12/19.
|
||||
*/
|
||||
public interface IThemedView {
|
||||
public interface IThemeAccentView {
|
||||
|
||||
public void setThemeTintColor(ColorStateList color);
|
||||
public void setAccentTintColor(ColorStateList color);
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Twidere - Twitter client for Android
|
||||
*
|
||||
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.mariotaku.twidere.view.iface;
|
||||
|
||||
import android.content.res.ColorStateList;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 14/12/19.
|
||||
*/
|
||||
public interface IThemeBackgroundTintView {
|
||||
|
||||
public void setBackgroundTintColor(ColorStateList color);
|
||||
|
||||
}
|
|
@ -26,12 +26,13 @@ import android.graphics.PorterDuff.Mode;
|
|||
import android.util.AttributeSet;
|
||||
import android.widget.ImageButton;
|
||||
|
||||
import org.mariotaku.twidere.view.iface.IThemedView;
|
||||
import org.mariotaku.twidere.view.iface.IThemeAccentView;
|
||||
import org.mariotaku.twidere.view.iface.IThemeBackgroundTintView;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 14/11/5.
|
||||
*/
|
||||
public class TintThemedImageButton extends ImageButton implements IThemedView {
|
||||
public class TintThemedImageButton extends ImageButton implements IThemeBackgroundTintView {
|
||||
|
||||
private final int mDefaultColor;
|
||||
|
||||
|
@ -62,7 +63,7 @@ public class TintThemedImageButton extends ImageButton implements IThemedView {
|
|||
|
||||
|
||||
@Override
|
||||
public void setThemeTintColor(ColorStateList color) {
|
||||
public void setBackgroundTintColor(ColorStateList color) {
|
||||
if (color == null) {
|
||||
clearColorFilter();
|
||||
} else {
|
||||
|
|
|
@ -26,12 +26,12 @@ import android.util.AttributeSet;
|
|||
|
||||
import com.pnikosis.materialishprogress.ProgressWheel;
|
||||
|
||||
import org.mariotaku.twidere.view.iface.IThemedView;
|
||||
import org.mariotaku.twidere.view.iface.IThemeAccentView;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 15/4/25.
|
||||
*/
|
||||
public class TintThemedProgressWheel extends ProgressWheel implements IThemedView {
|
||||
public class TintThemedProgressWheel extends ProgressWheel implements IThemeAccentView {
|
||||
public TintThemedProgressWheel(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ public class TintThemedProgressWheel extends ProgressWheel implements IThemedVie
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setThemeTintColor(ColorStateList color) {
|
||||
public void setAccentTintColor(ColorStateList color) {
|
||||
if (color != null) {
|
||||
setBarColor(color.getDefaultColor());
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue