I want to change the image of an Image view using a for loop, a String array and the png images in drawable folder.One Image should rest for 5 seconds then the next should appear.Here is the code I used.And it's not working properly.The images are not changing.It waits 5 seconds per each loop but the images are not changing.At last the last image is loaded.Please someone help me...
private String [] array1 = {"card2c","card2d","card2h","card2s",
"card3c","card3d","card3h","card3s",
"card4c","card4d","card4h","card4s",
"card5c","card5d","card5h","card5s",
"card6c","card6d","card6h","card6s",
"card7c","card7d","card7h","card7s",
"card8c","card8d","card8h","card8s",
"card9c","card9d","card9h","card9s",
"card10c","card10d","card10h","card10s",
"cardjc","cardjd","cardjh","cardjs",
"cardqc","cardqd","cardqh","cardqs",
"cardkc","cardkd","cardkh","cardks",
"cardac","cardad","cardah","cardas",};
for(int j=0;j<52;j++)
{
int resID_temp1 = getResources().getIdentifier(array1[j] , "drawable", getPackageName());
Drawable image_temp1 = getResources().getDrawable(resID_temp1);
Player1.setImageDrawable(image_temp1); // Player1 is the ImageView
try {
Thread.sleep(5000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
Tried the Timer
(for(int j=0;j<52;j++)
{
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
//switch image here
int resID_temp1 = getResources().getIdentifier(array1[j] , "drawable", getPackageName());
Drawable image_temp1 = getResources().getDrawable(resID_temp1);
Player1.setImageDrawable(image_temp1);
}
}, 0, 5000);
Based on your code example, you're changing the image resource, but then immediately sleeping the same thread that changed the image resource which will block it and prevent the image from visibly changing (particularly bad if this is the UI thread as this will lock up your app).
You should do as JohnyTex suggests and use a TimerTask to change the image every 5 seconds which does not require sleeping and so will not block the current thread.
Try using timers instead:
//Declare final variable here
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//Switch (declared final) image variable here
}
});
}
}, 0, 5000);
Something like that... I am not by my computer though.
Concepts: runOnUithread and final variables might also be of interest to read about. final is because otherwise the variable cant be manipulated on another thread (actually Its only another scope really here). RunOnUiThread is because graphical components must only be manipulated on the main UI thread, although the TimerTask is run on another separate thread.
I am making an Android game modeled after the old Simon game. It is a little different in the layout as it is using a 3x3 layout of buttons. I am trying to get the buttons to light up one at a time inside the loop that randomly selects a button. The trouble I have is that all of the buttons light up at once and only the last (or first, not sure) changes back to the original color. I have tried very thoroughly to find an appropriate answer to my situation but have had no luck here or elsewhere. The button id(s) are in the butts[]. butts[0] is button 1, butts[2] ... Below is my attempt.
public void play()
{
for(int x = 0; x <= numButtons; ++x)
{
spot = randomGenerator.nextInt(9);
playMe[x] = spot;
//butts[spot].setBackgroundColor(Color.parseColor("#540115"));
handler.postDelayed(new Runna(spot), (x+1)*1000);
}
}
class Runna implements Runnable
{
public Runna(int j2)
{
butts[j2].setBackgroundColor(Color.parseColor("#540115"));
}
public void run()
{
butts[spot].setBackgroundColor(Color.LTGRAY);
}
}
I think it has to do with the value of spot. It is global to the functions and you change it every time. It runs, but at the end there is still only one spot and EVERY runnable is trying to change that same spot.
Perhaps save spot in your runnable?
class Runna implements Runnable
{
int s;
public Runna(int j2)
{
s = j2;
butts[s].setBackgroundColor(Color.parseColor("#540115"));
}
public void run()
{
butts[s].setBackgroundColor(Color.LTGRAY);
}
}
Have you tried to invalidate the button each time?
public Runna(int j2)
{
butts[j2].setBackgroundColor(Color.parseColor("#540115"));
butts[j2].invalidate();
}
I need an advice how to create some animations I want to add in my buttons. Actually I have the animation code, the thing which I need is how to set properly the timing of each one. Here is what I tried already :
fest.setVisibility(View.INVISIBLE);
handler.postDelayed(new Runnable() {
#Override
public void run() {
fest.setVisibility(View.VISIBLE);
fest.startAnimation(anim);
handler.removeCallbacks(this);
}
}, 500);
This is the things which I did for 7 buttons. First I set the visibility to invisible because I want to achieve the effect that they are appearing after 5 miliseconds after onCreate and for every next button I am increasing the delay time with 5 miliseconds so every of them to appear after the previos one. But the problem in this code is that when the next handler starts for the second button for example, the previos button is getting invisible for a part of the seconds and shows again(I hope someone understand what I mean).
Any suggestions for a bette implementation of something like that?
Thanks in advance!
So here is the thing which fixed that problem. I used this for every button and it's working as I want :
final Handler festHandler = new Handler();
festHandler.postDelayed(new Runnable() {
#Override
public void run() {
Animation anim = AnimationUtils.loadAnimation(Menu.this, R.anim.fadein);
fest.setVisibility(View.VISIBLE);
fest.startAnimation(anim);
festHandler.removeCallbacks(this);
}
}, 400);
I'm a newbie in android so please don't judge me hard.
I'm trying to create a screen with two areas, an image area and a text area.
In the image area several images should be changed within a periodical time, 3-4 seconds. (Please see the image: )
Can you please give me an example how can I accomplish this?
Thank you.
You can use CountDownTimer class for this.
Or you can use a Gallery with Thread and Handler to achieve this.
Here is a link to the project called AutoSlideGallery,
https://github.com/nixit28/AutoSlideGallery
The core logic is here,
(new Thread() {
public void run() {
myslideshow();
handler.postDelayed(this, 2000); // execute every two second.
}
}
).start();
And the method which performs the action,
private void myslideshow() {
PicPosition = gallery.getSelectedItemPosition() + 1;
if (PicPosition >= pics.length)
gallery.setSelection(0); // stop
else
gallery.setSelection(PicPosition);// move to the next gallery
// element.
}
So im making a slideshow in android that i want to move very fast to look almost as though an animation. Bellow is the code that i am using but i want it go move faster(smaller delay) but i cant make the postDelay any smaller. How would i do this? If its not possible what would a better way to do this be?
private Runnable runnable = new Runnable() {
public void run() {
myslideshow();
handler.postDelayed(this, 1);
}
};
private void myslideshow()
{
if (position < imageIDs.length){
iv.setImageResource(imageIDs[position]);
position++;
}
else{
iv.setImageResource(R.drawable.logo);
}
}
Could the Android Animation API help instead?
You can use a gallery. On click of some button or anything you can run a for loop to
display next element in the gallery till the last element (image in your case) is displayed