Android Gets the color of a point in a linear gradient

Source: Internet
Author: User

Android does provide a lot of very powerful tools for us, for example, we've often added gradient (gradients) to shape recently, like a linear gradient in my project,

[HTML]View PlainCopy 
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <corners android:radius="5dip" />
  4. <gradient android:startcolor= "#262626" android:endcolor= "#ffbc1c" android: angle="0" />
  5. </shape>

And this image as the background of the Seekbar, used to choose the color, slide to choose where the color,:

So I thought of two possible implementations:
1, get the seekbar background picture of the bitmap, get the corresponding point to get the color
2, directly through the linear gradient algorithm to get the color of a position
Method 1 is easy and can be used for other controls, but consumes more memory
Method 2 Memory consumption is very small, but need to understand the linear gradient of the algorithm, and only for the linear gradient of the picture is valid, the other gradients need to be re-written, but the linear gradient is often used more, so it is intended to use the second method.

Linear gradient is the simplest gradient, the idea is that the R G B of the corresponding color A is moving toward the R G B of color B, and realizes that: in Java, (many other languages also) use an int to hold the RGB value of the color, but this is only the way of storage, in fact, each RGB is unrelated, The gradient needs to be removed separately and changed separately. Write a simple two color gradient color selector, the code is as follows:

[Java]View PlainCopy  
  1. /**
  2. * Created by Chenxiaoxuan1 on 16/3/25.
  3. */
  4. Public class Lineargradientutil {
  5. private int mstartcolor;
  6. private int mendcolor;
  7. Public lineargradientutil (int startcolor, int endcolor) {
  8. this.mstartcolor = StartColor;
  9. this.mendcolor = EndColor;
  10. }
  11. public void Setstartcolor (int startcolor) {
  12. this.mstartcolor = StartColor;
  13. }
  14. public void Setendcolor (int endcolor) {
  15. this.mendcolor = EndColor;
  16. }
  17. public int GetColor (float radio) {
  18. int redstart = color.red (Mstartcolor);
  19. int bluestart = Color.Blue (Mstartcolor);
  20. int greenstart = Color.green (Mstartcolor);
  21. int redend = color.red (Mendcolor);
  22. int blueend = Color.Blue (Mendcolor);
  23. int greenend = Color.green (Mendcolor);
  24. int red = (int) (Redstart + ((redend-redstart) * Radio + 0.5));
  25. int greed = (int) (Greenstart + ((greenend-greenstart) * Radio + 0.5));
  26. int blue = (int) (Bluestart + ((blueend-bluestart) * Radio + 0.5));
  27. return Color.argb (255,red, greed, blue);
  28. }
  29. }

After the implementation and test with the unit test, and then formally used, the results are very satisfied with the ~

Later, there is a need to do a simple bar color selector, is also implemented with Seekbar, but also linear gradient, just a little more color. :

The principle is the same, after a short time to fill the code later.

Android Gets the color of a point in a linear gradient

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.