1. Tween animation
The demo address:
Github.com/chaozhouzha…
1.1, scaling
XML realize
/** * <scale xmlns:android="http://schemas.android.com/apk/res/android" * android:duration="1000" * Android :fillAfter="true" * Android :fillBefore="false" * Android :fillEnabled="false" * Android :fromXScale="1.5" * Android :fromYScale="1.5" * Android :interpolator="@android:anim/accelerate_interpolator" * Android :pivotX="50%" * Android :pivotY="50%" * Android :repeatCount="infinite" * Android :repeatMode="reverse" * Android :toXScale="0.5" * Android :toYScale="0.5"> * </scale> * * Duration of completion of an animation, in milliseconds * after the animation ends, stay in the end state * after the animation ends, return to the state before the animation ends * After the animation ends, Return to the state before the beginning * The scale of space on the X-axis relative to itself at the beginning of the animation * The scale of space on the Y-axis relative to itself at the beginning of the animation * Interpolator: Specifies the animation effect * The scale starting point X-axis coordinates: starting from the upper left corner of the current control plus: Value, percent (percent of its own width), percent P (percent of the parent control width) * Scaling start point Y-axis coordinates: Starting from the upper left corner of the current control plus: Value, percent (percent of its own height), percent P (percent of the parent control height) * Number of repeats: Infinite = infinite * repetition mode: reverse = playback in reverse; Restart replacename, And must be used with repeatCount * the size of space at the end of Animation relative to itself in X * the size of space at the end of Animation relative to itself in Y * */ private void xmlAnimation() {Animation Animation = AnimationUtils.loadAnimation(this, R.anim.scale_test); mIvTest.startAnimation(animation); }Copy the code
Code implementation
private void codeAnimation() { ScaleAnimation scaleAnimation = new ScaleAnimation (1.5 f to 0.5 f to 1.5 f to 0.5 f, Animation. RELATIVE_TO_SELF, 0.5 f, Animation. RELATIVE_TO_SELF, 0.5 f); scaleAnimation.setDuration(1000); scaleAnimation.setFillAfter(true); scaleAnimation.setFillBefore(false); scaleAnimation.setFillEnabled(false); scaleAnimation.setRepeatCount(Animation.INFINITE); scaleAnimation.setRepeatMode(Animation.REVERSE); scaleAnimation.setInterpolator(new AccelerateInterpolator()); mIvTest.startAnimation(scaleAnimation); }Copy the code
1.2. Opacity
XML realize
/ * * * (alpha XMLNS: android = "http://schemas.android.com/apk/res/android" * android: fromAlpha = "1.0" * Android :duration="1000" * Android :repeatMode="reverse" * Android :repeatCount="infinite" * Android :toAlpha="0.1"> * </alpha> * Transparency at the beginning of animation: 0-1 * Transparency at the end of animation: 0-1 * */ private void xmlAnimation() { Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_test); mIvTest.startAnimation(animation); }Copy the code
Code implementation
Private void codeAnimation() {AlphaAnimation AlphaAnimation = new AlphaAnimation(1.0f,0.1f); private void codeAnimation() {AlphaAnimation AlphaAnimation = new AlphaAnimation(1.0f,0.1f); alphaAnimation.setDuration(1000); alphaAnimation.setRepeatCount(Animation.INFINITE); alphaAnimation.setRepeatMode(Animation.REVERSE); mIvTest.startAnimation(alphaAnimation); }Copy the code
Displacement of 1.3,
XML realize
/** * <translate xmlns:android="http://schemas.android.com/apk/res/android" * android:fromXDelta="10%" * android:fromYDelta="10%" * android:toXDelta="100%" * android:toYDelta="100%" * android:duration="1000" * Android :repeatCount="infinite" * Android :repeatMode="reverse" * > * </translate> * Start point X axis * start point Y axis * End point X axis * end point Y axis * */ private void xmlAnimation() { Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_test); mIvTest.startAnimation(animation); }Copy the code
Code implementation
private void codeAnimation() { TranslateAnimation translateAnimation = new RELATIVE_TO_SELF, 1.0f, animation.relative_to_self, animation.relative_to_self, 0.1 f, Animation. RELATIVE_TO_SELF, 1.0 f); translateAnimation.setDuration(1000); translateAnimation.setRepeatCount(Animation.INFINITE); translateAnimation.setRepeatMode(Animation.REVERSE); mIvTest.startAnimation(translateAnimation); }Copy the code
1.4, rotation,
XML realize
/** * <rotate xmlns:android="http://schemas.android.com/apk/res/android" * android:fromDegrees="90" * android:pivotY="50%" * android:pivotX="50%" * android:drawable="@color/colorAccent" * android:visible="true" * android:duration="1000" * android:repeatCount="infinite" * android:repeatMode="reverse" * android:toDegrees="-270" * > * </rotate> * Angle position at the beginning of animation rotation: positive number clockwise, negative number anticlockwise * Angle position at the end of animation rotation: Positive numbers are clockwise, Negative counterclockwise * * / private void xmlAnimation () {Animation Animation. = AnimationUtils loadAnimation (this, state Richard armitage nim. Rotate_test); mIvTest.startAnimation(animation); }Copy the code
Code implementation
private void codeAnimation(){ RotateAnimation rotateAnimation = new RotateAnimation (90-270, Animation. RELATIVE_TO_SELF, 0.5 f, Animation. RELATIVE_TO_SELF, 0.5 f); rotateAnimation.setDuration(1000); rotateAnimation.setRepeatCount(Animation.INFINITE); rotateAnimation.setRepeatMode(Animation.REVERSE); mIvTest.startAnimation(rotateAnimation); }Copy the code
1.5, collections,
XML realize
/** * <set xmlns:android="http://schemas.android.com/apk/res/android" * android:duration="1000" * Android :fillAfter="true" * Android :fillBefore="false" * android:fillEnabled="false"> * <scale * Android :fromXScale="1.5" * Android :fromYScale="1.5" * Android :interpolator="@android:anim/accelerate_interpolator" * Android :pivotX="50%" * Android :pivotY="50%" * Android :repeatCount="infinite" * Android :repeatMode="reverse" * Android :toXScale="0.5" * Android :toYScale="0.5"> * </scale> * <alpha * Android :fromAlpha="1.0" * Android :repeatMode="reverse" * Android :repeatCount="infinite" * Android :toAlpha="0.1"> * </alpha> * <rotate * Android :fromDegrees="90" * android:pivotY="50%" * android:pivotX="50%" * android:drawable="@color/colorAccent" * android:visible="true" * android:repeatCount="infinite" * android:repeatMode="reverse" * android:toDegrees="-270"> * </rotate> * <translate * android:fromXDelta="10%" * android:fromYDelta="10%" * android:toXDelta="100%" * android:toYDelta="100%" * RepeatCount ="infinite" * android:repeatMode="reverse"> * </translate> * </set> RepeatCount label void * * / private void xmlAnimation () {Animation Animation = AnimationUtils. LoadAnimation (this, R.anim.set_test); mIvTest.startAnimation(animation); }Copy the code
Code implementation
private void codeSetAnimation() { animationSet = new AnimationSet(true); animationSet.addAnimation(codeScaleAnimation()); animationSet.addAnimation(codeAlphaAnimation()); animationSet.addAnimation(codeRotateAnimation()); animationSet.addAnimation(codeTranslateAnimation()); animationSet.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation Animation) {/ / animation began to Toast. MakeText (SetAnimationActivity. This "animation began," Toast. LENGTH_SHORT), show (); } @ Override public void onAnimationEnd (Animation Animation) {/ / Animation over Toast. MakeText (SetAnimationActivity. This, "end of the Animation," Toast.LENGTH_SHORT).show(); } @ Override public void onAnimationRepeat Animation (Animation) {/ / animations repeat. Toast makeText (SetAnimationActivity. This, "Repeat animation ", toast.length_short).show(); }}); mIvTest.startAnimation(animationSet); }Copy the code
2. Frame by frame animation
The demo address:
Github.com/chaozhouzha…
XML realize
<? The XML version = "1.0" encoding = "utf-8"? > <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@mipmap/loading_01" android:duration="50"></item> <item android:drawable="@mipmap/loading_02" android:duration="50"></item> <item android:drawable="@mipmap/loading_03" android:duration="50"></item> <item android:drawable="@mipmap/loading_04" android:duration="50"></item> <item android:drawable="@mipmap/loading_05" android:duration="50"></item> <item android:drawable="@mipmap/loading_06" android:duration="50"></item> <item android:drawable="@mipmap/loading_07" android:duration="50"></item> <item android:drawable="@mipmap/loading_08" android:duration="50"></item> <item android:drawable="@mipmap/loading_09" android:duration="50"></item> <item android:drawable="@mipmap/loading_010" android:duration="50"></item> </animation-list>Copy the code
private void xmlAnimation() {
AnimationDrawable animationDrawable = (AnimationDrawable) mIvLoading.getDrawable();
animationDrawable.start();
}
Copy the code
Code implementation
Public void codeAnimation() {int num = 10; AnimationDrawable animationDrawable = new AnimationDrawable(); for (int i = 1; i <= num; i++) { int id = getResources().getIdentifier("loading_0" + i, "mipmap", getPackageName()); Drawable drawable = getResources().getDrawable(id); animationDrawable.addFrame(drawable, 50); } animationDrawable.setOneShot(false); mIvLoadingCode.setBackgroundDrawable(animationDrawable); animationDrawable.start(); }Copy the code
3. Interpolators
The demo address:
Github.com/chaozhouzha…
TranslateAnimation translateAnimation = codeAnimation();
switch (v.getId()) {
case R.id.btn_accelerate_decelerate:
translateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
break;
case R.id.btn_accelerate:
translateAnimation.setInterpolator(new AccelerateInterpolator());
break;
case R.id.btn_decelerate:
translateAnimation.setInterpolator(new DecelerateInterpolator());
break;
case R.id.btn_linear:
translateAnimation.setInterpolator(new LinearInterpolator());
break;
case R.id.btn_bounce:
translateAnimation.setInterpolator(new BounceInterpolator());
break;
case R.id.btn_overshoot:
translateAnimation.setInterpolator(new OvershootInterpolator());
break;
case R.id.btn_anticipate:
translateAnimation.setInterpolator(new AnticipateInterpolator());
break;
case R.id.btn_anticipate_overshoot:
translateAnimation.setInterpolator(new AnticipateOvershootInterpolator());
break;
case R.id.btn_cycle:
translateAnimation.setInterpolator(new CycleInterpolator(1));
break;
default:
break;
}
mIvTest.startAnimation(translateAnimation);
Copy the code
Welcome to the Android technology stack public number: