Android動畫繪製者Animator與LayoutAnimator

來源:互聯網
上載者:User

Android動畫繪製者Animator與LayoutAnimator
概述:

與Android的Animation控制項相比,Animator與LayoutAnimator處理後的控制項完成動畫效果後不會回複原狀。Animator只使用與View對象,和Animation類似,Animator也能通過xml定義,需要用objectAnimator來定義各種動畫效果。
LayoutView常用於給控制項的添加刪除增加一些特殊效果,如淡入淡出等。

DemoAnimator

Animator動態方式:

        //改變大小        ObjectAnimator.ofFloat(mImageViewAnim,scaleX,0.0f,1.0f).setDuration(2000).start();

Animator靜態方式,需要在res目錄下建立一個animator檔案夾,在此檔案夾裡建立一個xml檔案,我的是scale:

                        

在代碼中載入R.animator.scale資源

        AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(),R.animator.scale);        set.setTarget(view);//animator將永久改變view的狀態        set.start();
LayoutAnimator
public class LayoutAnimatorActivity extends Activity {    private Button mButtonAdd;    private LinearLayout mLinearLayout;    private int count;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.layout_animator);        mButtonAdd = (Button) findViewById(R.id.button_addAnimator);        mLinearLayout = (LinearLayout) findViewById(R.id.linear_layout);    //定義一個LayoutTransition,用於設定各種動畫效果        LayoutTransition transition = new LayoutTransition();        //        transition.getDuration(2000);        //當子控制項被添加後出現時,動畫效果是靜態實現的R.animator.scale        transition.setAnimator(LayoutTransition.APPEARING, AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.scale));//當CHANGE_APPEARING是,調用預設狀態改變的動畫效果       transition.setAnimator(LayoutTransition.CHANGE_APPEARING,transition.getAnimator(                LayoutTransition.CHANGE_APPEARING));//當子控制項消失時,調用預設的消失的動畫效果          transition.setAnimator(LayoutTransition.DISAPPEARING,transition.getAnimator(                LayoutTransition.DISAPPEARING));//當CHANGE_APPEARING是,調用預設的消失時狀態改變的動畫效果         transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,transition.getAnimator(                LayoutTransition.CHANGE_DISAPPEARING));        mLinearLayout.setLayoutTransition(transition);        mButtonAdd.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                count++;                Button btn = new Button(getApplicationContext());                //LayoutParams類是用於child view(子視圖) 向 parent view(父視圖)傳達自己各種屬性的容器                ViewGroup.LayoutParams params =                        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);                btn.setLayoutParams(params);                btn.setText(按鈕 + count);                //設定btn的初始尺寸大小                btn.setScaleX(0f);                btn.setScaleY(0f);                //給要新添加的Button類子控制項加點擊事件:點擊刪除自己                btn.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        mLinearLayout.removeView(v);                    }                });                //在mLinearLayout中添加這個子控制項                mLinearLayout.addView(btn);            }        });    }}

結果示範:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.