# # # MaterialDesign animation
MaterialDesign animation provides some standard animation effects, which are described below.
####Touch Feedback
5.0+ phones come with their own.
We do this by giving the control a background property value:
<Button android:id="@+id/btn_test" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="? attr/selectableItemBackground" android:background="? Attr/selectableItemBackgroundBorderless "android: text =" test "/ >Copy the code
SelectableItemBackground is a boundary of water ripple effect, there is no boundary selectableItemBackgroundBorderless water ripple effect.
You can change the background color and the color of the water ripple, and it’s better to use AppcompatActivity:
<item name="colorControlHighlight">@color/colorPrimary_pink</item>
<item name="colorButtonNormal">@color/material_blue_grey_800</item>
Copy the code
If you want to change the color of individual controls, you can do this by nesting a layer of layout on the outside.
####Reveal Effect
Example: The effect of exposing an Activity. The ViewAnimationUtil utility class is used to implement:
. / / circular water ripples reveal ViewAnimationUtils createCircularReveal (view, / / function in which the view above centerX, centerY, / / startRadius the center of the spread, EndRadius)// endRadiusCopy the code
Where, the radius of diffusion is calculated by Pythagorean theorem, for example:
(float) Math.hypot(view.getWidth() / 2, view.getHeight() / 2)
Copy the code
Examples are as follows:
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); view_root = (LinearLayoutCompat) findViewById(R.id.llc_test); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Animator animator = ViewAnimationUtils.createCircularReveal(view_root, view_root.getWidth() / 2, view_root.getHeight() / 2, 0f, (float) Math.hypot(view_root.getWidth() / 2, view_root.getHeight() / 2)); animator.setDuration(1000); animator.setInterpolator(new AccelerateInterpolator()); animator.start(); }}Copy the code
Because the animation is attached to the window and is not initialized when it is called in the Activity onCreate method, you need to perform the animation in onWindowFocusChanged.
####Activity Transition (Activity transition animation effect)
Concept: Animate transitions between two activities when they jump. We are through the overridePendingTransition method before.
Use the ActivityOptions class primarily. Only versions later than API21 are supported. It can be tricky to determine the version, but Google has designed a compatible class: ActivityOptionsCompat (in the V4 package), but this class has no transition animation on the lower version, just solved the problem of manually determining the version.
##### Use transition animation prerequisite: You need to set both activities to allow transition animation.
GetWindow ().requestFeature(window.feature_content_transitions); / / method 2: modify the theme: < item name = "android: windowContentTransitions" > true < / item >Copy the code
Transitions can be divided into two main categories: shared element transitions and common transitions.
##### 1. Conversion of shared elements
Single element:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(MDAnimActivity.this, iv_test, "test");
Intent intent = new Intent(MDAnimActivity.this, MDAnimSceneTransitionActivity.class);
startActivity(intent, options.toBundle());
}
Copy the code
Converting multiple elements simultaneously:
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat
.makeSceneTransitionAnimation(this, Pair.create((View)iv1, "iv1"),Pair.create((View)bt, "bt"));
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent, optionsCompat.toBundle());
Copy the code
This is done automatically when the page returns. See the FragmentActivity onBackPressed method:
@Override
public void onBackPressed() {
if (!mFragments.getSupportFragmentManager().popBackStackImmediate()) {
super.onBackPressed();
}
}
Copy the code
##### 2. Conversion of non-shared elements
Only API 21 has the following built-in effects, so you need to determine the version number when using them.
There are three systems: Slide, Explode, Fade and hide. The following is an example of the Fade:
Fade = new Fade(); fade.setDuration(1000); getWindow().setExitTransition(fade); // getWindow().setenterTransition (fade); // If there is a shared element, you can set the shared element, and it will be executed as the shared element animation, and the other child views will be executed as the Fade animation. ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this); Intent intent = new Intent(this, SecondActivity.class); startActivity(intent, optionsCompat.toBundle());Copy the code
####Curved motion
Design: View translation rotation effects combined with Path, Interpolator Interpolator.
####View State change
Example: Pressing a control such as FloatingActionButton gives a Z-axis shadow deepening effect.
If you feel that my words are helpful to you, welcome to pay attention to my public number:
My group welcomes everyone to come in and discuss all kinds of technical and non-technical topics. If you are interested, please add my wechat huannan88 and I will take you into our group.