Property Animation (lower)

Source: Internet
Author: User

Property Animation (lower)

Hello, everyone. I haven't written a blog for a long time recently, because I decided to resign.

Let's just look at the usage of Property Animation in the previous explanation of Property Animation in Android, in fact, attribute animation has more interesting usage.

1. Use in xml

In eclipse, right-click "New xml" and choose "New property Animation" from the shortcut menu,

Vcrsz6S1xNK7xLs8L3A + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20141202/20141202081511156.png" alt = "\">

Then we can use smart prompts to see more familiar


That's right. Now we should know how to use xml layout to write property animations.

 
 


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")@Overridepublic void onClick(View v) {Animator animator = AnimatorInflater.loadAnimator(AnimationActivity.this, R.animator.animation);animator.setTarget(button);animator.start();}});}
Effect

At the same time, we can see that there is set when creating xml, and the set usage is also very simple.

 
 
      
      
  
 

2. layout Animation

When the view hierarchy in the container changes, there is a transitional animation effect. Let's take a look at the effect of ApiDemo.


We can see that the new button or remove button selected after in and out has an animation effect. Next let's look at the code.

 // 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);            }        });

These are two checkboxes. Let's look at the setupTransition method.

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);    }

We can find that the key is LayoutTransition, and Animation Generation is also the basis.

LayoutTransition. APPEARING;

LayoutTransition. DISAPPEARING;

LayoutTransition. CHANGE_APPEARING;

LayoutTransition. CHANGE_DISAPPEARING;

The new view animation CHANGE_APPEARING of APPEARING changes the layout of the animation, so we can draw images based on the gourd.


 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() {@Overridepublic void onClick(View v) {count++;               Button button = new Button(AnimationActivity.this);               button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {                gridLayout.removeView(v);}});               button.setText("btn"+count);               gridLayout.addView(button);}});}
Effect


If you do not like the default animation effect, you can also replace it with your favorite effect.


mAdbtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {count++;Button button = new Button(AnimationActivity.this);button.setOnClickListener(new OnClickListener() {@Overridepublic 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. Here is the introduction of property animation. I wish you all a good job.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.