Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何动态改变形状颜色?_Android_Android Layout_Xamarin.android_Shape - Fatal编程技术网

Android 如何动态改变形状颜色?

Android 如何动态改变形状颜色?,android,android-layout,xamarin.android,shape,Android,Android Layout,Xamarin.android,Shape,我有 所以现在我想让这个形状根据我从web服务调用中得到的信息改变颜色。所以它可能是黄色、绿色、红色或其他颜色,这取决于我从网络服务电话中收到的颜色 如何更改形状的颜色?基于此信息?您可以用Java构建自己的形状。 我为一个类似iPhone的页面控制器做了这项工作,并用Java绘制了形状: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/r

我有


所以现在我想让这个形状根据我从web服务调用中得到的信息改变颜色。所以它可能是黄色、绿色、红色或其他颜色,这取决于我从网络服务电话中收到的颜色


如何更改形状的颜色?基于此信息?

您可以用Java构建自己的形状。 我为一个类似iPhone的页面控制器做了这项工作,并用Java绘制了形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <solid
       android:color="#FFFF00" />
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
</shape>

<TextView
    android:background="@drawable/test"
    android:layout_height="45dp"
    android:layout_width="100dp"
    android:text="Moderate"
/>
希望这有帮助。
格里兹·费边

你可以这样简单地修改它

/**
 * Builds the active and inactive shapes / drawables for the page control
 */
private void makeShapes() {

    activeDrawable = new ShapeDrawable();
    inactiveDrawable = new ShapeDrawable();
    activeDrawable.setBounds(0, 0, (int) mIndicatorSize,
            (int) mIndicatorSize);
    inactiveDrawable.setBounds(0, 0, (int) mIndicatorSize,
            (int) mIndicatorSize);

    int i[] = new int[2];
    i[0] = android.R.attr.textColorSecondary;
    i[1] = android.R.attr.textColorSecondaryInverse;
    TypedArray a = this.getTheme().obtainStyledAttributes(i);

    Shape s1 = new OvalShape();
    s1.resize(mIndicatorSize, mIndicatorSize);
    Shape s2 = new OvalShape();
    s2.resize(mIndicatorSize, mIndicatorSize);

    ((ShapeDrawable) activeDrawable).getPaint().setColor(
            a.getColor(0, Color.DKGRAY));
    ((ShapeDrawable) inactiveDrawable).getPaint().setColor(
            a.getColor(1, Color.LTGRAY));

    ((ShapeDrawable) activeDrawable).setShape(s1);
    ((ShapeDrawable) inactiveDrawable).setShape(s2);
}

对我来说,它崩溃是因为
getBackground
返回了
GradientDrawable
而不是
ShapeDrawable

所以我把它改成这样:

GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);
   circleColor = (ImageView) view.findViewById(R.id.circleColor);
   int color = Color.parseColor("#00FFFF");
   ((GradientDrawable)circleColor.getBackground()).setColor(color);

这对我来说是可行的,有一个初始xml资源:

    LayerDrawable bgDrawable = (LayerDrawable) button.getBackground();
    final GradientDrawable shape = (GradientDrawable)
            bgDrawable.findDrawableByLayerId(R.id.round_button_shape);
    shape.setColor(Color.BLACK);

上述结果:

此解决方案适用于我使用android sdk v19的情况:

example.setBackgroundResource(R.drawable.myshape);
GradientDrawable gd = (GradientDrawable) example.getBackground().getCurrent();
gd.setColor(Color.parseColor("#000000"));
gd.setCornerRadii(new float[]{30, 30, 30, 30, 0, 0, 30, 30});
gd.setStroke(2, Color.parseColor("#00FFFF"), 5, 6);

如果您有这样的imageView:

//get the image button by id
ImageButton myImg = (ImageButton) findViewById(R.id.some_id);

//get drawable from image button
GradientDrawable drawable = (GradientDrawable) myImg.getDrawable();

//set color as integer
//can use Color.parseColor(color) if color is a string
drawable.setColor(color)

也许其他人需要更改XML中的颜色,而不需要像我所需要的那样创建多个Drawable。然后制作一个无颜色可绘制的圆,然后为ImageView指定背景色

circle.xml

ImageView iv = (ImageView)findViewById(R.id.color_button);
GradientDrawable bgShape = (GradientDrawable)iv.getDrawable();
bgShape.setColor(Color.BLACK);
(textView.getBackground()).setColorFilter(Color.parseColor("#FFDE03"), PorterDuff.Mode.SRC_IN);

在您的布局中

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
</shape>

编辑:

<shape> ... </shape>
<TextView
    android:id="@+id/textView"
    android:background="@drawable/test"
    android:layout_height="45dp"
    android:layout_width="100dp"
    android:text="Moderate"/>
