Android uses Math. Random of java to obtain random color values. androidmath. Random

Source: Internet
Author: User

Android uses Math. Random of java to obtain random color values. androidmath. Random

There is a ColorPicker open source project on the Internet. Select the color value. Here, I want to dynamically modify the background color of a view.

Open a thread and modify the background color of the view once every 1 s.

We know that the background color of a view in android is

framelayout.setBackgroundColor(Color.parseColor("#"+arg0.obj));
The android Color class is used to parse a hexadecimal Color value. What we need to do now is to dynamically and randomly obtain the hexadecimal color value.

Java provides two methods to obtain random numbers:

I. Use the Random class

Generate a Random number through the Random class. This is a professional Random tool class with powerful functions.

There is a blog on the more detailed http://www.cnblogs.com/linjiqin/archive/2011/08/23/2150212.html

Iii. Random usage instructions
1. Differences between seed and Seed
The Random class uses instances with seeds or without seeds.
In general, the difference between the two is:
The results generated by each running with seeds are the same.
Without seeds, each running generates random and irregular data.
2. Create a Random object without seeds
Random random = new Random ();
3. Create a Random object without seeds
There are two methods:
1) Random random = new Random (555L );
2) Random random = new Random ();
Random. setSeed (555L );

In general, Random can generate a Random number within the specified range.

For example, to generate a random number between 1000 and 100, random. nextInt (1000) + 100. Note that 1000 is not included here,

I tested it. If the returned result of random. nextInt (1) is never 1, it is 0.

2. directly use the Math. random () method
Math. random () returns a double value between 0 and 1.

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


But how do we generate hexadecimal color values.

I use

Long I = Long. parseLong ("ff000000", 16 );

Long I = Long. parseLong ("ffffffff", 16); after testing, a Long data type is returned.

If the Long type is converted to the int type, int k = (int) I can be used directly. The resulting k is a negative number. This is related to the length of the int type.

Int type and long type conversion.

If all values are 0, the int type converted from the Long type is-16777216;

The int type converted from the Long type to the F type is-1;

Now that we know the range, we can use Random or Math to generate a Random number ranging from 16777216 to 1.

Then, the random number is taken as a negative number. Then

String hex = Integer.toHexString(-16777216);
This method can obtain a hexadecimal number, for example, FFFFFFFF;

The hexadecimal character is the Color value that needs to be parsed using Color. parse. Now we should know how to do it!

PS: if there are more convenient, more accurate, and more useful methods, I hope you will leave a message. Let's discuss it together!








In java, how does mathrandom () randomly generate 0 Numbers?

I guess you did this.
(Int) (Math. random (), so the occurrence is 0

The random () method returns a floating point number of [0, 1.
Generally, we use this method. For example, if I want to obtain ~ Random Number of 9
Int a = (int) (Math. random () * 10)
Note that it is multiplied by 10 and then transferred.
But not
Int a = (int) (Math. random () * 10

Because your = (int) (Math. random () value is always 0, 0 multiplied by any number will be 0

Java uses Mathrandom () to generate a random number of less than 100;

Int val = (int) (Math. random () * 100 + 1 );
System. out. println (val); // The first

Random r = new Random (); // type 2
Int;
A = 1 + r. nextInt (100 );
System. out. println ();
 

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.