?? Time passing quickly, tomorrow finally can get the house, pay the rent, you can also become a landlord, look at the blog has not been updated, recently has been in the whole robot, there is not too much time to organize the blog.
?? This afternoon with colleagues encountered a problem, that is, obviously did not change the alpha value of a control, but the transparency of this control actually changed. It was strange that the great God had raised the question in the StackOverflow, and in the end there was another great God who gave the correct reply.
?? Finally we know is Android drawable mutations some small details of the problem, specific can refer to a drawable mutations very good article.
?? In fact Android in order to save memory, the resources to share, if the two control drawable use the same resources, may be the same picture resources, may be the same color, or other.
?? Perhaps not very understanding, so let's take an example, first we create a new activity layout file, as follows:
<button android:id = "@+id/test1" android:layout_width =< Span class= "Hljs-value" > "80DP" android:layout_height =" 100DP " android:background =" @ Android:color/holo_green_dark " android:text = "tes1" /> <button android:id = "@+id/test2" android:layout_width =< Span class= "Hljs-value" > "100DP" android:layout_height =" 80DP " android:layout_margintop = "10DP" android:background = "@android: Color/holo_green_ Dark " android:text =" test2 "/> <SeekBarandroid:id= "@+id/seekbar_1"android:layout_margintop=" 10DP "android:layout_width="match_parent "android:layout_height=" Wrap_ Content "android:max=" 255 "/>
?? Omitted here, only the desired control is shown, where test1 and test2 background are the same, and then seeker is used later in order to change the transparency. Then start writing the test code:
Button test1 = (button) Findviewbyid (R.ID.TEST1); Button test2 = (button) Findviewbyid (R.ID.TEST2); SeekBar SeekBar = (SeekBar) Findviewbyid (r.id.seekbar_1); Seekbar.setonseekbarchangelistener (NewSeekbar.onseekbarchangelistener () {@Override Public void onprogresschanged(SeekBar SeekBar,intProgressBooleanFromuser) {Test1.getbackground (). Setalpha (255-progress); Test2.invalidate (); }@Override Public void Onstarttrackingtouch(SeekBar SeekBar) { }@Override Public void Onstoptrackingtouch(SeekBar SeekBar) { }
?? Here, by dragging the seekbar and then changing the alpha value of the Button1, there is no alpha button2 set, just the Button2 refresh. You can look at the effect:
?? Obviously we didn't set Button2 's background, so why Button2 's background transparency also changed? In fact, as mentioned on the resource sharing, two button, we all use the same color, so the system is common to the same color resources, then when we change the color of button1, the Button2 alpha value will also change.
?? But there are times when we really need to change the state of a control instead of changing two, how do we deal with it? This is the mutations here to say, mutation meaning for change, the meaning of mutation, if you use mutation here, then will only change a color, then we modify the I code:
test1.getBackground().mutate().setAlpha(255-progress);
?? Modify the method that sets Alpha, use the mutate () method, and then run to see the effect:
?? For example, we have got good practice.
Android Development Learning Path--drawable mutations