1

我按照一些例子画了一个圆圈,但是当我改变颜色时,圆圈变成了一个正方形。颜色应用于视图。如何更改具有“@+id/circle2”颜色的实际圆圈,而不是包含该圆圈的投射视图。

圈子.xml

 <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"
android:id="@+id/circle2" > >
<gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
android:angle="270"/>
 </shape>

这是在主 xml

        <View
    android:id="@+id/colordot"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_marginLeft="325dp"
    android:layout_marginTop="35dp"
    android:background="@drawable/circle" />

我投射了包含 circle.xml 的视图

    F1Color =(View) findViewById(R.id.colordot);

我改变颜色

F1Color.setBackgroundColor(Color.rgb(R_value,G_value,B_value));

这个很好用,谢谢

private void SetColorDot(int index, int i, int j, int k)

                Resources res = getResources();
        final Drawable drawable = res.getDrawable(R.drawable.circle);
        drawable.setColorFilter(Color.rgb(i, j, k), Mode.SRC_ATOP);
        ImageView img = (ImageView) findViewById(R.id.colordot1);
        img.setBackgroundDrawable(drawable);
4

1 回答 1

0

当您使用 时View.setBackgroundColor(color);,这会将您设置为背景的前一个替换为新的,这就是您看到方形的原因。DrawableColorDrawable

一种可能的解决方案可能是colordot从 a更改ViewImageView- 这将允许您使用setColorFilter(). 您可以在 中使用您的 RGB 颜色ColorFilter重新着色您的圆圈,即:

F1Color = (ImageView) findViewById(R.id.colordot);
F1Color.setColorFilter(new LightingColorFilter(Color.BLACK, Color.rgb(R_value, G_value, B_value));
于 2013-11-11T01:13:21.407 回答