I want to display several images with varying intervals, for the time, i can show the pictures but at equal intervals, as may make it show at varying intervals? for example, the first image displayed 5 seconds, the second image 7 seconds, the third image 4 seconds, etc... any ideas and/or examples?
this is my activity that I'm showing the pictures, but in equal intervals:
public class WCourse extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
//mp3 player
private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;
public static TextView TextViewTopTitle;
//Slider
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
//images ids
private int[] IMAGE_IDS = {
R.drawable.c5_d1, R.drawable.c5_d2, R.drawable.c5_d3, R.drawable.c5_d4, R.drawable.c5_d5,
R.drawable.c5_d6, R.drawable.c5_d7, R.drawable.c5_d8, R.drawable.c5_d9, R.drawable.c5_d10,
R.drawable.c5_d11, R.drawable.c5_d12, R.drawable.c5_d13, R.drawable.c5_d14, R.drawable.c5_d15,
R.drawable.c5_d16, R.drawable.c5_d17, R.drawable.c5_d18, R.drawable.c5_d19, R.drawable.c5_d20,
R.drawable.c5_d21, R.drawable.c5_d22, R.drawable.c5_d23, R.drawable.c5_d24, R.drawable.c5_d25,
R.drawable.c5_d26, R.drawable.c5_d27, R.drawable.c5_d28, R.drawable.c5_d29, R.drawable.c5_d30,
R.drawable.c5_d31, R.drawable.c5_d32, R.drawable.c5_d33, R.drawable.c5_d34, R.drawable.c5_d35,
R.drawable.c5_d36, R.drawable.c5_d37, R.drawable.c5_d38, R.drawable.c5_d39, R.drawable.c5_d40,
R.drawable.c5_d41, R.drawable.c5_d42, R.drawable.c5_d43, R.drawable.c5_d44, R.drawable.c5_d45,
R.drawable.c5_d46
};
//player
private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class
//slider handler
private final Handler handler = new Handler();
/** Called when the activity is first created.
* #param <MyCount>*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.watch_course);
//intent
TextViewTopTitle = (TextView) findViewById(R.id.TextViewTopTitle);
Bundle bundle = getIntent().getExtras();
TextViewTopTitle.setText(bundle.getString("RESULT2")+" ");
getText(bundle.getString("RESULT2"));
final Handler mHandler = new Handler();
initView();
final Runnable mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
final int delay = 5000; // delay for xx sec.
final long period = 11000;//repet every xx seconds
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);}
// Helper method to start the animation on the splash screen
private void AnimateandSlideShow() {
slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
slidingimage.getAnimation();}
really would appreciate your help.
public void run(){
try{
Thread.sleep(4000);
}
catch(Exception ex){
Log.e("Welcome Exception :",ex.toString());
}
mHandler.sendEmptyMessage(0);
}
why not use a random generated number instead of the currently specific values?
i mean :
final int delay = 5000; // delay for xx sec.
final long period = 11000;//repet every xx seconds
? seems like an oversite or maybe im reading this wrong
Related
I have created two TextViews and updating their contents through two different handlers, one for each TextView. I am incremented two counters in both handlers, one in each, and posting their values to respective TextView.
My first handler is ticking according to the value of long speed variable, and second handler is ticking 10 times later.
public class Main extends Activity{
private View root;
private final Context context = this;
private int count1 = 0, count2 = 0;
private TextView view1, view2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
setContentView(R.layout.s_main);
root = findViewById(R.id.root);
view1 = findViewById(R.id.txv1);
view2 = findViewById(R.id.txv2);
//
ViewTreeObserver vto = root.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
root.getViewTreeObserver().removeOnPreDrawListener(this);
startRuns();
return true;
}
});
}
private void startRuns() {
try {
final long sleep = 10;
final Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
#Override
public void run() {
view1.setText(String.valueOf(count1++));
handler1.postDelayed(this, sleep);
}
}, 1);
final Handler handler2 = new Handler();
handler1.postDelayed(new Runnable() {
#Override
public void run() {
view2.setText(String.valueOf(count2++));
handler2.postDelayed(this, sleep * 10);
}
}, 1);
Threads.run = true;
} catch (Exception ex) {
Alerts.alert(context, ex.getMessage());
}
}
}
This is good till the value of speed is 10 or above, but as soon i set the value of speed to 1, both handlers work with same duration or speed. First handler should work with speed of 1, and second handler should work with speed of 10, but both works with speed of 10 ms.
EDIT
In short, minimum delay on which postDelayed is working is 10 ms, not less than that, not 1, not 5, at least 10. I need to run a runnable with 5 ms delay in a project.
Can you please suggest where I am wrong?
The following program displays full screen images that animate automatically by using the loadAnimation() method. Here is is the code.
public class MainActivity extends ActionBarActivity {
/** Called when the activity is first created. */
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
private int[] IMAGE_IDS = {
R.drawable.picture1, R.drawable.picture2, R.drawable.picture3,
R.drawable.picture4
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
int delay = 1000; // delay for 1 sec.
int period = 3000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);
}
/**
* Helper method to start the animation on the splash screen
*/
private void AnimateandSlideShow() {
slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
Animation leftToRight = AnimationUtils.loadAnimation(this, R.anim.left_to_right);
slidingimage.startAnimation(leftToRight);
}
}
As you can see the full sized picture change every 4 seconds. Now what i want to do is to stop the animation after the appearance of the last image(picture 4 in this case). After fixing this,I will try fetch images from a server using JSON.
Thank you,
Theo.
1)You have to load animation until only you have image in IMAGE_IDS array
2)Then stop your timer also
Add following lines in AnimateandSlideShow() below currentimageindex++
if(currentimageindex <= IMAGE_IDS.length)
{
Animation leftToRight = AnimationUtils.loadAnimation(this, R.anim.letf_anim);
slidingimage.startAnimation(leftToRight);
}
else
{
timer.cancel();
}
Also replace following line
Timer timer = new Timer();
To
timer = new Timer();
I am making an application for android which should show pictures (i was already obtained), only now the app ah changed course and the images should be displayed at varying intervals, for example, the first image is displayed when starting the activity, the second after 4 seconds, the third after 15 seconds, etc ... this is my activity that shows the images within fixed intervals;
public class WCourse extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
//player
private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;
public static TextView TextViewTopTitle;
//Slider
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
//images ids
private int[] IMAGE_IDS = {
R.drawable.c5_d1, R.drawable.c5_d2, R.drawable.c5_d3, R.drawable.c5_d4, R.drawable.c5_d5,
R.drawable.c5_d6, R.drawable.c5_d7, R.drawable.c5_d8, R.drawable.c5_d9, R.drawable.c5_d10,
R.drawable.c5_d11, R.drawable.c5_d12, R.drawable.c5_d13, R.drawable.c5_d14, R.drawable.c5_d15,
R.drawable.c5_d16, R.drawable.c5_d17, R.drawable.c5_d18, R.drawable.c5_d19, R.drawable.c5_d20,
R.drawable.c5_d21, R.drawable.c5_d22, R.drawable.c5_d23, R.drawable.c5_d24, R.drawable.c5_d25,
R.drawable.c5_d26, R.drawable.c5_d27, R.drawable.c5_d28, R.drawable.c5_d29, R.drawable.c5_d30,
R.drawable.c5_d31, R.drawable.c5_d32, R.drawable.c5_d33, R.drawable.c5_d34, R.drawable.c5_d35,
R.drawable.c5_d36, R.drawable.c5_d37, R.drawable.c5_d38, R.drawable.c5_d39, R.drawable.c5_d40,
R.drawable.c5_d41, R.drawable.c5_d42, R.drawable.c5_d43, R.drawable.c5_d44, R.drawable.c5_d45,
R.drawable.c5_d46
};
//player
private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class
//slider handler
private final Handler handler = new Handler();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.watch_course);
TextViewTopTitle = (TextView) findViewById(R.id.TextViewTopTitle);
Bundle bundle = getIntent().getExtras();
TextViewTopTitle.setText(bundle.getString("RESULT2")+" ");
getText(bundle.getString("RESULT2"));
final Handler mHandler = new Handler();
initView();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
final int delay = 500; // delay for .5 sec.
final long period = 1000;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);}
private void AnimateandSlideShow() {
slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
slidingimage.getAnimation();}
Any suggestions or examples of how to display these images at varying intervals? really would appreciate your help.
Use below code hope it will help
MyCount count=new MyCount(IMAGE_IDS.length * 1000,1000);
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
start=0;
count.start();
}
#Override
public void onTick(long millisUntilFinished) {
if(start<=IMAGE_IDS.length)
{
img.setImageResource(IMAGE_IDS[start])
start+=1;
}
}
}
int start=0;
count.start();
I have the Uri to the selected folder in SDcard(code working perfectly fine). All I want to do is to make a slideshow of pictures present in that folder. Kindly tell me the steps I have to follow. Any pointers would be greatly helpful.
Right now I'm able to display only a single picture using
BitmapFactory.decodeFile
and then passing that to image.setImagebitmap
Try this code..it worked perfectly.
/** For Images Slidshow. */
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
int[] IMAGE_IDS = {R.drawable.mic, R.drawable.mic5, R.drawable.mic6,
R.drawable.mice7};
//code of slideshow start from here
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
int delay = 1000; // delay for 1 sec.
int period = 8000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);
}
private void AnimateandSlideShow() {
slidingimage = (ImageView)findViewById(R.id.ImageView_id);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
}
//code of slideshow ends here
I would like to change the background image of a frame layout per second. For this task I use timer and timertask classes but it does not seem to work as the initial background never changes and the pyhsical device that I test the following code terminates abnormally.
FrameLayout fl;
List<Integer> myList;
int i = 0;
TimerTask myTimerTask = new TimerTask()
{
public void run()
{
fl.setBackgroundResource(myList.get(i));
i++;
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = new ArrayList<Integer>();
myList.add(R.drawable.square1);
myList.add(R.drawable.square2);
myList.add(R.drawable.square3);
myList.add(R.drawable.square4);
myList.add(R.drawable.square5);
myList.add(R.drawable.square6);
myList.add(R.drawable.square7);
myList.add(R.drawable.square8);
myList.add(R.drawable.square9);
myList.add(R.drawable.square10);
myList.add(R.drawable.square11);
myList.add(R.drawable.square12);
fl = (FrameLayout)findViewById(R.id.frameLayout1);
long delay = 1000;
long period = 1000;
Timer t = new Timer();
t.schedule(myTimerTask,delay,period);
}
Where do I fail ? ^^
Thanks in advance for your time.
You should call invalidate() after setting the new background resource.
You can't access a view from a non UI thread like a timer. You need to have a handler to update the view and get the timer to send messages to it. And you need to stop i from going out of bounds, like:
TimerTask myTimerTask = new TimerTask() {
public void run() {
Message m = Message.obtain();
m.what = i;
myUpdateHandler.sendMessage(m);
i++;
if (i >= myList.size())
i = 0;
}
};
.
Handler myUpdateHandler = new Handler() {
/** Gets called on every message that is received */
#Override
public void handleMessage(Message msg) {
fl.setBackgroundResource(myList.get(msg.what));
}
};
.
Well, you got several problems on your code.
From your code it doesn't clear where the "fi" is initialized, is it before the timer callback is called? after?
What is the purpose of the int "i"? Shouldn't it be a class member?
You must stop the timer on the onDestroy of the Activity, otherwise you might get some undesired behavior when accessing the frame layout.
Anyway, try running the the following from the onCreate:
final FrameLayout fl = (FrameLayout)findViewById(R.id.frameLayout1); // You must have final here
final List<Integer> myList = <get it from where you need to>
int i = 0; // What is the purpose of this int? it passed by value to the callback - are you sure it is needed?
TimerTask myTimerTask = new TimerTask()
{
public void run()
{
fl.setBackgroundResource(myList.get(i));
i++; // Shouldn't it be a class member?
}
};