Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 動畫詳解之屬性動畫(Property Animation)(下)

Android 動畫詳解之屬性動畫(Property Animation)(下)

編輯:關於Android編程

Hello,大家好,最近好長時間沒有寫博客了,因為我決定辭職了。

廢話不多說,我們還是來看屬性動畫在上一篇 Android 動畫詳解之屬性動畫(Property Animation)中我們簡單的介紹了一下屬性動畫的用法,其實屬性動畫還有更多有趣的用法。

1,在xml中使用

在eclipse中我們右鍵新建xml可以選擇新建屬性動畫,如圖

vcrsz6S1xNK7xLs8L3A+CjxwPjxpbWcgc3JjPQ=="/uploadfile/Collfiles/20141202/20141202081511156.png" alt="\">

然後我們用智能提示就可以看到更熟悉的

\

沒錯,這下我們應該知道怎麼用xml布局來寫屬性動畫了吧



	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_animation);
		button = (Button) findViewById(R.id.btn_anim);
		button.setOnClickListener(new OnClickListener() {

			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				Animator animator = AnimatorInflater.loadAnimator(
						AnimationActivity.this, R.animator.animation);
				animator.setTarget(button);
				animator.start();
			}
		});
	}
效果

\

同時我們可以看到在新建xml的時候是有set的,set的用法同樣很簡單




    
    

2,布局動畫

當容器中的視圖層次發生變化時存在過渡的動畫效果,這個我們先來看看ApiDemo的效果。

\

可以看到我們勾選了in於out之後我們新增的button或者remove掉的button會有一個動畫效果,接下來我們來看代碼

 // Check for disabled animations
        CheckBox appearingCB = (CheckBox) findViewById(R.id.appearingCB);
        appearingCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setupTransition(transitioner);
            }
        });
        CheckBox disappearingCB = (CheckBox) findViewById(R.id.disappearingCB);
        disappearingCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setupTransition(transitioner);
            }
        });

這是倆個checkbox,我們再看setupTransition方法

	private void setupTransition(LayoutTransition transition) {
        CheckBox customAnimCB = (CheckBox) findViewById(R.id.customAnimCB);
        CheckBox appearingCB = (CheckBox) findViewById(R.id.appearingCB);
        CheckBox disappearingCB = (CheckBox) findViewById(R.id.disappearingCB);
        CheckBox changingAppearingCB = (CheckBox) findViewById(R.id.changingAppearingCB);
        CheckBox changingDisappearingCB = (CheckBox) findViewById(R.id.changingDisappearingCB);
        transition.setAnimator(LayoutTransition.APPEARING, appearingCB.isChecked() ?
                (customAnimCB.isChecked() ? customAppearingAnim : defaultAppearingAnim) : null);
        transition.setAnimator(LayoutTransition.DISAPPEARING, disappearingCB.isChecked() ?
                (customAnimCB.isChecked() ? customDisappearingAnim : defaultDisappearingAnim) : null);
        transition.setAnimator(LayoutTransition.CHANGE_APPEARING, changingAppearingCB.isChecked() ?
                (customAnimCB.isChecked() ? customChangingAppearingAnim :
                        defaultChangingAppearingAnim) : null);
        transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,
                changingDisappearingCB.isChecked() ?
                (customAnimCB.isChecked() ? customChangingDisappearingAnim :
                        defaultChangingDisappearingAnim) : null);
    }

我們可以發現關鍵就是LayoutTransition,而且動畫產生也是依據

LayoutTransition.APPEARING;

LayoutTransition.DISAPPEARING;

LayoutTransition.CHANGE_APPEARING;

LayoutTransition.CHANGE_DISAPPEARING;

APPEARING新增view的動畫CHANGE_APPEARING對布局產生改變的動畫,那麼我們就可以依葫蘆畫瓢。


 private RelativeLayout relativeLayout;
   private Button mAdbtn;
   private int count = 0;
	@SuppressLint("NewApi") 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_animation);
		relativeLayout = (RelativeLayout)findViewById(R.id.relative);
		mAdbtn = (Button)findViewById(R.id.btn);
		final GridLayout gridLayout = new GridLayout(this);
		gridLayout.setColumnCount(5);
		relativeLayout.addView(gridLayout);
		gridLayout.setLayoutTransition(new LayoutTransition());
		mAdbtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				count++;
               Button button = new Button(AnimationActivity.this);
               button.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
                gridLayout.removeView(v);					
				}
			});
               button.setText("btn"+count);
               gridLayout.addView(button);
			}
		});
	}
效果

\

同時如果我們不喜歡默認的動畫效果也可以替換為自己喜歡的效果。

\

	mAdbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				count++;
				Button button = new Button(AnimationActivity.this);
				button.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						gridLayout.removeView(v);
					}
				});
				button.setText("btn" + count);
				layoutTransition.setAnimator(LayoutTransition.APPEARING,
						ObjectAnimator.ofFloat(button, "RotationX", 0, 360).setDuration(2000));
				gridLayout.setLayoutTransition(layoutTransition);
				gridLayout.addView(button);
			}
		});

ok,屬性動畫就介紹到這裡吧,也預祝大家都工作順利天天開心


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved