Ich redigiere, um die Frage zu vereinfachen, und hoffe, dass dies zu einer genauen Antwort führt.
Angenommen, ich habe die folgende oval
-Form:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:Android="http://schemas.Android.com/apk/res/Android" Android:shape="oval">
<solid Android:angle="270"
Android:color="#FFFF0000"/>
<stroke Android:width="3dp"
Android:color="#FFAA0055"/>
</shape>
Wie kann ich die Farbe innerhalb einer Aktivitätsklasse programmgesteuert einstellen?
Hinweis: Die Antwort wurde aktualisiert, um das Szenario abzudecken, in dem background
eine Instanz von ColorDrawable
ist. Vielen Dank Tyler Pfaff für diesen Hinweis.
Das Drawable ist ein Oval und ist der Hintergrund einer ImageView
Rufen Sie die Drawable
von imageView
mit getBackground()
ab:
Drawable background = imageView.getBackground();
Gegen übliche Verdächtige prüfen:
if (background instanceof ShapeDrawable) {
// cast to 'ShapeDrawable'
ShapeDrawable shapeDrawable = (ShapeDrawable) background;
shapeDrawable.getPaint().setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
} else if (background instanceof GradientDrawable) {
// cast to 'GradientDrawable'
GradientDrawable gradientDrawable = (GradientDrawable) background;
gradientDrawable.setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
} else if (background instanceof ColorDrawable) {
// alpha value may need to be set again after this call
ColorDrawable colorDrawable = (ColorDrawable) background;
colorDrawable.setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
}
Kompakte Version:
Drawable background = imageView.getBackground();
if (background instanceof ShapeDrawable) {
((ShapeDrawable)background).getPaint().setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
} else if (background instanceof GradientDrawable) {
((GradientDrawable)background).setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
} else if (background instanceof ColorDrawable) {
((ColorDrawable)background).setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
}
Beachten Sie, dass eine Nullprüfung nicht erforderlich ist.
Sie sollten jedoch mutate()
für die Zeichenobjekte verwenden, bevor Sie sie ändern, wenn sie an anderer Stelle verwendet werden. (Standardmäßig haben Drawables, die aus XML geladen werden, denselben Status.)
Mach das so:
ImageView imgIcon = findViewById(R.id.imgIcon);
GradientDrawable backgroundGradient = (GradientDrawable)imgIcon.getBackground();
backgroundGradient.setColor(getResources().getColor(R.color.yellow));
Eine einfachere Lösung wäre heutzutage, Ihre Form als Hintergrund zu verwenden und dann programmgesteuert ihre Farbe über zu ändern
view.getBackground().setColorFilter(Color.parseColor("#343434"), PorterDuff.Mode.SRC_ATOP)
EDIT:
Ich habe die Antwort auf PorterDuff.Mode.SRC_ATOP
Aktualisiert. Hier ist die offizielle Dokumentation mit mehr Details zu den verschiedenen PorterDuff.Mode
Optionen, die Sie haben.
Versuche dies:
public void setGradientColors(int bottomColor, int topColor) {
GradientDrawable gradient = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]
{bottomColor, topColor});
gradient.setShape(GradientDrawable.RECTANGLE);
gradient.setCornerRadius(10.f);
this.setBackgroundDrawable(gradient);
}
für mehr detail überprüfen sie diesen link das
hoffe hilfe.
ich hoffe, das hilft jemandem, der das gleiche Problem hat
GradientDrawable Gd = (GradientDrawable) YourImageView.getBackground();
//To shange the solid color
Gd.setColor(yourColor)
//To change the stroke color
int width_px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, youStrokeWidth, getResources().getDisplayMetrics());
Gd.setStroke(width_px, yourColor);
Erweitern Sie die Option Vikram's , wenn Sie dynamische Ansichten, wie zum Beispiel Elemente der Wiederverwertungsanlage usw., färben. Wenn Sie dies nicht tun, werden Ansichten, die ein gemeinsames Zeichenelement haben (d. H. Ein Hintergrund), ebenfalls geändert/farblich dargestellt.
public static void setBackgroundColorAndRetainShape(final int color, final Drawable background) {
if (background instanceof ShapeDrawable) {
((ShapeDrawable) background.mutate()).getPaint().setColor(color);
} else if (background instanceof GradientDrawable) {
((GradientDrawable) background.mutate()).setColor(color);
} else if (background instanceof ColorDrawable) {
((ColorDrawable) background.mutate()).setColor(color);
}else{
Log.w(TAG,"Not a valid background type");
}
}
dies ist die Lösung, die für mich funktioniert ... schrieb es auch in eine andere Frage: Wie kann ich die Formfarbe dynamisch ändern?
//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)
Diese Frage wurde vor einiger Zeit beantwortet, kann aber durch Umschreiben als Kotlin-Erweiterungsfunktion modernisiert werden.
fun Drawable.overrideColor(@ColorInt colorInt: Int) {
when (this) {
is GradientDrawable -> setColor(colorInt)
is ShapeDrawable -> Paint.color = colorInt
is ColorDrawable -> color = colorInt
}
}
Der einfache Weg, die Form mit dem Radius zu füllen, ist:
(view.getBackground()).setColorFilter(Color.parseColor("#FFDE03"), PorterDuff.Mode.SRC_IN);
Nichts funktioniert für mich, aber wenn ich die Tönungsfarbe setze, funktioniert es mit Shape Drawable
Drawable background = imageView.getBackground();
background.setTint(getRandomColor())
erfordern Android 5.0 API 21