重构ASupportToolbar自定义控件,支持AES主题切换

- 优化ASupportToolbar控件的绘图流程
- 移除atoolbar_frame.xml依赖,改用Java代码内部绘图
- 添加notifyColorChange()方法绘制三层渐变背景
- 在onAttachedToWindow()时自动调用refreshFromTheme()刷新背景
- 主题切换时通过DrawerFragmentActivity调用refreshFromTheme()
- 添加attrs.xml属性:android:background用于自定义style
- 添加详细调试日志追踪整个绘制流程
- TestASupportToolbarActivity添加日志便于测试验证

修改文件:
- libaes/src/main/java/.../views/ASupportToolbar.java:重构绘图逻辑,添加调试日志
- libaes/src/main/java/.../activitys/DrawerFragmentActivity.java:主题切换时刷新Toolbar
- libaes/src/main/java/.../unittests/TestASupportToolbarActivity.java:添加调试日志
- libaes/src/main/res/values/attrs.xml:添加android:background属性
- aes/build.properties:版本号更新
- libaes/build.properties:版本号更新
This commit is contained in:
2026-05-12 10:53:18 +08:00
parent 58b2cace16
commit 632ecc51d8
6 changed files with 111 additions and 49 deletions

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Tue May 12 09:45:14 CST 2026
#Tue May 12 10:47:54 CST 2026
stageCount=3
libraryProject=libaes
baseVersion=15.20
publishVersion=15.20.2
buildCount=5
buildCount=14
baseBetaVersion=15.20.3

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Tue May 12 09:45:14 CST 2026
#Tue May 12 10:47:54 CST 2026
stageCount=3
libraryProject=libaes
baseVersion=15.20
publishVersion=15.20.2
buildCount=5
buildCount=14
baseBetaVersion=15.20.3

View File

@@ -31,6 +31,7 @@ import cc.winboll.studio.libaes.utils.DevelopUtils;
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
import cc.winboll.studio.libaes.views.ADrawerMenuListView;
import cc.winboll.studio.libaes.views.ADsBannerView;
import cc.winboll.studio.libaes.views.ASupportToolbar;
import cc.winboll.studio.libappbase.GlobalApplication;
import cc.winboll.studio.libappbase.LogUtils;
import com.baoyz.widget.PullRefreshLayout;
@@ -174,6 +175,9 @@ public abstract class DrawerFragmentActivity extends AppCompatActivity implement
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (AESThemeUtil.onAppThemeItemSelected(this, item)) {
if (mToolbar instanceof ASupportToolbar) {
((ASupportToolbar) mToolbar).refreshFromTheme();
}
recreate();
} if (DevelopUtils.onDevelopItemSelected(this, item)) {
LogUtils.d(TAG, String.format("onOptionsItemSelected item.getItemId() %d ", item.getItemId()));

View File

@@ -12,6 +12,8 @@ import androidx.appcompat.widget.Toolbar;
import cc.winboll.studio.libaes.R;
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
import cc.winboll.studio.libaes.utils.AESThemeUtil;
import cc.winboll.studio.libaes.views.ASupportToolbar;
import cc.winboll.studio.libappbase.LogUtils;
public class TestASupportToolbarActivity extends AppCompatActivity implements IWinBoLLActivity {
@@ -29,14 +31,17 @@ public class TestASupportToolbarActivity extends AppCompatActivity implements IW
@Override
protected void onCreate(Bundle savedInstanceState) {
LogUtils.d(TAG, "onCreate() start");
AESThemeUtil.applyAppTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testasupporttoolbar);
LogUtils.d(TAG, "setContentView() done");
Toolbar toolbar = findViewById(R.id.activitytestasupporttoolbarASupportToolbar1);
LogUtils.d(TAG, "findViewById() done, toolbar=" + toolbar.getClass().getSimpleName());
setSupportActionBar(toolbar);
LogUtils.d(TAG, "setSupportActionBar() done");
getSupportActionBar().setTitle(TAG);
LogUtils.d(TAG, "setTitle() done");
LogUtils.d(TAG, "onCreate() end");
}
}

View File

