【问题标题】:How to set and get tint color on SVG programmaticaly in android?如何在 android 中以编程方式在 SVG 上设置和获取色调颜色?
【发布时间】:2020-06-06 20:08:48
【问题描述】:

我有一个ImageView,它有一个 SVG 作为源示例:android:src="@drawable/bold_svg"

现在单击时我想将色调颜色设置为color accent 或将其返回给white。两种状态。

我尝试过的:

    myImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int currentColor, colorAccent;
            currentColor=ImageViewCompat.getImageTintList(myImageView).getDefaultColor();
            colorAccent=getResources().getColor(R.color.colorAccent);
            if (currentColor==colorAccent) {
                myImageView.setColorFilter(getResources().getColor(R.color.white_text_color));
            } else {
                myImageView.setColorFilter(getResources().getColor(R.color.colorAccent));
            }
        }
    });

看起来每次我点击按钮时,currentColor 都不会改变,所以else 是唯一被调用的东西!我错过了什么?

【问题讨论】:

    标签: java android tint


    【解决方案1】:

    我设法找到了这样的答案:

        myImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int currentColor, colorAccent;
                currentColor=ImageViewCompat.getImageTintList(myImageView).getDefaultColor();
                colorAccent=getResources().getColor(R.color.colorAccent);
                if (currentColor==colorAccent) {
                    ImageViewCompat.setImageTintList(ivBold, ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.white_text_color)));
                } else {
                    ImageViewCompat.setImageTintList(myImageView, ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)));
                }
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      • 2022-11-11
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      相关资源
      最近更新 更多