How to set and get tint color on SVG programmaticaly in android?

这一生的挚爱 提交于 2021-02-11 17:37:57

问题


I have an ImageView which has an SVG as source example: android:src="@drawable/bold_svg".

Now on click I want to set the tint color to color accent or return it to white. Two states.

What I have tried:

    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));
            }
        }
    });

It looks like every time I click the button, currentColor it does not change, so else is the only thing that is called! What am I missing?


回答1:


I managed to find the answer something like this would work:

    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)));
            }
        }
    });


来源:https://stackoverflow.com/questions/62237431/how-to-set-and-get-tint-color-on-svg-programmaticaly-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!