Android-use Animation. RELATIVE_TO_SELF to make personalized animations

Source: Internet
Author: User

Android-use Animation. RELATIVE_TO_SELF to make personalized animations

In Android development, Animation is used to make effects for controls. Most controls can use this class. This class contains four basic actions: Moving, rotating, fading, and scaling.

There are two ways to use Animation:

Method 1: Create, set, and start an animation in the Code (move TranslateAnimation/rotate RotateAnimation/fade in and out AlphaAnimation/scale ScaleAnimation). This advantage is that you can easily debug the program effect;

Method 2: Set the control attributes in xml. The advantage is that the Code is highly reusable and the disadvantage is that debugging is not convenient.

The following is an example of how to create code:

Before the example is started, we will popularize two very important reference standards: Animation. RELATIVE_TO_SELF (relative to itself) and Animation. RELATIVE_TO_PARENT (relative to parent control (container )).

1. The imageView control changes from completely transparent to completely opaque. The duration is 0.2 s;

private void toVisibleAnim(View view){AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);alphaAnimation.setDuration(200);view.startAnimation(alphaAnimation);}

2. The imageView control scales from the original size to the center of its own size to 0 and lasts for 0.2 s;

private void toHideAnim(View view){ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);scaleAnimation.setDuration(200);view.startAnimation(scaleAnimation);}

3. The imageView control rotates 90 degrees in the center of itself and lasts for 0.2 s;

private void rotateAnim(View view){view.setVisibility(View.VISIBLE);RotateAnimation rotateAnimation = new RotateAnimation(0, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);rotateAnimation.setDuration(200);view.startAnimation(rotateAnimation);}

4. The imageView control slides its width horizontally to the left from the rightmost end of its position for 0.2 s;

private void showScrollAnim(View view) {view.setVisibility(View.VISIBLE);TranslateAnimation mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF,0.0f, Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f);mShowAction.setDuration(200);view.startAnimation(mShowAction);}

5. The image control slides horizontally to the right from the leftmost end of its position to hide the animation. The duration is 0.2 s.

private void hiddenScrollAnim(LinearLayout view) {view.setVisibility(View.GONE);TranslateAnimation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,1.0f, Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f);mHiddenAction.setDuration(200);view.startAnimation(mHiddenAction);}


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.