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.