文本和图像的动画

发布于 2024-11-05 23:35:41 字数 2145 浏览 1 评论 0原文

我正在尝试将图像(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上的星空 2024-11-12 23:35:41

试试这个。我已经更正了您的代码,

handToUI.post(new Runnable()
{
     ImageView image = (ImageView) findViewById(R.id.picView);
     TextView text = (TextView) findViewById(R.id.usertext);
     public void run()
     {
          try
          {
              Thread.sleep(5000);
          }
          catch (InterruptedException e)
          {
              e.printStackTrace();
          }
          text.setText(si);
          image.setImageResource(ii);
     }
});

希望对您有所帮助。

Try this. I have corrected your code

handToUI.post(new Runnable()
{
     ImageView image = (ImageView) findViewById(R.id.picView);
     TextView text = (TextView) findViewById(R.id.usertext);
     public void run()
     {
          try
          {
              Thread.sleep(5000);
          }
          catch (InterruptedException e)
          {
              e.printStackTrace();
          }
          text.setText(si);
          image.setImageResource(ii);
     }
});

I hope it is help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文