Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic gradient background
    primarykey
    data
    text
    <p>In my application i had a pretty cool idea of changing the background (as gradient) to other gradient colors when an event occured. It all had to happen <code>dynamically</code>, and i spent a couple of hours on this. But it doesn't work because it throws a <code>stackOverflow error</code> when i am using the following tactic.</p> <ul> <li>Set gradientbackground of linearLayout.</li> <li>Convert Hex digits to RGB.</li> <li>Calculate new RGB value.</li> <li>convert back to Hex value.</li> <li>put new hex value as background.</li> </ul> <p>So this background will slightly change during run-time when you press on a button.</p> <p>My question will be, is there a different approach i can use to accomplish this? It is easily possible with a solid colored background, but this isn't what i want.</p> <p>To draw the gradient background i have used:</p> <pre><code> GradientDrawable g = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, Colors); //Colors is an array with 2 heximal values as int. lnrLayout.setBackgroundDrawable(g); </code></pre> <p>With the following piece of code the RGB values are calculated.</p> <pre><code> int[] ret = new int[3]; int[] ret2 = new int[3]; for(int i=0; i&lt;3; i++){ ret[i] = hexToInt(rgb1.charAt(i*2+2), rgb1.charAt(i*2+1+2)); ret2[i] = hexToInt(rgb2.charAt(i*2+2), rgb2.charAt(i*2+1+2)); } </code></pre> <p>The hexToInt method,</p> <pre><code> public int hexToInt(char a, char b){ int x = a &lt; 65 ? a-48 : a-55; int y = b &lt; 65 ? b-48 : b-55; return x*16+y; } </code></pre> <p>Then i calculated the next rgb code with a simple piece of code and some if-loops were i have setted the values of the <code>ret</code> array from above. Then i wanted to create a new hex value of the new RGB values with,</p> <pre><code> String hex = makeNewHex(ret[0],ret[1],ret[2]); String hex2 = makeNewHex(ret2[0],ret2[1],ret2[2]); public String makeNewHex(int r, int g, int b) { String n; return n = toHex(r,g,b); } public String toHex(int r, int g, int b) { return "#" + toBrowserHexValue(r) + toBrowserHexValue(g) + toBrowserHexValue(b); } private static String toBrowserHexValue(int number) { StringBuilder builder = new StringBuilder(Integer.toHexString(number &amp; 0xff)); while (builder.length() &lt; 2) { builder.append("0"); } return builder.toString().toUpperCase(); } </code></pre> <p>After this the code isn't that importing anymore, because i just had to tweak the hex value a bit, so its compatible to the <code>gradientDrawable</code> method.</p> <p>But i have the feeling i did a little too hard my best.. and there is a way more easier way to accomplish this.</p> <p>For your information, i did not use a timer thread to update the UI, but a <code>RefreshHandler</code>.</p> <p>Someone know how i can do this more efficiently, or with a better tactic so i won't get the <code>stackoverflow error</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload