Screen Flash with colored strobe - android

I want to create a colored strobe effect on the screen using the below code but it didn't work. It's changed the screen color only one time.
int[] colorArray = new int[]{R.color.blue, R.color.purple, R.color.brown, R.color.red, R.color.green, R.color.valvet
, R.color.darkBlue, R.color.yellow, R.color.litegreen, R.color.orange, R.color.pink, R.color.pgreen,
R.color.liteBlue, R.color.divider, R.color.icons, R.color.colorAccent, R.color.colorPrimaryDark};
final Handler handler = new Handler() {
final Runnable runnable = new Runnable() {
int i=0;
public void run() {
multi_disco.setBackgroundColor(colorArray[i]);
i++;
if(i>colorArray.length-1)
{
i=0;
}
handler.postDelayed(this, 50);
}
};
handler.postDelayed(this, 2000)};

The flashlight from smartphones can't be colored just because the flash is created for the camera, and in 99,99% of the smartphones is just white.
So, what you are asking, cannot be done.

If you are talking about changing the main view of the app color try to create a loop that will
//generate random color
String letters = "0123456789ABCDEF";
String color = "#";
for (int i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
//now set the color of the view
v.setBackgroundColor(color);
Do this in a loop...

Related

How to dynamically get R value [duplicate]

This question already has answers here:
how to access the drawable resources by name in android
(7 answers)
Closed 6 years ago.
I have to dynamically get some resources from the R file.
For example, let's say I have to dynamically generate ImageView and dynamically get a drawable to put inside it.
I don't know how many ImageView I have to generate; there may be 10, 50 or 100 so I have to do everything dynamically.
My main problem is to get dynamically the drawable from the R file.
Lets say I have this drawable:
R.drawable.img1
R.drawable.img2
R.drawable.img3
R.drawable.img4
I should do something like this:
for(int i = 0; i < 10; i++){
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.img + i);
}
How can I "build" this line of code: R.drawable.img + i
How can I reach my goal?
At first make sure your image is jpg or png
You can try with this
for(int i = 0; i < 10; i++)
{
ImageView iv = new ImageView(this);
int imageResource = context.getResources().getIdentifier("#drawable/img "+i.replace(".jpg", ""), null,context.getPackageName());
iv.setImageResource(imageResource);
}
final int []imageArray=
{R.drawable.img1,R.drawable.img2,R.drawable.img3};
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i=0;
public void run() {
//randomimg.setImageResource(imageArray[i]);
//hEAR CREATE dynamic image view and set image resource
i++;
if(i>imageArray.length-1)
{
i=0;
}
handler.postDelayed(this, 5000); //for interval...
}
};
handler.postDelayed(runnable, 5000);
put above code in your activity. and set image resource it work for me.

Changing the color of a ShapeDrawable ImageView in a GridLayout

I have a GridLayout with 16 dots, which are ImageViews created from ShapeDrawables. How can I set the color of these without creating a new ShapeDrawable?
Here's most of my code:
for (int i = 0; i < dotNumWidth * dotNumWidth; i++) {
ShapeDrawable redShape = new ShapeDrawable(new OvalShape());
redShape.getPaint().setColor(Color.RED);
redShape.setIntrinsicHeight(width / dotNumWidth - dotNumWidth * padding);
redShape.setIntrinsicWidth(width / dotNumWidth - dotNumWidth * padding);
// Put Cyan Shape into an ImageView
ImageView redView = new ImageView(getApplicationContext());
redView.setImageDrawable(redShape);
redView.setPadding(padding, padding, padding, padding);
grid.addView(redView);
}
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
grid.getChildAt(child).setAlpha(100); // I want to change this line
child = (new Random()).nextInt(dotNumWidth * dotNumWidth);
grid.getChildAt(child).setAlpha(0); // and this one
handler.postDelayed(this, 1000);
}
}, 1000);
This is run in the onCreate(), and I wish to change the lines marked above to change the color of the views instead of the alpha. I know this is possible if I replace the view with a new view, but is there a better way to do this?
This is a picture of the dot grid:

Changing background color dynamically (Android)

I have the following code:
public class MainActivity extends Activity {
private BufferedReader br;
private Socket s;
private View v,v1;
private RelativeLayout rl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
s = new Socket("192.168.1.36",50000);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
}catch(Exception e){e.printStackTrace();}
color();
}
private void color(){
rl = (RelativeLayout) this.findViewById(R.id.rellay);
while(true){
try{
String received = br.readLine();
if(received != null){
// System.out.println(received);
String[] color = received.split(",");
setColor(color);
}
}catch(Exception e){e.printStackTrace();}
}
}
private void setColor(String[] color){
rl = (RelativeLayout) this.findViewById(R.id.rellay);
int red = Integer.parseInt(color[0]);
int green = Integer.parseInt(color[1]);
int blue = Integer.parseInt(color[2]);
int a = Integer.parseInt(color[3]);
rl.setBackgroundColor(Color.argb(a, red, green, blue));
What I want to do is receive 4 values separated by commas (this works), and I want the values in the range 0-255 to be the RGB color.
I want to change the background of the android activity. I can change the color once, from the onCreate method, but when I try to change it further times I get the default white screen. The values never exceed 255.
How can this be done? Thanks!!
Make sure the alpha value != 0.
The alpha value represent the transparency of the color so 0 is fully transparent.
try to start with fixed values, like:
rl.setBackgroundColor(Color.argb(255, 0, 0, 0));
and make sure it works.
after that, pull your values from the server and parse them into a color.
BTW, Listen to Nick Cardoso comments, It's a bad user experience to feel the network latency.

Programmatically create EditTexts based on Sudoku grid

I have a Sudoku grid image loaded into my app, but I'm having a lot of trouble getting EditTexts to appear in each square. When I run the app, the numbers in the EditTexts are hardly visible. I've tried changing the background color and text color and nothing has changed.
/* PROGRAMATICALLY ADD EDITVIEWS via waiting for draw */
layout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener()
{
public void onGlobalLayout()
{
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
/* not used yet
int height = grid.getMeasuredHeight();
int width = grid.getMeasuredWidth();
int top = grid.getTop();
//int bottom = grid.getBottom();
int left = grid.getLeft();
int square_height = height/9;
int square_width = width/9;*/
for ( int r = 0; r < 9; r++ )
{
for ( int c = 0; c < 9; c++ )
{
text_boxes[r][c] = new EditText(getApplicationContext());
text_boxes[r][c].setId(r+c);
text_boxes[r][c].setText("1");
text_boxes[r][c].setInputType(InputType.TYPE_CLASS_NUMBER);
text_boxes[r][c].setFilters(new InputFilter[] {new InputFilter.LengthFilter(1)});
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(c*10, r*50, 0, 0);
layout.addView(text_boxes[r][c], params);
text_boxes[r][c].setBackgroundColor(0x0FF00);
}
}
}
});
you didn't remove the listener when you are finished using it, so it can keep being called.
also, about the color of the EditText, you can change it to whatever you wish , by just changing the background of the EditTexts, or use a global style that sets a default background for EditTexts. you can even use this tool

Place the image at random x,y of the screen and make the image invisible after certain peroid in android

I need to create an image at random places. After few seconds it has to be disappeared. After the first image get disappeared the second image has to come. I used the following code to place the image at random coordinates and use a handler to make it invisible after few seconds. I used for loop to create some number of images. All the images comes and goes at the same time. I think the for loop is the problem. I couldn't find a solution for that. Any help is appreciated.
ImageView iv=null;
RelativeLayout rl=null;
Random rand= new Random();
int min=10, max=100;
int randomNum;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
for(int i=0;i<100;i++){
randomNum = rand.nextInt(max - min + 1) + min;
Log.d("RandomNum",Integer.toString(randomNum));
Log.d("i value",Integer.toString(i));
iv = new ImageView(this);
iv.setBackgroundColor(Color.YELLOW);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50+randomNum;
params.topMargin = 60+randomNum;
rl.addView(iv, params);
timerDelayRemoveView(500, iv);
}
}
public void timerDelayRemoveView(long time, final ImageView iv){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
iv.setVisibility(View.GONE);
}
}, time);
}
You can try to implement a timerDelayAddView (like you delayed timerDelayRemoveView) where you specify an image-dependent delay (e.g. 2000 * i) before you call addView.

Categories

Resources