文本和图像的动画
我正在尝试将图像(imageView)及其关联文本(textView)制作成动画。 我遵循了这里的建议,并使用 AsyncTask 和 UI 线程的处理程序来动态更改图像和文本。这应该是一个简单的代码,但问题是我只获取/看到最后一个图像和文本。在下面的代码中,仅显示 im2 (第二个图像)和 t2 (第二个文本)。
有人能说出问题是什么吗?
我的代码是这样的:
public class picshow extends Activity
{
private static Integer[] Imgid = {R.drawable.im1, R.drawable.im2};
private static Integer[] Strid = {R.string.t1, R.string.t2};
private Handler handToUI = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.picView);
TextView text = (TextView) findViewById(R.id.usertext);
myTask task = new myTask();
task.execute("nothing");
}
private class myTask extends AsyncTask<String, Void, String >
{
@Override
protected String doInBackground(String... params )
{
//Do noting and return a String. a preparation for future code
return params[0];
}
@Override
protected void onPostExecute( String result )
{
//send message to UI
for(int i = 0;i<Strid.length;i++)
{
final int si = Strid[i];
final int ii = Imgid[i];
try
{
Thread.sleep(5000);
handToUI.post(new Runnable()
{
ImageView image = (ImageView) findViewById(R.id.picView);
TextView text = (TextView) findViewById(R.id.usertext);
public void run()
{
text.setText(si);
image.setImageResource(ii);
}
});
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
谢谢 里皮
I am trying to animate images (imageView) together with their associate text (textView).
I followed an advice I got here and used AsyncTask with handler to UI thread to dynamically change the images and text. This should be a simple code but the problem is that i get/see only the last image and text. in the case of the code below, only im2 (the second image) and t2 (the second text) will be shown.
can someone tell what the problem is?
my code goes like this:
public class picshow extends Activity
{
private static Integer[] Imgid = {R.drawable.im1, R.drawable.im2};
private static Integer[] Strid = {R.string.t1, R.string.t2};
private Handler handToUI = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.picView);
TextView text = (TextView) findViewById(R.id.usertext);
myTask task = new myTask();
task.execute("nothing");
}
private class myTask extends AsyncTask<String, Void, String >
{
@Override
protected String doInBackground(String... params )
{
//Do noting and return a String. a preparation for future code
return params[0];
}
@Override
protected void onPostExecute( String result )
{
//send message to UI
for(int i = 0;i<Strid.length;i++)
{
final int si = Strid[i];
final int ii = Imgid[i];
try
{
Thread.sleep(5000);
handToUI.post(new Runnable()
{
ImageView image = (ImageView) findViewById(R.id.picView);
TextView text = (TextView) findViewById(R.id.usertext);
public void run()
{
text.setText(si);
image.setImageResource(ii);
}
});
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
thanks
lipi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个。我已经更正了您的代码,
希望对您有所帮助。
Try this. I have corrected your code
I hope it is help you.