آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

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

tiam  9 سال پیش  9 سال پیش
+1 0

سلام میخوام توی یک فرگمنت عکس داخل 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.
+1 0
یک پیشنهاد برای عوض شدن عکس ها با انیمیشن : لینک (9 سال پیش)
+1 0
ارایه از جنس int قرار بدید و مقادیر درون آرایه رو بصورت R.drawable.img قرار بدید (9 سال پیش)
0 0
از نوع int هم گذاشتم نتیجه تفاوتی نداشت (9 سال پیش)
0 0
اینم نمونه کدش 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 سال پیش)
0 0
لیست خطاها رو از logcat بخونید و قرار بدید. (9 سال پیش)
0 0
هر کاری میکنم عکس 80 کیلویی آپلود نمیشه که logcat رو بزارم ..... به دستور imageView.setImageResource(id) گیر میده (9 سال پیش)
 برای این سوال 2 پاسخ وجود دارد.
پاسخ به سوال 
tiam  9 سال پیش
0 0

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


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.
0 0
LogCat رو بررسی کنید ببینید از کجا خطا می گیره؟ (9 سال پیش)
0 0
هر کاری میکنم عکس 80 کیلویی آپلود نمیشه ..... به دستور imageView.setImageResource(id) گیر میده (9 سال پیش)
پاسخ به سوال 
tiam  9 سال پیش
+1 0

حل شد اینم نمونه کد نهایی ... دوستان میتونن استفاده کنن داخل 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.
0 0
من از این کد داخل یه فرگمنت استفاده کردم ولی برنامه کرش میکنه! خطای این قسمت رو میگیره: getContext().getPackageName() چطور رفعش کنم؟ (9 سال پیش)
0 0
بجای () getContext از ()getActivity استفاده کردم (9 سال پیش)
0 0
دقیقا دارم از همین استفاده میکنم مشکلی نداره (9 سال پیش)

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