【问题标题】:to change color of each element in SVG image更改 SVG 图像中每个元素的颜色
【发布时间】:2015-05-18 18:30:45
【问题描述】:

我有一个 Android SVG 图像。我想根据给定的输入更改不同部分的颜色,例如头部、右臂和左臂、眼睛和身体。

例如,当输入为“60”时,SVG 图像的任何部分都应变为洋红色。我将 SVG 图像以 XML 格式保存在“Drawable/android_pic”下。那么如何改变 SVG 图像中不同部分/元素的颜色呢?

问题是,我无法更改 SVG 图像不同部分的颜色。
这是我的编码。

 public class MainActivity extends Activity {

        EditText et,et2,et3;
        ImageView img1,img2,img3;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            final ImageView img1 = (ImageView) findViewById(R.id.android1);//change color of android logo
            final ImageView img2 = (ImageView) findViewById(R.id.arms);//change color of arms
            final ImageView img3 = (ImageView) findViewById(R.id.head_eyes);//change color of head and eyes parts


            final EditText et = (EditText) findViewById(R.id.editText);
            final EditText et2 = (EditText) findViewById(R.id.editText2);
            final EditText et3 = (EditText) findViewById(R.id.editText3);

            et.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
            et.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {

                    int i = Integer.parseInt(s.toString());
                    if (i <= 30) {
                        //img1.setBackgroundColor(Color.BLUE);
                        img1.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
                    } else if (i <= 60) {
                        img1.getDrawable().setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);

                    } else if (i > 60) {

                        img1.getDrawable().setColorFilter(Color.MAGENTA, PorterDuff.Mode.MULTIPLY);
                    }
                }
            });

            //change color of arms
            et2.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
            et2.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {

                    int i = Integer.parseInt(s.toString());
                    if (i <= 30)
                    {
                        img2.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
                    }
                    else if (i <= 60)
                    {
                        img2.getDrawable().setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
                       }
                    else if (i > 60) {

                        img2.getDrawable().setColorFilter(Color.MAGENTA, PorterDuff.Mode.MULTIPLY);

                    }
                }
            });

                    //change color of head and eyes parts

            et3.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
            et3.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {

                    int i = Integer.parseInt(s.toString());
                    if (i <= 30)
                    {
                        img3.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
                    }
                    else if (i <= 60)
                    {
                        img3.getDrawable().setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
                    }
                    else if (i > 60) {

                        img3.getDrawable().setColorFilter(Color.MAGENTA, PorterDuff.Mode.MULTIPLY);

                    }
                }
            });


        }
        }`

我以 XML 格式保存了图像。这是 Android 图像的 XML 编码。

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:viewportWidth="500"
    android:viewportHeight="500"
    android:width="500px"
    android:height="500px">

    <group android:name="android1">
        <group android:name="head_eyes">
            <path
                android:name="head"
                android:fillColor="#9FBF3B"
                android:pathData="M301.314,83.298l20.159-29.272c1.197-1.74,0.899-4.024-0.666-5.104c-1.563-1.074-3.805-0.543-4.993,1.199L294.863,80.53c-13.807-5.439-29.139-8.47-45.299-8.47c-16.16,0-31.496,3.028-45.302,8.47l-20.948-30.41c-1.201-1.74-3.439-2.273-5.003-1.199c-1.564,1.077-1.861,3.362-0.664,5.104l20.166,29.272c-32.063,14.916-54.548,43.26-57.413,76.34h218.316C355.861,126.557,333.375,98.214,301.314,83.298" />
            <path
                android:name="left_eye"
                android:fillColor="#FFFFFF"
                android:pathData="M203.956,129.438c-6.673,0-12.08-5.407-12.08-12.079c0-6.671,5.404-12.08,12.08-12.08c6.668,0,12.073,5.407,12.073,12.08C216.03,124.03,210.624,129.438,203.956,129.438" />
            <path
                android:name="right_eye"
                android:fillColor="#FFFFFF"
                android:pathData="M295.161,129.438c-6.668,0-12.074-5.407-12.074-12.079c0-6.673,5.406-12.08,12.074-12.08c6.675,0,12.079,5.409,12.079,12.08C307.24,124.03,301.834,129.438,295.161,129.438" />
        </group>
        <group android:name="arms">
            <path
                android:name="left_arm"
                android:fillColor="#9FBF3B"
                android:pathData="M126.383,297.598c0,13.45-10.904,24.354-24.355,24.354l0,0c-13.45,0-24.354-10.904-24.354-24.354V199.09c0-13.45,10.904-24.354,24.354-24.354l0,0c13.451,0,24.355,10.904,24.355,24.354V297.598z" />
            <path
                android:name="right_arm"
                android:fillColor="#9FBF3B"
                android:pathData="M372.734,297.598c0,13.45,10.903,24.354,24.354,24.354l0,0c13.45,0,24.354-10.904,24.354-24.354V199.09c0-13.45-10.904-24.354-24.354-24.354l0,0c-13.451,0-24.354,10.904-24.354,24.354V297.598z" />
        </group>
        <path
            android:name="body"
            android:fillColor="#9FBF3B"
            android:pathData="M140.396,175.489v177.915c0,10.566,8.566,19.133,19.135,19.133h22.633v54.744c0,13.451,10.903,24.354,24.354,24.354c13.451,0,24.355-10.903,24.355-24.354v-54.744h37.371v54.744c0,13.451,10.902,24.354,24.354,24.354s24.354-10.903,24.354-24.354v-54.744h22.633c10.569,0,19.137-8.562,19.137-19.133V175.489H140.396z" />
    </group>
</vector>

`

【问题讨论】:

  • 看起来您正在为每个身体部位使用单独的 ImageView。是对的吗? ImageViews 也不支持 SVG drawable,那么你是如何加载 SVG 的呢?
  • @PaulLeBeau 也许她正在使用新的VectorDrawable,它是由 Lollipop 引入的。她错误地认为它是 SVG。嗯,它类似,但它只是SVG的subset(它只使用path元素)。
  • 是的。它不是 ImageView。实际上编码有点错误。我只是尝试过这种方式。因为我不知道如何单独更改。我正在使用 VectorDrawable。当然,我认为它是 SVG。那么,是否有可能通过使用路径元素或组名/id 来单独更改颜色??

标签: android svg colors


【解决方案1】:

你可以做你想做的事。点击链接https://medium.com/@emmaguy/dynamically-changing-svg-colours-on-android-b026a99137ad#.7m1nxdfcb。如前所述,创建具有不同颜色的主题并更改主题

    final ContextThemeWrapper wrapper1 = new ContextThemeWrapper(this, R.style.Theme1);
    final Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.analytics, wrapper1.getTheme());
    ImageView imageView = (ImageView) findViewById(R.id.my_image);
    imageView.setImageDrawable(drawable);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-07
    • 2017-09-20
    • 1970-01-01
    • 2012-04-08
    • 2020-10-31
    • 2018-01-25
    • 1970-01-01
    相关资源
    最近更新 更多