I want to generate/randomize a color and then I want second one that is close to the generated one.
This is how I generate the colors ftm:
Paint colors = new Paint();
int red = ran.nextInt(256-100)+100;
int green = ran.nextInt(256-100)+100;
int blue = ran.nextInt(256-100)+100;
colors.setARGB(255, red, green, blue);
and later the 2nd color I generate like this:
switch (ran.nextInt(3)) {
case 0:
red = red - (40 - level);
break;
case 1:
green = green - (40 - level);
break;
default:
blue = blue - (40-level);
break;
}
The problem is that it works in some cases and sometimes it can give me a 2nd color that is off by miles.
Is there another, better and easier way to generate these colors?
br
You need to create a real random number between 0 and 3:
Random ran = new Random();
int max = 3;
int min = 0;
int randomNum = ran.nextInt((max - min) + 1) + min;
switch (randomNum ) {
case 0:
red = red - (40 - level);
break;
case 1:
green = green - (40 - level);
break;
default:
blue = blue - (40-level);
break;
}
You could use java.awt.Color.brighter() and Color.darker().
Related
Hello friends I have 2 situation but I want to do it randomly. When user click button then there is 2 situation case0 or case1 randomly changes. So text's are randomly changes too...
*lastImageName and lastImageName2 are variable strings...
Random rand = new Random();
int newrand = rand.nextInt(1) + 1;
switch(newrand) {
case 0:
text1.setText(lastImageName);
text2.setText(lastImageName2);
break;
case 1:
text1.setText(lastImageName2);
text2.setText(lastImageName);
break;
}
But that's not working sometimes.. What's the rand. problem?
Random rand = new Random();
int a = 0;
int b = 1;
int c = random.nextBoolean() ? a : b;
Remove the +1. Just do that: int newrand = rand.nextInt(2);
Random#nextInt(int) generates a random number between 0 inclusive and the upper bound exclusive. I.e., nextInt(1) will always return 0. Instead, you should just use:
int newrand = rand.nextInt(2);
If you only have two cases then use
boolean state = rand.nextBoolean();
text1.setText(state?lastImageName:lastImageName2);
text2.setText(state?lastImageName2:lastImageName);
For multiple you can also use
Random rand = new Random();
int newrand = rand.nextInt() % count;//count = 2 for your case
switch(newrand) {
case 0:
text1.setText(lastImageName);
text2.setText(lastImageName2);
break;
case 1:
text1.setText(lastImageName2);
text2.setText(lastImageName);
break;
}
I have this code from RatingBar android - custom draw runtime
It worked before which I used to change the color of a graphic on my rating control:
vthf.rating.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//--
float touchPositionX = event.getX();
float width = vthf.rating.getWidth();
float starsf = (touchPositionX / width);
starsf = starsf * param_final__data.score_max;
int starsint = (int) Math.round(starsf);
byte starsbyte = (byte) starsint;
param_final__data.score_cur = starsbyte;
starsf = starsint;
vthf.rating.setRating(starsf);
//--
int color = Color.BLACK;
switch (starsbyte % 4) {
case 0: color = Color.BLUE; break; // 4 stars
case 3: color = Color.GREEN; break;
case 2: color = Color.YELLOW; break;
case 1: color = Color.RED; break;
}
final LayerDrawable layerDrawable = (LayerDrawable) vthf.rating.getProgressDrawable();
Drawable myWrap = layerDrawable.getDrawable(2);
//--
//
//--
myWrap = DrawableCompat.wrap(myWrap);
//myWrap = myWrap.mutate();
//--
// myWrapMutate.setColorFilter(color, PorterDuff.Mode.SRC_IN);
// DrawableCompat.setTintList(myWrap, ColorStateList.valueOf(color));
//--
DrawableCompat.setTint(myWrap, color);
//--
vthf.rating.invalidate();
}
else
if (event.getAction() == MotionEvent.ACTION_DOWN) {
param_final__view.setPressed(true);
}
else
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
param_final__view.setPressed(false);
}
return true;
}
});
I believe it stopped working after I upgraded from Android Studio 1.5.1 and all support libraries (not sure what version) - I am not 100% sure though since I am no sure I dsicovered the problem immediate or if it had been here longer.
I am tesing on Huawei P6 Android 4.4.2
My gradle is looking like his:
compileSdkVersion 23
buildToolsVersion '23'
dependencies {
compile 'com.android.support:support-v4:+'
compile 'com.google.android.gms:play-services:+'
compile 'com.android.support:appcompat-v7:+'
}
My manifest has this
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17"
/>
This is my best lead on why the code is no longer working - but I am open to all kinds of suggestions if I am missing something obvious that could cause this unrelated to API versions.
---Note---
This code appears to tint the graphic slightly darker:
final LayerDrawable layerDrawable = (LayerDrawable) vthf.rating.getProgressDrawable();
Drawable myWrap = layerDrawable.getDrawable(2);
myWrap = DrawableCompat.wrap(myWrap);
DrawableCompat.setTint(myWrap, color);
This code has no effect:
final LayerDrawable layerDrawable = (LayerDrawable) vthf.rating.getProgressDrawable();
Drawable myWrap = layerDrawable.getDrawable(2);
DrawableCompat.setTint(myWrap, color);
Not sure what to make of the above. Maybe the problem is caused by something else that I do not understand. If the graphic is darkened, one would think it is kinda working... But it is the exact same darkening no matter he color - a least as far as my eye can tell.
I use something like this to set the Color. Might work here as well, all objects that inherrit from View should be able to do this. This case uses ContextCompat to get to the color, but there are more ways than this to get the color you want.
View view = (findViewById(R.id.text_view_location));
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.wantedColor));
Something in AppCompat 23.3 breaks my previous solution. Anyway, you can get your rating bar back to work just by avoiding DrawableCompat#Wrap:
ratingBarB.setOnTouchListener(new View.OnTouchListener() {
private int lastColoredProgress = 0;
#Override
public boolean onTouch(View v, MotionEvent event) {
int progress = ratingBarB.getProgress();
if (progress != lastColoredProgress) {
lastColoredProgress = progress;
int color;
switch (lastColoredProgress) {
case 0:
case 1:
color = Color.RED;
break;
case 2:
case 3:
color = Color.YELLOW;
break;
case 4:
default:
color = Color.GREEN;
break;
}
final Drawable drawable = ratingBarB.getProgressDrawable();
if (drawable instanceof LayerDrawable) { // Lollipop and newer
final LayerDrawable layerDrawable = (LayerDrawable) drawable;
DrawableCompat.setTint(layerDrawable.getDrawable(2), color);
} else {
DrawableCompat.setTint(drawable, color);
}
}
return false;
}
});
When you wrap your Drawable with DrawableCompat, a new DrawableWrapper is created. You need to set the new drawable to your component.
In your code, you need to add somethink like this (just after setTint) :
layerDrawable.setCompoundDrawablesWithIntrinsicBounds(myWrap, null, null, null);
I am attempting to make a slider bar that randomly changes the color of several parts of the users interface when the user slides the bar from one side to the other. I have a piece of code that already works but I know it's not as efficient as it could be. I have 3 of the same layout modifiers as below:
private void getLayout2(){
int color = 0;
final Random randColor2 = new Random();
int control = randColor2.nextInt(4);
switch(control){
case 0: color = getResources().getColor(R.color.White);
Log.i(TAG, "color is white");
break;
case 1: color = getResources().getColor(R.color.Red);
Log.i(TAG, "color is red");
break;
case 2: color = getResources().getColor(R.color.Yellow);
Log.i(TAG, "color is yellow");
break;
case 3: color = getResources().getColor(R.color.Blue);
Log.i(TAG, "color is blue");
break;
}
Layout2.setBackgroundColor(color);
return;
}
What I would like to do is change all of the layout backgrounds in the same piece of code but I am not sure how to do that as there is only one color variable and I don't want all the colors to be the same at any given time. Currently I am just calling multiple versions of this method for each layout in the "onProgressChanged" method of the slider listener. Can this be done in a single method with this scheme?
There are a number of options. You can have your function return a the color and set it your main routine, or you can pass the layout as a parameter to your function. An example of the latter follows. Since this is a function, the color variable is recreated every time it is called, so no need to worry that it will be the same when you call the function a different time. Try it and see.
private void setRandomBackgroundColor(View layout){
int color = 0;
final Random randColor = new Random();
int control = randColor.nextInt(4);
switch(control) {
case 0:
color = getResources().getColor(R.color.White);
Log.i(TAG, "color is white");
break;
case 1:
color = getResources().getColor(R.color.Red);
Log.i(TAG, "color is red");
break;
case 2:
color = getResources().getColor(R.color.Yellow);
Log.i(TAG, "color is yellow");
break;
case 3:
color = getResources().getColor(R.color.Blue);
Log.i(TAG, "color is blue");
break;
}
layout.setBackgroundColor(color);
return;
}
i am new developer on andengine. i am making a game of bike racer, where i want to move my background instead of my bike. so how can i use two 3 background if 1 background is complete then start second one and when it complete then start third? How should i do for it please tell me to solve this problem.
Are you using tiles in your background?
I got this snippet of code from : This very useful tutorial
switch(lDirection) {
case RIGHT:
int RightAnimTiles[] = {WorldActivity.RIGHT_CENTER_TILE - 1,
WorldActivity.RIGHT_CENTER_TILE,
WorldActivity.RIGHT_CENTER_TILE + 1,
WorldActivity.RIGHT_CENTER_TILE};
animate(WorldActivity.WALK_ANIMATE_DURATION, RightAnimTiles, -1);
break;
case DOWN:
int DownAnimTiles[] = {WorldActivity.DOWN_CENTER_TILE - 1,
WorldActivity.DOWN_CENTER_TILE,
WorldActivity.DOWN_CENTER_TILE + 1,
WorldActivity.DOWN_CENTER_TILE};
animate(WorldActivity.WALK_ANIMATE_DURATION, DownAnimTiles, -1);
break;
case LEFT:
int LeftAnimTiles[] = {WorldActivity.LEFT_CENTER_TILE - 1,
WorldActivity.LEFT_CENTER_TILE,
WorldActivity.LEFT_CENTER_TILE + 1,
WorldActivity.LEFT_CENTER_TILE};
animate(WorldActivity.WALK_ANIMATE_DURATION, LeftAnimTiles, -1);
break;
case UP:
int UpAnimTiles[] = {WorldActivity.UP_CENTER_TILE - 1,
WorldActivity.UP_CENTER_TILE,
WorldActivity.UP_CENTER_TILE + 1,
WorldActivity.UP_CENTER_TILE};
animate(WorldActivity.WALK_ANIMATE_DURATION, UpAnimTiles, -1);
break;
default:
break;
}
I am trying to make a very simple tile engine. However, using the Bitmap.getpixel(x,y) is not always matching the color correctly. It seems to be doing fine with 0xFFFFFFFF, 0xFF000000, 0xFF189600, and 0xFF18FF00, but has a problem with 0xFF186600. I tried changing it to multiple different similar colors, but it still doesn't seem to be reading it correctly. I am comparing with a simple switch statement. Here is the code for my method
public void LoadLevel(Canvas canvas, int levelName)
{
Bitmap level = BitmapFactory.decodeResource(getResources(), levelName);
Bitmap startTile = BitmapFactory.decodeResource(getResources(), R.drawable.starttile);
canvas.drawColor(Color.WHITE);
int drawX = 0;
int drawY = 0;
for(int y = 0; y < level.getHeight(); y++)
{
for(int x = 0; x < level.getWidth(); x++)
{
switch(level.getPixel(x, y))
{
case 0xFF000000: break;
case 0xFFFFFFFF:
canvas.drawBitmap(startTile, drawX, drawY, null);
break;
case 0xFF189600:
canvas.drawBitmap(startTile, drawX, drawY, null);
break;
case 0xFF18FF00:
canvas.drawBitmap(startTile, drawX, drawY, null);
break;
case 0xFF186600:
canvas.drawBitmap(startTile, drawX, drawY, null);
break;
}
Log.d("Color", Integer.toString(level.getPixel(x, y)));
drawX += 128;
}
drawX = 0;
drawY += 128;
}
}
According to the log, the color is "-15177472". I am not sure what color that actually is though... So I am not sure if -15177472 == 0xFF186600
What am I doing incorrectly to not get the pixel? Is android changing the image? Are there safe colors I am suppose to use?
-15177472 is 0xFF186900. Note the 9 digit instead of the 6 you were looking for.
Where are you getting these expected values from, and how are you loading the bitmaps? If you are creating the bitmaps in a drawing program, and then loading them in your Android app, you may find that they are being decompressed as 16bpp, in which case there will be some loss of accuracy when converting pixel values back to 32bpp for getPixel().