I want slider in my imageview but it gives me only one image at a time if any one of you have a solution please tell me.
here in my activity class.
images come from the API and there is a whole path of every image and its a web url
ArrayList<String> imagepath1 = i.getExtras().getStringArrayList("event_photo");
for (int i1 = 0; i1 < imagepath1.size(); i1++) {
stockArr = new String[imagepath1.size()];
stockArr = imagepath1.toArray(stockArr);
// imagepath = stockArr.toString();
String imagepath = stockArr[i1];
}
if (!TextUtils.isEmpty(imagepath)) {
if (imagepath.endsWith(".jpeg") || imagepath.endsWith(".jpg")
|| imagepath.endsWith(".gif") || imagepath.endsWith(".png")) {
imageLoader = new ImageLoader(mcontext);
mUpdateResults = new Runnable() {
public void run() {
Animation rotateimage = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.alpha);
for (int i2 = 0; i2 < stockArr.length; i2++) {
imageLoader.DisplayImage(imagepath1.get(i2),
imagetop);
// imagetop.startAnimation(rotateimage);
/*imagetop.setImageResource(number[currentimageindex
% stockArr.length]);*/
}
}
};
final Handler mHandler = new Handler();
int delay = 500; // delay for 1 sec.
int period = 5000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);
} else {
}
}
Please Help if any one have a solution
Related
I want to play the audio two times before playing the next item.
This is my class where I want to display the items.
public void startSlides() {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
Intent intent = getIntent();
int setId = intent.getIntExtra("SelectedSetId",0);
databaseAccess.open();
setId = setId + 1;
List<byte[]> audio = databaseAccess.getAudioA(setId);
if (i > audio.size()){
i = 0;
}else{
playAudio(audio.get(i));
playAudio(audio.get(i));
i++;}
List<String> vocab = databaseAccess.getVocabNameA(setId);
textView2.setText(vocab.get(k));
k++;
textView.setText(+j + "/" + vocab.size());
j++;
databaseAccess.close();
if (j == vocab.size() + 1) {
timer.cancel();
}
}
});
}
}, 0, DURATION);
}
I think I have to add something at the playAudio(audio.get(i)), but I have no idea how should I do. If I do this, it plays the audio at the same time. Thanks.
if (i > audio.size()){
i = 0;
}else{
playAudio(audio.get(i));
playAudio(audio.get(i));
i++; }
Im trying to animate the three dots of "Loading..." textview as follows,
Handler handler = new Handler();
for (int i = 100; i <= 3500; i =i+100) {
final int finalI = i;
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(finalI %300 == 0){
loadigText.setText("Loading.");
}else if(finalI %200 == 0){
loadigText.setText("Loading..");
}else if(finalI %100 == 0){
loadigText.setText("Loading...");
}
}
}, i);
The problem is that ,
1. Im unable to animate it infinitely till the dialog is visible.
2. Im unable to reduce the speed of Three dots animation,
How can I be able to sort this out
Example:
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int count = 0;
#Override
public void run() {
count++;
if (count == 1)
{
textView.setText("Loading.");
}
else if (count == 2)
{
textView.setText("Loading..");
}
else if (count == 3)
{
textView.setText("Loading...");
}
if (count == 3)
count = 0;
handler.postDelayed(this, 2 * 1000);
}
};
handler.postDelayed(runnable, 1 * 1000);
I searched other posts and I didn't find a case matching my problem, so please do not refer me to other posts.
I have a list of hundreds of different times starting like the below list:
timeList = {5, 13, 21, 40, ...}.
Suppose that they are saved in an arraylist. That means some task must be executed at 5 secs, 13 secs,... from a starting point . How can I use a Timer or any other way to achieve this?
Also I may have the timeList in a format like this if it helps:
timeList = {8:05, 8:13, 8:21, 8:40, ...}.
My current timer is like this:
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
synchronized public void run() {
//do some task
}
}, startPoint, Period);
But this is obviously working at fixed rate!
By using Timer.schedule(TimerTask task, long delay), set all your timers and wait for them to execute
Assuming you are getting in MM:SS format (ex: 8:58):
ArrayList<String> your_arraylist = new ArrayList<String>();
//your list above, fill it with MM:SS
final ArrayList<Long> seconds = new ArrayList<Long>();
final ArrayList<Long> minutes = new ArrayList<Long>();
long first_minute;
for (String time : your_arraylist) {
minutes.add(Long.parseLong(time.split(":")[0]));
seconds.add(Long.parseLong(time.split(":")[1]));
}
first_minute = minutes.get(0);
for (int i = 0; i < seconds.size(); i++) {
long waiting_duration;
if (seconds.get(i) < Calendar.SECOND) {
waiting_duration = (60 - Calendar.SECOND + seconds.get(i)) * 1000;
}
else {
waiting_duration = (seconds.get(i) - Calendar.SECOND) * 1000;
}
waiting_duration += (minutes.get(i) - first_minute) * 60 * 1000;
Timer timer = new Timer();
TimerTask timer_task = new TimerTask() {
#Override
public void run() {
// do jobs
}
};
timer.schedule(timer_task, waiting_duration);
}
Don't use Timer use a Handler instead.
private int[] mTimeList = {5000, 2000, 7000, 4000};
private int mCount;
private int mDelay;
private Handler mHandler = new Handler();
private void initRepeatedTimer() {
mHandler.postDelayed(new Runnable() {
public void run() {
Log.e(TAG, "run: " + mCount);
mDelay = mTimeList[mCount];
if (mCount < mTimeList.length - 1)
mCount++;
else
mCount = 0;
mHandler.postDelayed(this, mDelay);
}
}, mDelay);
}
You wil notice that the log is printed as per delay in the list.
i want to change image of ImageView inside of a DialogFragment every 1s ;
the code is running ok in android 5 or 4 . but in android 2.3.7 (api 10) it changes ui for once and then doesnt change ui anymore .meanwhile thread is running okay
so far , i tried handler message method with a Timer , handler normal method , runOnUIThread . View.Post method but no luck
Bitmap[] onItems = new Bitmap[3];
Bitmap[] offItems= new Bitmap[3];
ImageView[] cheatHoldrs = new ImageView[3];
public Dialog onCreateDialog(Bundle savedInstanceState) {
//all Bitmaps and ImageView has been inited
myMethod();
}
View.Post Method :
for (int i = 0; i < 3; i++) {
final int j = i;
cheatHoldrs[j].post(new Runnable() {
#Override
public void run() {
random Random = new Random();
cheatHoldrs[j].setImageBitmap((random.nextInt(3) % 3 == 1) ? onItems[j]
: offItems[j]);
cheatHoldrs[j].postDelayed(this, 1000);
}
});
}
RunOnUIThread
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Random random = new Random();
for (int j = 0; j < 3; j++)
cheatHoldrs[j]
.setImageBitmap((random.nextInt(3) % 3 == 1) ? onItems[j]
: offItems[j]);
new Handler().postDelayed(this, 1000);
}
});
Noraml Handler method :
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
Random random = new Random();
for (int j = 0; j < 3; j++)
cheatHoldrs[j]
.setImageBitmap((random.nextInt(3) % 3 == 1) ? onItems[j]
: offItems[j]);
handler.postDelayed(this, 1000);
}
});
Handler Message Method :{
in global scope :
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
Random random = new Random();
for (int j = 0; j < 3; j++)
cheatHoldrs[j]
.setImageBitmap((random.nextInt(3) % 3 == 1) ? onItems[j]
: offItems[j]);
}
};
in OnCreateDialog:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.obtainMessage(1).sendToTarget();
}
}, 0, 1000);
}
EDIT:
i finally know whats the problem is . threads are working fine and if i dont use animation, ui updates . when dialog starts showing . i start an animation and move ImageViews (cheatHoldrs) . after the animation ImageViews image does not changes .
i tried 9oldroid animation and TranslateAnimation like this but still dosent change ui after animation :
for api > 11 i use ObjectAnimator and it works fine , but for api < 11 i use this methods
TranslateAnimation animation = new TranslateAnimation(0, forLeft, 0, 0);
animation.setFillAfter(true);
animation.setDuration(1000);
cheatHoldrs[1].startAnimation(animation);
TranslateAnimation animation2 = new TranslateAnimation(0, forLeft, 0, 0);
animation2.setFillAfter(true);
animation2.setDuration(1000);
cheatHoldrs[2].startAnimation(animation2);
TranslateAnimation animation3 = new TranslateAnimation(0, forRight, 0, 0);
animation3.setFillAfter(true);
animation3.setDuration(1000);
cheatHoldrs[0].startAnimation(animation3);
and nineoldandroids method :
com.nineoldandroids.animation.ObjectAnimator transitionForFirst = com.nineoldandroids.animation.ObjectAnimator
.ofFloat(cheatHoldrs[0], "translationX", forRight);
transitionForFirst.setDuration(1000);
com.nineoldandroids.animation.ObjectAnimator transitionForSecound = com.nineoldandroids.animation.ObjectAnimator
.ofFloat(cheatHoldrs[1], "translationX", forLeft);
transitionForSecound.setDuration(1000);
com.nineoldandroids.animation.ObjectAnimator transitionForThird = com.nineoldandroids.animation.ObjectAnimator
.ofFloat(che
atHoldrs[2], "translationX", forLeft);
transitionForThird.setDuration(1000);
transitionForFirst.start();
transitionForSecound.start();
transitionForThird.start();
Since AnimationDrawable's memory blows up after a few images, I needed to write my own. Google has some demonstrations regarding efficient frame animations, but it is very complicated(needlessly?).
Here is my initial approach, can I get some experts/smart ppl to chime in?
Create a Handler on the UI Thread.
create runables (A) for the handler.
inside the runables (A), I create AsyncTasks for each frame.
each AsyncTask is run using PostDelay to achieve desired frame rate.
the target ImageVew is set wigh the new frame from the AsyncTask.
Here is the code:
private void addTask(Handler handler, final String animationName, final int frameNumber, final AsyncTaskLoadBitmap asyncLoadBitmap, int frameIndex) {
Runnable runnable = new Runnable() {
#Override
public void run() {
int frameIdentifier = getResources().getIdentifier(animationName, "drawable", getPackageName());
Log.v("Runable","adding name");
new AsyncTaskLoadBitmap(imageView,thisActivity).execute(frameIdentifier);
}
};
boolean done = handler.postDelayed(runnable,100 + (100 * frameIndex));
Log.v("PostDelayed","Done");
}
The problem I'm having is making it loop and a way to stopping the loop.
What could be a better approach?
try this:
final ImageView iv = new ImageView(this);
setContentView(iv);
final int LOAD = 1;
final int SHOW = 2;
HandlerThread loader = new HandlerThread("frameLoader");
loader.start();
Callback callback = new Callback() {
int idx = 0;
int[] ids = {
R.drawable.ic_launcher, R.drawable.layer0, R.drawable.layer1,
};
Resources res = getResources();
#Override
public boolean handleMessage(Message msg) {
if (msg.what == LOAD) {
Log.d(TAG, "handleMessage LOAD " + Thread.currentThread().getName());
idx++;
if (idx < 18) {
Bitmap btm = BitmapFactory.decodeResource(res, ids[idx % ids.length]);
msg = mUIHandler.obtainMessage(SHOW, btm);
mUIHandler.sendMessage(msg);
}
} else
if (msg.what == SHOW) {
Log.d(TAG, "handleMessage SHOW " + Thread.currentThread().getName());
iv.setImageBitmap((Bitmap) msg.obj);
mBackgroundHandler.sendEmptyMessageDelayed(LOAD, 500);
}
return true;
}
};
mBackgroundHandler = new Handler(loader.getLooper(), callback);
mUIHandler = new Handler(callback);
mBackgroundHandler.sendEmptyMessage(LOAD);