Android uses Java math.random to get random color values

Source: Internet
Author: User

There's an online ColorPicker Open Source project, select a color value. And what I want to achieve here is to dynamically modify the background color of a view.

Open a thread that modifies the view's background color every 1s.

We know that setting a view background color on Android has

Framelayout.setbackgroundcolor (Color.parsecolor ("#" +arg0.obj));
is to use the Android color class to parse a hex color value with a # sign. Now it's time to get this 16 binary color dynamically and randomly.

Java provides us with two ways to get random numbers:

I. Use of the Random class

A random number is generated by the random class, which is a professional random tool class with powerful functions.

There's a blog post that says more detailed http://www.cnblogs.com/linjiqin/archive/2011/08/23/2150212.html

iii. Description of use of the random class
  1, with the difference between seed and without seed
the random class is used at the root of the policy-striped and random instances without seeds.
in layman's words, the difference between the two is:
with a seed, the results are the same for each run.
Without seeds, each run is randomly generated, with no regularity.
  2. Create random objects without seeds
random random = new random ();
  3. Create random objects without seeds
  There are two ways of doing this:
1) Random random = new Random (555L);
2) Random random = new random ();
random.setseed (555L);

In general, random can generate a number within a specified range.

For example: to generate a random number from 1000 to 100, Random.nextint (1000) +100; Note that this is not 1000,

I have tested, if let Random.nextint (1) return will never be 1, are 0

Ii. Direct use of the Math.random () method
A double value between 0 and 1 is returned by Math.random ().

If you want to generate a random number between 1000 and 100, then Math.random () * (1000-100) +100;


But how is the color value of the 16 binary generated?

I use

long i = Long.parselong ("ff000000", 16);

long i = Long.parselong ("FFFFFFFF", 16), tested, returns a long type of data.

A long type can be converted to an int type directly int k = (int) I; the resulting k is a negative number. This is related to the length of the int type, you can go to the bottom

type int and long conversions.

All 0, the size of int that is converted by a long type is-16777216;

All F, the size of the int that is converted by a long type is-1;

Now that you know the scope, you can generate a random number from 16777216 to 1 with random or math.

This random number is then taken as negative. And then through

String hex = integer.tohexstring (-16777216);
This method can get a hexadecimal number, such as: FFFFFFFF;

This hexadecimal character is the color value we need to parse with Color.parse. The following people should know how to do it!

PS: If there is more convenient, more accurate, better use of the method to achieve the hope that you leave a message oh, we discuss together Ah!







Android uses Java math.random to get random color values

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.