Home >Web Front-end >HTML Tutorial >Introduction to android animation Custom Animation animation_html/css_WEB-ITnose
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); }}
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); } }); }}
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.