LogCat رو بررسی کنید ببینید از کجا خطا می گیره؟
بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
پرسش و پاسخ
تعویض عکس imageview بصورت اتوماتیک
مدیریت گفتگو
آخرین تغییر: 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.
پاسخها 2
اینم نمونه کدش
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.
نظرات
هر کاری میکنم عکس 80 کیلویی آپلود نمیشه ..... به دستور imageView.setImageResource(id) گیر میده
حل شد اینم نمونه کد نهایی ... دوستان میتونن استفاده کنن داخل 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.
نظرات
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .
ویرایش
پیام
نظرات