我的可绘制文件夹中有两张图片,我希望每隔 x 次在我的 View 中交替显示这两张图片。 我尝试使用 Asynctask 但没有用,而且我找不到解决方案。
我的 xml 代码
<ImageView
android:id="@+id/imageload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="64dp"
android:adjustViewBounds="false"
android:baselineAlignBottom="false"
android:contentDescription="@string/imatge"
android:cropToPadding="false"
android:fitsSystemWindows="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/hdtitol2" />
我给类(class)打电话:
new ModifyImage().execute(null,null,null);
主类包含带有异步代码的类
public class ModifyImage extends AsyncTask<Void, Void, Void> {
ImageView img= (ImageView)findViewById(R.id.imageload);
@Override
protected void onPreExecute(){
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
int i = 0;
boolean opt = true;
boolean exit = false;
while(!exit){
if(i == 1000){
i = 0;
if(!opt){
img.setImageResource(R.drawable.blackhdtitol2);
opt =true;
}else{
opt = false;
img.setImageResource(R.drawable.hdtitol2);
}
}
i++;
}
return null;
}
@Override
protected void onPostExecute(Void i){
}
}
最佳答案
这样做,
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Integer tag = (Integer) img.getTag();
if(tag == R.drawable.blackhdtitol2){
img.setImageResource(R.drawable.blackhdtitol2);
img.setTag(R.drawable.blackhdtitol2);
}else{
img.setImageResource(R.drawable.hdtitol2);
img.setTag(R.drawable.hdtitol2);
}
}
}, 60*1000);
关于android - 屏幕 ImageView 中的替代图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16377739/