全局变量用于第三项活动

时间:2011-09-06 10:53:47

标签: android

{p> AnimationDrawable animation;Frame by Frame animation中使用。我这样使用它::

try {
               ImageView img = (ImageView) findViewById(R.id.girl_anim);
               animation = new AnimationDrawable();
               new xyz().execute();
                animation.setOneShot(false);
               img.setBackgroundDrawable(animation);
               img.post(new Starter());
           } catch (Exception e) {
            }

外部课程::

  private class xyz extends AsyncTask<Void, Void, Void> {
            //private final ProgressDialog dialog = new ProgressDialog(tranning.this);

            protected void onPreExecute() {
                /*this.dialog.setMessage("Please Wait...");
                this.dialog.show();
                */

            }

            @Override
            protected Void doInBackground(Void... arg0) {

                try {
                    for(int i = 1;i<54;i++)
                    {
                    Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                    "http://test/MRESC/images/test/girl/"+"girl000"+i+".png")
                    .getContent());
                    Drawable frame =new BitmapDrawable(bitmap);
                    animation.addFrame(frame, 50);
                    }
                } catch (Exception e) {

                }

                return null;
            }

            protected void onPostExecute(final Void unused) {
                //if (this.dialog.isShowing()) {
                //  this.dialog.dismiss();

                //}

            }
        }
    class Starter implements Runnable {

        public void run() {
            animation.start();        
        }


    }

现在这个代码在第三个活动中使用了。但问题是我想在第一个活动中创建它,所以我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

您可以将变量设置为static,也可以将其作为参数传递给它们(putExtra),但是因为您在异步任务中使用它,您可能必须使用runnable:

handler.postDelayed(new Runnable() {
    public void run() {
//do something with a variable from the activity
}
}, 100);

还在活动中声明处理程序:

private final Handler handler = new Handler();