@@ -7,15 +7,12 @@ package cc.winboll.studio.libaes.views;
*/
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import androidx.appcompat.widget.Toolbar;
import cc.winboll.studio.libaes.R;
import android.graphics.drawable.Drawable;
import androidx.core.content.ContextCompat;
import android.graphics.PorterDuff;
import cc.winboll.studio.libappbase.LogUtils;
public class ASupportToolbar extends Toolbar {
@@ -25,65 +22,120 @@ public class ASupportToolbar extends Toolbar {
int mStartColor;
int mCenterColor;
int mEndColor;
LayerDrawable ld;
GradientDrawable[] array = new GradientDrawable[3];
//private GradientDrawable gradientDrawable;
LayerDrawable ld;
public ASupportToolbar(Context context) {
super(context);
LogUtils.d(TAG, "ASupportToolbar() constructor");
}
public ASupportToolbar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ASupportToolbar, R.attr.aSupportToolbar, 0);
mTitleTextColor = a.getColor(R.styleable.ASupportToolbar_attrASupportToolbarTitleTextColor, Color.GREEN);
mStartColor = a.getColor(R.styleable.ASupportToolbar_attrASupportToolbarStartColor, Color.BLUE);
mCenterColor = a.getColor(R.styleable.ASupportToolbar_attrASupportToolbarCenterColor, Color.RED);
mEndColor = a.getColor(R.styleable.ASupportToolbar_attrASupportToolbarEndColor, Color.YELLOW);
// 返回一个绑定资源结束的信号给资源
a.recycle();
notifyColorChange();
LogUtils.d(TAG, "ASupportToolbar() attrs constructor");
initStyledAttributes(attrs, R.attr.aSupportToolbar);
}
public ASupportToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LogUtils.d(TAG, "ASupportToolbar() attrs defStyleAttr constructor");
initStyledAttributes(attrs, defStyleAttr);
}
void initStyledAttributes(AttributeSet attrs, int defStyleAttr) {
LogUtils.d(TAG, "initStyledAttributes() start");
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ASupportToolbar, defStyleAttr, 0);
mTitleTextColor = a.getColor(R.styleable.ASupportToolbar_attrASupportToolbarTitleTextColor, 0xFF00FF00);
mStartColor = a.getColor(R.styleable.ASupportToolbar_attrASupportToolbarStartColor, 0xFF03AB4E);
mCenterColor = a.getColor(R.styleable.ASupportToolbar_attrASupportToolbarCenterColor, 0xFF03AB4E);
mEndColor = a.getColor(R.styleable.ASupportToolbar_attrASupportToolbarEndColor, 0xFF3DDC84);
LogUtils.d(TAG, String.format("initStyledAttributes() colors: title=0x%x, start=0x%x, center=0x%x, end=0x%x", mTitleTextColor, mStartColor, mCenterColor, mEndColor));
a.recycle();
setTitleTextColor(mTitleTextColor);
LogUtils.d(TAG, "initStyledAttributes() end");
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
LogUtils.d(TAG, "onAttachedToWindow() start");
refreshFromTheme();
LogUtils.d(TAG, "onAttachedToWindow() end");
}
public void refreshFromTheme() {
LogUtils.d(TAG, "refreshFromTheme() start");
TypedArray a = getContext().obtainStyledAttributes(R.styleable.ASupportToolbar);
try {
mTitleTextColor = a.getColor(R.styleable.ASupportToolbar_attrASupportToolbarTitleTextColor, 0xFF00FF00);
mStartColor = a.getColor(R.styleable.ASupportToolbar_attrASupportToolbarStartColor, 0xFF03AB4E);
mCenterColor = a.getColor(R.styleable.ASupportToolbar_attrASupportToolbarCenterColor, 0xFF03AB4E);
mEndColor = a.getColor(R.styleable.ASupportToolbar_attrASupportToolbarEndColor, 0xFF3DDC84);
LogUtils.d(TAG, String.format("refreshFromTheme() colors: title=0x%x, start=0x%x, center=0x%x, end=0x%x", mTitleTextColor, mStartColor, mCenterColor, mEndColor));
} finally {
a.recycle();
}
postNotifyColorChange();
LogUtils.d(TAG, "refreshFromTheme() end");
}
void postNotifyColorChange() {
LogUtils.d(TAG, "postNotifyColorChange()");
removeCallbacks(mRefreshRunnable);
post(mRefreshRunnable);
}
Runnable mRefreshRunnable = new Runnable() {
@Override
public void run() {
LogUtils.d(TAG, "mRefreshRunnable.run() start");
notifyColorChange();
LogUtils.d(TAG, "mRefreshRunnable.run() end");
}
};
void notifyColorChange() {
// 工具栏描边
LogUtils.d(TAG, "notifyColorChange() start");
LogUtils.d(TAG, String.format("notifyColorChange() size: width=%d, height=%d", getWidth(), getHeight()));
int nWidth = getWidth();
int nHeight = getHeight();
if (nWidth == 0 || nHeight == 0) {
LogUtils.d(TAG, "notifyColorChange() skipped: width or height is 0");
return;
}
int nStroke = 5;
//分别为开始颜色,中间夜色,结束颜色
int colors0[] = { mEndColor , mCenterColor, mStartColor};
GradientDrawable gradientDrawable0;
int colors0[] = { mEndColor , mCenterColor, mStartColor };
array[2] = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors0);
gradientDrawable0 = array[2];
gradientDrawable0.setShape(GradientDrawable.RECTANGLE);
gradientDrawable0.setColors(colors0); //添加颜色组
gradientDrawable0.setGradientType(GradientDrawable.LINEAR_GRADIENT);//设置线性渐变
gradientDrawable0.setCornerRadius(20);
array[2].setShape(GradientDrawable.RECTANGLE);
array[2].setColors(colors0);
array[2].setGradientType(GradientDrawable.LINEAR_GRADIENT);
array[2].setCornerRadius(20);
int colors1[] = { mCenterColor , mCenterColor, mCenterColor };
GradientDrawable gradientDrawable1;
int colors1[] = { mCenterColor, mCenterColor, mCenterColor };
array[1] = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors1);
gradientDrawable1 = array[1];
gradientDrawable1.setShape(GradientDrawable.RECTANGLE);
gradientDrawable1.setColors(colors1); //添加颜色组
gradientDrawable1.setGradientType(GradientDrawable.LINEAR_GRADIENT);//设置线性渐变
gradientDrawable1.setCornerRadius(20);
array[1].setShape(GradientDrawable.RECTANGLE);
array[1].setColors(colors1);
array[1].setGradientType(GradientDrawable.LINEAR_GRADIENT);
array[1].setCornerRadius(20);
int colors2[] = { mEndColor, mCenterColor, mStartColor };
GradientDrawable gradientDrawable2;
int colors2[] = { mEndColor, mCenterColor, mStartColor };
array[0] = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors2);
gradientDrawable2 = array[0];
gradientDrawable2.setShape(GradientDrawable.RECTANGLE);
gradientDrawable2.setColors(colors2); //添加颜色组
gradientDrawable2.setGradientType(GradientDrawable.LINEAR_GRADIENT);//设置线性渐变
gradientDrawable2.setCornerRadius(20);
array[0].setShape(GradientDrawable.RECTANGLE);
array[0].setColors(colors2);
array[0].setGradientType(GradientDrawable.LINEAR_GRADIENT);
array[0].setCornerRadius(20);
ld = new LayerDrawable(array); //参数为上面的Drawable数组
ld.setLayerInset(2, nStroke * 2, nStroke * 2, getWidth() + nStroke * 2, getHeight() + nStroke * 2);
ld.setLayerInset(1, nStroke, nStroke, getWidth() + nStroke, getHeight() + nStroke);
ld.setLayerInset(0, 0, 0, getWidth(), getHeight());
ld = new LayerDrawable(array);
ld.setLayerInset(2, nStroke * 2, nStroke * 2, nWidth + nStroke * 2, nHeight + nStroke * 2);
ld.setLayerInset(1, nStroke, nStroke, nWidth + nStroke, nHeight + nStroke);
ld.setLayerInset(0, 0, 0, nWidth, nHeight);
setBackgroundDrawable(ld);
setTitleTextColor(mTitleTextColor);
setSubtitleTextColor(mTitleTextColor);
LogUtils.d(TAG, "notifyColorChange() end");
}
}
}

View File

@@ -43,6 +43,7 @@
<attr name="attrASupportToolbarStartColor"/>
<attr name="attrASupportToolbarCenterColor"/>
<attr name="attrASupportToolbarEndColor"/>
<attr name="android:background"/>
</declare-styleable>
<declare-styleable name="AButton">