Android - dynamically adjust the gradient

I have the opportunity to extract from an XML resource, and I want to use this opportunity, but dynamically set the color of the gradient. So far, I have something like this:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="3dip"> </corners> <gradient android:angle="90" android:type="linear" android:startColor="#FFFFFFFF" android:centerColor="#FFFF0000" android:endColor="#FFFF0000"> </gradient> </shape> 

Now I realized that I can make colors dynamically by getting the ability to paint at runtime, dropping it as a GradientDrawable and using the method to set the colors. However, GradientDrawable does not have such a method, and you can only specify colors in the constructor. It is very strange to me that this is so, because all other aspects of the gradient can be set. Is there an easier way than overriding onDraw () and making the gradient itself? Some of the classes I'm trying to use are very poorly documented.

+4
source share
3 answers

You can set as wallpaper for presentation dynamically.

view.setBackgroundDrawable (R.drawable.your_drawable_id);

0
source

Resources are mostly static and usually not changeable. Some types of resources allow you to "clone" a mutable copy. GradientDrawable allows you to set colors in the constructor (as you discovered), so you need to create them internally if you want to dynamically control colors at runtime or, even better, choose one of a fixed number of backgrounds from instead. As mentioned earlier, use setBackgroundDrawable() to set the background during runtime. No need to make a decision, just Get-R-Done!

0
source

Create the GradientDrawable class as follows:

 public class RoundedDrawable extends GradientDrawable { public RoundedDrawable(int shape, int solidColor, int strokeWidth, int strokeColor, float[] fourRadii) { this.mutate(); this.setShape(shape); this.setColor(solidColor); this.setStroke(strokeWidth, strokeColor); this.setCornerRadii(fourRadii); } } 

Now use this in your activity as follows:

public class AAActivity extends the action {

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_transaction_layout); RoundedDrawable customBg; RelativeLayout relList = (RelativeLayout) findViewById(R.id.relList); float radii[]={5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f}; customBg = new RoundedDrawable(GradientDrawable.RECTANGLE,Color.parseColor("#FFFFFF"), 2, Color.parseColor("#8C8C8C"),radii); relList.setBackgroundDrawable(customBg); LinearLayout linearItemsRow = (LinearLayout) findViewById(R.id.linearItemsRow); float[] rowRadii={5.0f, 5.0f, 5.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f}; customBg = new RoundedDrawable(GradientDrawable.RECTANGLE,Color.parseColor("#CBCBCB"), 0, 0, rowRadii); linearItemsRow.setBackgroundDrawable(customBg); } 

}

Hope this helps.

0
source

Source: https://habr.com/ru/post/1342833/


All Articles