如何在Android的onCreate()中调用此动画?

时间:2019-05-13 21:49:33

标签: android animation oncreate

我遵循此link来满足动画要求。 https://medium.com/@ayushpguptaapg/how-to-add-shine-glare-effect-on-an-imageview-ab3e9e660307

这已经完成了按钮点击监听器。哪个工作正常。但是我想在不单击按钮的情况下执行此操作,因为此动画随着活动负载开始。 我已经搜索了很多,但是没有找到任何简单的解决方案。 谁能告诉我简单的解决方案。 预先感谢。


@ismail,我正在使用您的这段代码,如下所示:

public class MainActivity extends AppCompatActivity{

    Button shinebtn;
    ImageView img,shine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

          img = findViewById(R.id.img);
        shine = findViewById(R.id.shine);
         shinebtn=findViewById(R.id.button);


        doAnimation();


}
    public void doAnimation(){
        new Thread(new Runnable() {
            public void run() {
                final Animation animation = new TranslateAnimation(0, img.getWidth()+shine.getWidth(),0, 0);
                animation.setDuration(1000);
                animation.setFillAfter(false);
                animation.setRepeatCount(Animation.INFINITE);
                animation.setInterpolator(new AccelerateDecelerateInterpolator());
                shine.post(new Runnable() {
                    public void run() {
                        shine.startAnimation(animation);
                    }
                });
            }
        }).start();


    }


}

1 个答案:

答案 0 :(得分:0)

只需在onCreate()中设置元素后编写代码

   @Override
   protected void onCreate(Bundle SavedInstanceState){
    super.onCreate(SavedInstanceState);
    setContentView(yor_layout_source);
    ....

    ....
}

 @Override
 public void onResume(){
  super.onResume();  
  //do your animation code here 

 }

,这里是您的动画功能:

 public void doAnimation(){
   new Thread(new Runnable() {
    public void run() {
      Animation animation = new TranslateAnimation(0, img.getWidth()+shine.getWidth(),0, 0);
            animation.setDuration(550);
            animation.setFillAfter(false);
            animation.setInterpolator(new AccelerateDecelerateInterpolator());
      shine.post(new Runnable() {
        public void run() {
               shine.startAnimation(animation);
        }
      });
    }
  }).start();


    }