簡體   English   中英

無法在“浮動操作”按鈕上重復動畫

[英]Cannot repeat animation on Floating Action Button

現在,我可以為FloatingActionButton設置動畫了,我還有另一個問題。

當我單擊FAB時,FAB的旋轉開始並一直進行到AsyncTask完成其工作為止。

以下是與代碼相關的部分:

@OnClick(R.id.floating_action_button) //Butterknife feature
    public void refreshAccountInfo(){
        APIHandler api = new APIHandler(databaseHelper, c, getActivity());
        AccountSync accountSync = new AccountSync(api);
        accountSync.execute();
    }

    public class AccountSync extends AsyncTask<Void,Void,Account> {

        APIHandler apiHandler;
        ObjectAnimator animation;

        public AccountSync(APIHandler api){
            apiHandler = api;
        }

        @Override
        protected void onPreExecute(){
            FloatingActionButton button = ButterKnife.findById(getActivity(), R.id.floating_action_button);
            PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, -360);
            animation = ObjectAnimator.ofPropertyValuesHolder(button, pvhR);
            animation.setRepeatCount(Animation.INFINITE);
            animation.setDuration(1500);
            animation.start();
        }

        @Override
        protected Account doInBackground(Void... params) {
            Account a;
            try {
                a = apiHandler.getAccountInfo();
                return a;
            } catch (final ResponseException e) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(apiHandler.getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });
                return null;
            }
        }

        @Override
        protected void onPostExecute(Account result) {
            animation.setRepeatCount(0);
            if(result!=null){
                a = result;
                showInfo();
                Toast.makeText(getActivity(),"Account synchronization done",Toast.LENGTH_SHORT).show();
            }
        }

    }

我的問題是,當我第一次觸發按鈕時,動畫可以正常播放,但是當我嘗試單擊更多次時,AsyncTask將完成其工作,但是旋轉動畫將永遠不會播放,直到我完全重新加載片段。

我該如何解決?

例如,您可以像這樣在onPostExecute中結束動畫:

 @Override
    protected void onPostExecute(Account result) {

        if(animation.isRunning())
            animation.end();           

        ...
        }
    }

這應該比將repeatCount設置為0更有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM