动态绘制动画

发布于 2024-12-19 17:48:03 字数 2342 浏览 0 评论 0原文

在我的应用程序中,我需要动态获取所有内容。所以我试图动态获取可绘制动画。我尝试了下面的代码,但没有得到任何输出。请帮助我哪里出错了。

        RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        img.setLayoutParams(lp);
        relativeLayout.addView(img);
        strtbtn= new Button(this);
        strtbtn.setId(1);
        RelativeLayout.LayoutParams lp1=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        strtbtn.setLayoutParams(lp1);
        relativeLayout.addView(stpbtn);
        stpbtn=new Button(this);
         stpbtn.setId(2);
         RelativeLayout.LayoutParams lp2=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
         stpbtn.setLayoutParams(lp2);
         relativeLayout.addView(stpbtn);
         setContentView(relativeLayout);
        BitmapDrawable frame0 = (BitmapDrawable)getResources().getDrawable(R.drawable.f0);
        BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.f1);
        BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.f2);
        BitmapDrawable frame3 = (BitmapDrawable)getResources().getDrawable(R.drawable.f3);
        BitmapDrawable frame4 = (BitmapDrawable)getResources().getDrawable(R.drawable.f4);
        BitmapDrawable frame5 = (BitmapDrawable)getResources().getDrawable(R.drawable.f5);

        int reasonableDuration = 750;
        mAnimation = new AnimationDrawable();

        mAnimation.addFrame(frame0, reasonableDuration);
        mAnimation.addFrame(frame1, reasonableDuration);
        mAnimation.addFrame(frame2, reasonableDuration);
        mAnimation.addFrame(frame3, reasonableDuration);
        mAnimation.addFrame(frame4, reasonableDuration);
        mAnimation.addFrame(frame5, reasonableDuration);

        img.setBackgroundDrawable(mAnimation);
        strtbtn = (Button) findViewById(1);
        strtbtn.setOnClickListener(this);
        stpbtn = (Button) findViewById(2);
        stpbtn.setOnClickListener(this);
    }

    public void onClick(View v) {

      if(v.getId()== 3) {
          mAnimation.start();
          mAnimation.setOneShot(false);
      }
      else
        mAnimation.stop();
    }
}

in my apps i need to get everything dynamically. so i am trying to get drawable animation dynamically.I tried for below code but am not getting any output.please help me where i did the mistake.

        RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        img.setLayoutParams(lp);
        relativeLayout.addView(img);
        strtbtn= new Button(this);
        strtbtn.setId(1);
        RelativeLayout.LayoutParams lp1=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        strtbtn.setLayoutParams(lp1);
        relativeLayout.addView(stpbtn);
        stpbtn=new Button(this);
         stpbtn.setId(2);
         RelativeLayout.LayoutParams lp2=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
         stpbtn.setLayoutParams(lp2);
         relativeLayout.addView(stpbtn);
         setContentView(relativeLayout);
        BitmapDrawable frame0 = (BitmapDrawable)getResources().getDrawable(R.drawable.f0);
        BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.f1);
        BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.f2);
        BitmapDrawable frame3 = (BitmapDrawable)getResources().getDrawable(R.drawable.f3);
        BitmapDrawable frame4 = (BitmapDrawable)getResources().getDrawable(R.drawable.f4);
        BitmapDrawable frame5 = (BitmapDrawable)getResources().getDrawable(R.drawable.f5);

        int reasonableDuration = 750;
        mAnimation = new AnimationDrawable();

        mAnimation.addFrame(frame0, reasonableDuration);
        mAnimation.addFrame(frame1, reasonableDuration);
        mAnimation.addFrame(frame2, reasonableDuration);
        mAnimation.addFrame(frame3, reasonableDuration);
        mAnimation.addFrame(frame4, reasonableDuration);
        mAnimation.addFrame(frame5, reasonableDuration);

        img.setBackgroundDrawable(mAnimation);
        strtbtn = (Button) findViewById(1);
        strtbtn.setOnClickListener(this);
        stpbtn = (Button) findViewById(2);
        stpbtn.setOnClickListener(this);
    }

    public void onClick(View v) {

      if(v.getId()== 3) {
          mAnimation.start();
          mAnimation.setOneShot(false);
      }
      else
        mAnimation.stop();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夜夜流光相皎洁 2024-12-26 17:48:03

在这里我已经举了例子尝试一下并告诉我会发生什么?

AnimationDrawable Tranninganimation1;

Tranninganimation1 = new AnimationDrawable();
        new playninzi().execute();

        Tranninganimation1.setOneShot(false);
        ImageView img = (ImageView) findViewById(R.id.girl_anim);
        img.setBackgroundDrawable(Tranninganimation1);
        img.post(new Starter());

异步任务::

private class playninzi extends AsyncTask<Void, Void, Void> {

        protected void onPreExecute() {

            try {

                // Drawable frame = new BitmapDrawable(R.drawable.girl10001);

                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);

                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
            } catch (Exception e) {

            }

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            return null;
        }
    }

    class Starter implements Runnable {

        public void run() {
            Tranninganimation1.start();

        }

    }

here i have set example try it and tell me what happen?

AnimationDrawable Tranninganimation1;

Tranninganimation1 = new AnimationDrawable();
        new playninzi().execute();

        Tranninganimation1.setOneShot(false);
        ImageView img = (ImageView) findViewById(R.id.girl_anim);
        img.setBackgroundDrawable(Tranninganimation1);
        img.post(new Starter());

AsyncTask ::

private class playninzi extends AsyncTask<Void, Void, Void> {

        protected void onPreExecute() {

            try {

                // Drawable frame = new BitmapDrawable(R.drawable.girl10001);

                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);

                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
                Tranninganimation1.addFrame(
                        getResources()
                                .getDrawable(R.drawable.boy_listening0030), 50);
            } catch (Exception e) {

            }

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            return null;
        }
    }

    class Starter implements Runnable {

        public void run() {
            Tranninganimation1.start();

        }

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