我有一个Android SVG图片。根据给出的输入,我想改变不同部位的颜色,如头部,右臂和左臂,眼睛和身体。
例如,当输入为"< = 30"时,SVG图像的任何部分都应变为红色。如果输入为"< = 60&#34 ;,则SVG图像的任何部分都应变为黑色。当输入为"> 60"时,SVG图像的任何部分都应转向品红。我在XML格式下保存了SVG图像," Drawable / android_pic"。那么如何更改SVG图像中不同部分/元素的颜色呢?
问题是,我无法改变SVG图像不同部分的颜色 这是我的编码。
$id = "";
$key = "id";
foreach($results as $result) {
$id .= $result[$key] . ",";
}
$id = rtrim($id, ",");
echo $id;
我以XML格式保存了图像。这是Android图像的XML编码。
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);
}
}
});
}
}`
答案 0 :(得分: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);