Random number Java
Generate random number Android Java example source code.
Random rand = new Random();
int i = rand.nextInt() % 256; // range -255 +255
System.out.print(i + "
"); // -184
i = Math.abs(rand.nextInt() % 12); // range 0 +11
System.out.print(i); // 7
// Math.random() start with 0. e.g. 0.35981234
int nRan = (int) (Math.random()*10); // 0 - 10
// nextDouble(), nextFloat(), nextInt(), nextLong() returns 0 - 10
import java.util.Random;´
Random r = new Random();
int nRan = r.nextInt(); // 0 - 10
double dRan = r.nextDouble() * 10; // e.g. 7.496285271597397
nextDouble – return 0 - 1
nextFloat – same as double
nextInt – -2147483648 +2147483647
nextLong – -922337203685775808 +9223372036854775807
nextGaussian – 0.0 aberation 1.0.
396LW NO topic_id
AD
Další témata ....(Topics)
private int[] mData = new int[2]; // fill some values into array!!
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("myBoolean", true);
outState.putDouble("myDouble", 2.7);
outState.putInt("myInt", 5);
outState.putString("myString", "Heloo girls!");
int[] data = new int[mData.length];
for (int i = 0; i < data.length; i++) {
data[i] = mData[i];
}
outState.putIntArray("myArray", data);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
boolean myBoolean = savedInstanceState.getBoolean("myBoolean", false); // false basic value
double myDouble = savedInstanceState.getDouble("myDouble", 1.5); // 1.5 basic value
int myInt = savedInstanceState.getInt("myInt", 10);
String myString = savedInstanceState.getString("myString", "Hello boys!");
int[] data = savedInstanceState.getIntArray("myArray");
if (data != null && data.length == mData.length) {
for (int i = 0; i < data.length; i++) {
mData[i] = data[i];
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean myBoolean = savedInstanceState != null ? savedInstanceState.getBoolean("myBoolean", false) : true;
// etc. .......
}
Example source code for Android developers how create array of strings in *.xml file.
You can using array of strings in Spinner ( combobox - dropdown list ) or ListView etc.
You can using array of strings in Spinner ( combobox - dropdown list ) or ListView etc.
<resources>
<string-array name="colors">
<item>red</item>
<item>orange</item>
<item>yellow</item>
<item>green</item>
<item>blue</item>
</string-array>
</resources>
How set rounded corners and own styles ActivityMy.java
\res\drawable\back_button_answer.xml
Button btn.setBackgroundResource(R.drawable.back_button_answer);
\res\drawable\back_button_answer.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="//schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners android:radius="10dip" />
<!-- background -->
<gradient
android:startColor="#D6D7D6"
android:centerColor="#E2E2E2"
android:centerY="0.75"
android:endColor="#D6D7D6"
android:angle="270"
/>
<stroke android:width="2dip" android:color="#fff"/>
</shape>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawColor(Color.YELLOW);
Bitmap b = Bitmap.createBitmap(200, 200,
Bitmap.Config.ARGB_8888);
// you need to insert a image flower_blue into res/drawable folder
paint.setFilterBitmap(true);
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.flower_blue);
canvas.drawBitmap(bitmapOrg, 10, 10, paint);
int width, height;
Canvas c = new Canvas(b);
paint.setAlpha(255); //0x80
c.translate(0, 30);
c.drawBitmap(bitmapOrg, new Matrix(), paint);
paint.setColor(Color.BLUE);
Path mPath = new Path();
mPath.addCircle(50, 50, 50, Path.Direction.CCW);
//c.clipPath(mPath, Region.Op.UNION);
//c.clipPath(mPath, Region.Op.DIFFERENCE);
c.clipPath(mPath, Region.Op.INTERSECT);
//c.clipPath(mPath, Region.Op.REPLACE);
//c.clipPath(mPath, Region.Op.XOR);
paint.setColor(Color.GREEN);
paint.setAntiAlias(true);
c.drawCircle(30, 20, 30, paint);
int h = bitmapOrg.getHeight();
//canvas.drawBitmap(bitmapOrg, 10, 10, paint);
canvas.drawBitmap(b, 0, 10 + h + 10, paint);
}
}
}
Delete - unistal app from testing device - emulator and try again RUN - DEBUGG your app.
Editace: 2013-12-09 10:54:07
Počet článků v kategorii: 396
Url:random-number-java