بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]
پرسش و پاسخ

تعویض عکس imageview بصورت اتوماتیک

مدیریت گفتگو
tiam 9 سال پیش
آخرین تغییر: 9 سال پیش

سلام میخوام توی یک فرگمنت عکس داخل imageview بصورت اتوماتیک چند ثانیه یکبار عوض بشه نمونه کدشم قرار میدم اما حین کار برنامه کرش میکنه دوستان نظر بدن

Timer timer = new Timer();

timer.schedule(new TimerTask() {
@Override
public void run() {
// Your logic here...
final String[] imageArray={"ic_food","ic_discount","ic_movie","ic_travel"};
// When you need to modify a UI element, do so on the UI thread.
// 'getActivity()' is required as this is being ran from a Fragment.
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// This code will always run on the UI thread, therefore is safe to modify UI elements.
if (cur<imageArray.length) {
Log.i("LOG","Cur < Array Length current = " + cur);

String imageName = imageArray[cur];
int id = getContext().getResources().getIdentifier(imageName, "drawable", getContext().getPackageName());
imageView.setImageResource(id);
cur++;
}else{
cur = 0;
String imageName = imageArray[cur];
int id = getContext().getResources().getIdentifier(imageName, "drawable", getContext().getPackageName());
imageView.setImageResource(id);
}
}
});
}
}, 0, 3000); // End of your timer code.

نظرات

علی 9 سال پیش
یک پیشنهاد برای عوض شدن عکس ها با انیمیشن : لینک
Criss 9 سال پیش
ارایه از جنس int قرار بدید و مقادیر درون آرایه رو بصورت R.drawable.img قرار بدید
tiam 9 سال پیش
از نوع int هم گذاشتم نتیجه تفاوتی نداشت
tiam 9 سال پیش
اینم نمونه کدش Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { لینک Your logic here... final int[] imageArray={R.drawable.ic_food,R.drawable.ic_discount,R.drawable.ic_movie,R.drawable.ic_travel}; لینک final String[] imageArray={"ic_food","ic_discount","ic_movie","ic_travel" لینک When you need to modify a UI element, do so on the UI thread. لینک 'getActivity()' is required as this is being ran from a Fragment. getActivity().runOnUiThread(new Runnable() { @Override public void run() { لینک This code will always run on the UI thread, therefore is safe to modify UI elements. لینک my text"); if (cur
حمید 9 سال پیش
لیست خطاها رو از logcat بخونید و قرار بدید.
tiam 9 سال پیش
هر کاری میکنم عکس 80 کیلویی آپلود نمیشه که logcat رو بزارم ..... به دستور imageView.setImageResource(id) گیر میده

پاسخ‌ها 2

tiam 9 سال پیش

اینم نمونه کدش


Timer timer = new Timer();

timer.schedule(new TimerTask() {
@Override
public void run() {
// Your logic here...
final int[] imageArray={R.drawable.ic_food,R.drawable.ic_discount,R.drawable.ic_movie,R.drawable.ic_travel};
// final String[] imageArray={"ic_food","ic_discount","ic_movie","ic_travel"



// When you need to modify a UI element, do so on the UI thread.
// 'getActivity()' is required as this is being ran from a Fragment.
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// This code will always run on the UI thread, therefore is safe to modify UI elements.
//myTextBox.setText("my text");
if (cur<imageArray.length) {
String imageName = String.valueOf(imageArray[cur]);
int id = getContext().getResources().getIdentifier(imageName, "drawable", getContext().getPackageName());
imageView.setImageResource(id);
cur++;
}else{
cur = 0;
String imageName = String.valueOf(imageArray[cur]);
int id = getContext().getResources().getIdentifier(imageName, "drawable", getContext().getPackageName());
imageView.setImageResource(id);
}
}
});
}
}, 0, 3000); // End of your timer code.

نظرات

Amir 9 سال پیش
LogCat رو بررسی کنید ببینید از کجا خطا می گیره؟
tiam 9 سال پیش
هر کاری میکنم عکس 80 کیلویی آپلود نمیشه ..... به دستور imageView.setImageResource(id) گیر میده
tiam 9 سال پیش

حل شد اینم نمونه کد نهایی ... دوستان میتونن استفاده کنن داخل fragment

imageView = (ImageView) root.findViewById(R.id.imgAnim);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// Your logic here...
final int[] imageArray={R.drawable.ic_food,R.drawable.ic_discount,R.drawable.ic_movie,R.drawable.ic_travel};
// When you need to modify a UI element, do so on the UI thread.
// 'getActivity()' is required as this is being ran from a Fragment.
if (isAdded()) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// This code will always run on the UI thread, therefore is safe to modify UI elements.
//myTextBox.setText("my text");
if (cur < imageArray.length) {
String imageName = String.valueOf(imageArray[cur]);
int id = getResources().getIdentifier(imageName, "drawable", getContext().getPackageName());
imageView.setImageResource(id);
cur++;
} else {
cur = 0;
String imageName = String.valueOf(imageArray[cur]);
int id = getResources().getIdentifier(imageName, "drawable", getContext().getPackageName());
imageView.setImageResource(id);
}
}
});
}
}
}, 0, 3000); // End of your timer code.

نظرات

آراز 9 سال پیش
من از این کد داخل یه فرگمنت استفاده کردم ولی برنامه کرش میکنه! خطای این قسمت رو میگیره: getContext().getPackageName() چطور رفعش کنم؟
آراز 9 سال پیش
بجای () getContext از ()getActivity استفاده کردم
tiam 9 سال پیش
دقیقا دارم از همین استفاده میکنم مشکلی نداره

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .