slok
V2EX  ›  Android

动画执行问题

  •  
  •   slok · Jun 16, 2015 · 14101 views
    This topic created in 4167 days ago, the information mentioned may be changed or developed.

    ========================代码一========================

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_anim);

    dotImg = (ImageView) findViewById(R.id.anim_dotimg);
    btn = (Button) findViewById(R.id.anim_btn);
    
    btn.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlphaAnimation alphaAnim = new AlphaAnimation(0.5f, 1f);
            alphaAnim.setDuration(2000);
            dotImg.setAnimation(alphaAnim);
            alphaAnim.start();
    
            Log.d("test", "start anim");
        }
    });
    

    }

    ========================代码一========================

    ========================代码二========================

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_anim);

    dotImg = (ImageView) findViewById(R.id.anim_dotimg);
    btn = (Button) findViewById(R.id.anim_btn);
    
    btn.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlphaAnimation alphaAnim = new AlphaAnimation(0.5f, 1f);
            alphaAnim.setDuration(2000);
            dotImg.startAnimation(alphaAnim);
    
            Log.d("test", "start anim");
        }
    });
    

    }

    ========================代码二========================

    Q1:为何代码一动画无效果?或者说不执行?
    Q2:有些文章说,更新UI必须在UI主线程。那么请问一些事件,如Click、Destroy等,我如何分辨哪些事件并不是UI主线程在执行呢?

    5 replies  •  2015-06-16 15:15:03 +08:00
    kcworms
    kcworms
    Jun 16, 2015
    1
    1.动画没有应用到View上
    2.只要自己不采取任何手段开新线程,都是在UI线程(不确定,求指出例外)
    hahasong
    hahasong
    Jun 16, 2015
    2
    Q1:动画官方建议是用 ValueAnimator 及其子类
    Q2:所有在 Activity主线程上执行的代码都属于UI线程,另起的Thread, HandleThread, AscyTask 上执行的代码属于后台线程
    slok
    slok
    OP
    Jun 16, 2015
    3
    @kcworms 感谢回答,动画没有应用到View上我不太明白啊,代码一的动画代码,放在onCreate却可以执行啊。
    slok
    slok
    OP
    Jun 16, 2015
    4
    @hahasong 谢谢
    slok
    slok
    OP
    Jun 16, 2015
    5
    @hahasong 可否加QQ请教?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1678 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 16:19 · PVG 00:19 · LAX 08:19 · JFK 11:19
    ♥ Do have faith in what you're doing.