有一个关于这个方法的问题,阻止它在Android棒棒糖5.0(API级别21)上工作。但已在较新版本中修复。

我的形状xml:

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/circle"
    android:backgroundTint="@color/red"/>
结果图片:

circle.xml(可绘制)


我尝试了罗尼的答案,我的应用程序崩溃了。然后我检查我的可绘制xml。看起来是这样的:

GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);
   circleColor = (ImageView) view.findViewById(R.id.circleColor);
   int color = Color.parseColor("#00FFFF");
   ((GradientDrawable)circleColor.getBackground()).setColor(color);
。。。
。我将其更改为:(也更改了属性)

。。。
它起作用了


对于遇到相同问题的人。

半径填充形状的最简单方法是:

XML:

<shape> ... </shape>
<TextView
    android:id="@+id/textView"
    android:background="@drawable/test"
    android:layout_height="45dp"
    android:layout_width="100dp"
    android:text="Moderate"/>

可绘制的矩形.xml

ImageView iv = (ImageView)findViewById(R.id.color_button);
GradientDrawable bgShape = (GradientDrawable)iv.getDrawable();
bgShape.setColor(Color.BLACK);
(textView.getBackground()).setColorFilter(Color.parseColor("#FFDE03"), PorterDuff.Mode.SRC_IN);

TextView

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@android:color/transparent" />

    <stroke
        android:width="1dp"
        android:color="#9f9f9f" />

    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

</shape>

公共静态void changeRectangleColor(视图,int颜色){
((GradientDrawable)view.getBackground()).setStroke(1,颜色);
}    
changeRectangleColor(textView,ContextCompat.getColor(context,R.color.colorAccent));

您可以使用绑定适配器(Kotlin)来实现这一点。创建名为ChangeShapeColor的绑定适配器类,如下所示

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/drawable_rectangle"
    android:gravity="center"
    android:padding="10dp"
    android:text="Status"
    android:textColor="@android:color/white"
    android:textSize="20sp" />


public static void changeRectangleColor(View view, int color) {
    ((GradientDrawable) view.getBackground()).setStroke(1, color);
}    

changeRectangleColor(textView, ContextCompat.getColor(context, R.color.colorAccent));
在res/drawable文件夹中创建可绘制形状。我已经创建了一个圆

@BindingAdapter("shapeColor")

// Method to load shape and set its color 

fun loadShape(textView: TextView, color: String) {
// first get the drawable that you created for the shape 
val mDrawable = ContextCompat.getDrawable(textView.context, 
R.drawable.language_image_bg)
val  shape =   mDrawable as (GradientDrawable)
// use parse color method to parse #34444 to the int 
shape.setColor(Color.parseColor(color))
 }

最后,请参考您的观点

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#anyColorCode"/>

<size
    android:width="@dimen/dp_16"
    android:height="@dimen/dp_16"/>
</shape>

.........
app:shapeColor=“@{modelName.colorString}”

什么是btn.getBackground?我得到了
java.lang.ClassCastException:android.graphics.drawable.GradientDrawable无法强制转换为android.graphics.drawable.ShapeDrawable
尝试此建议时。无效。您将得到一个强制转换错误。需要一个修复或另一个答案接受这项工作很好。之前的评论已过期,因为答案已被编辑!应该使用GradientDrawable bgShape=(GradientDrawable)btn.getBackground().getCurrent();由@Couitchy method
View指定。getBackground()
返回一个
GradientDrawable
,而不是一个
ShapeDrawable
,由于尝试获取引用和以编程方式设置颜色时无效的强制转换,导致应用程序在运行时崩溃。[Android Shape doc]()声明:已编译的资源数据类型:指向
GradientDrawable
的资源指针。正确答案可能与此重复。以上两个答案对我都不适用。我在这两个方面都遇到了一个例外。这不是一个完美的解决方案,但帮助我获得了解决方案。圆形按钮形状应该在形状xml文件中定义在哪里?
@BindingAdapter("shapeColor")

// Method to load shape and set its color 

fun loadShape(textView: TextView, color: String) {
// first get the drawable that you created for the shape 
val mDrawable = ContextCompat.getDrawable(textView.context, 
R.drawable.language_image_bg)
val  shape =   mDrawable as (GradientDrawable)
// use parse color method to parse #34444 to the int 
shape.setColor(Color.parseColor(color))
 }
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#anyColorCode"/>

<size
    android:width="@dimen/dp_16"
    android:height="@dimen/dp_16"/>
</shape>
  <TextView>
   .........
  app:shapeColor="@{modelName.colorString}"
 </Textview>