Home > Web Front-end > HTML Tutorial > Introduction to android animation Custom Animation animation_html/css_WEB-ITnose

Introduction to android animation Custom Animation animation_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:40:01
Original
1242 people have browsed it

Yesterday we introduced the basic usage of Animation. How do you guys understand? If you haven’t learned about Animation yet, you can read this blog

Introduction to android animation - Animation to achieve loading animation effect

Although the four built-in animations in Android can be combined using AnimationSet Operation, but these combinations are often not enough in specific situations. Just like view, we can customize animation effects.

The effect we want to create today is to imitate QQ’s shaking effect. As usual, let’s take a look at today’s renderings first:


Customized animation The implementation method is to inherit the Animation class and rewrite the applyTransformation() method

public class CustomAnimation extends Animation {    @Override    protected void applyTransformation(float interpolatedTime, Transformation t) {        t.getMatrix().setTranslate((float)Math.sin(interpolatedTime*50)*20,0);        super.applyTransformation(interpolatedTime, t);    }}
Copy after login

You can see that the applyTransformation() method has two parameters. The first parameter interpolatedTime represents a 0-1 gradually The increased float and t represent the changes required by the view, such as t.setAlpha(); t.setTranslate(); as the names suggest, these two methods are responsible for transparency changes and displacement changes.

So our goal today is to achieve a shaking effect. The idea is as follows: The view is first displaced to the left, and then to the right. The coordinates are (0-n,0) --->(0 n,n). The change of one positive and one negative requires the use of our trigonometric functions. So here t.setTranslate(Math.sin()).

Basically our custom animation is complete. Next, get the current layout under MainActivity and add animation effects to it

public class MainActivity extends ActionBarActivity {    private Button mButton;    private RelativeLayout mRelativeLayout;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mButton = (Button) findViewById(R.id.shake);        mRelativeLayout = (RelativeLayout) findViewById(R.id.relativelayout);        final CustomAnimation ca = new CustomAnimation();        ca.setDuration(1000);        mButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                mRelativeLayout.startAnimation(ca);            }        });    }}
Copy after login


That’s it! If you like my blog, please like it

Source code download

Copyright statement: This article is an original article by the blogger. You are welcome to reprint after indicating the source.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template