android - 如何在运行时更改自定义 View 颜色?

标签 android view drawable

我的类定义如下:

public class Island extends View {
    private ShapeDrawable mDrawable;

    public Island(Context context) {
        super(context);
        int width = 50;
        int height = 50;

        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds(0, 0, width, height);
    }

    public void change() {
        mDrawable.getPaint().setColor(Color.BLACK);
    }

    protected void onDraw(Canvas canvas) {
        mDrawable.draw(canvas);
    }

为什么当我在对象上调用 change() 时形状不会改变颜色?谢谢。

最佳答案

您需要调用 invalidate 以便 View 知道它需要重绘

关于android - 如何在运行时更改自定义 View 颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26766328/

相关文章:

php - 当我使用 httpPost 从 android 设备调用它时,我的 php 代码出错

html - Rails current_page? View 上的 UrlGenerationError

html - Css 布局 Bootstrap 覆盖

android - 在不使用两个可绘制对象的情况下按下时突出显示按钮?

Android:在运行时更改形状颜色

android - 如何从android中的USB驱动器读取文件?

android - 他们是一种手动重新生成推送通知 token 的方法(iOS、Android、Windows)

android - Android 中的 AAC 格式

sql - 我可以从另一个数据库服务器在我的数据库服务器中创建 View 吗

java - BitmapFactory类型中的方法decodeResource(Resources, int)不适用于Android中的参数(Resources, String)