Material Design Series of custom behavior support all view_android

Source: Internet
Author: User
Tags static class

This example for you to share the Android custom behavior support all view, for your reference, the specific content as follows

One, the realization of the effect chart

The fab in the lower right corner, the animations can be of course varied, can be placed anywhere in the interface, we just give an example here. However, the behavior provided in the V7 package can only be used by the Floatingactionbutton, so today we implement this behavior is to support all view, can be used in ImageView, Button, Layout, Any class that inherits view can be used.

Second, custom behavior and animation encapsulation

We know that behavior is a subclass of Coordinatorlayout, Ctrl + t view its implementation classes currently have the following:

1. Appbarlayout.behavior;
2. Appbarlayout.scrollingviewbehavior;
3. Floatingactionbutton.behavior;
4. Snackbar.behavior;
5. Bottomsheetbehaviro;
6. Swipedismissbehavior;
7. Headerbehavior;
8. Viewoffsetbehavior;
9. Headerscrollingviewbehavior;

1th, 7 is the abstract class, 8 is the package protection of the class, 9 is 8 of a subclass, we go back to say.

Appbarlayout.scrollingviewbehavior we often use, that is, we often use in Layout xml: app:layout_behavior= "@string/appbar_scrolling_ View_behavior ".

Snackbar.behavior is used for snackbar, this needless to say.

Floatingactionbutton.behavior, Bottomsheetbehaviro, Swipedismissbehavior in the article at the beginning of several links to the blog has been said very clearly, we can go back to look again.

Today is about customizing behavior support all view as FAB, So that is floatingactionbutton.behavior, but it only supports Floatingactionbutton, so today we have to inherit behavior to write Definebehavior. So the first step is to open the Floatingactionbutton.behavior source look.

Implement Basicbehavior

The first thing you have to know is that the base class is coordinatorlayout.behavior to support generics, see Floatingactionbutton.behavior after it is limited to reference it's view must be Floatingactionbutton just, then I Here we also learn it to inherit the OK.

We create a new class Basicbehavior, copy the Floatingactionbutton.behavior code, and change the generics into the following:

public class Basicbehavior<t extends view> extends coordinatorlayout.behavior<t>;

That is, as long as a reference to the implementation of the Basicbehavior class is a view can be, so the basicbehavior inside the copy of the code in the reference to the generic Floatingactionbutton of the place to view, Well, I think when I was a part-time worker, I found that there are several kinds of package guide:

Take a closer look, These classes under the Android.support.design.widget package, one want to be sure that these classes are package protected classes, so we create a new Android.support.design.widget package under our project to move the implementation basicbehavior To the new package, found the problem solved.

Project source code and basicbehavior full source download link, please at the beginning or end of the article to find.

Implementation and simplification of animation

(did not see before the objectivity of the blog must look back, there will be a different harvest.) )
In our previous series of blogs, we implemented the zoom animation of the view, especially when the view was hidden, the following code was used to record the view move out of the animation, because the view removal would be called by behavior while the interface was sliding, so it could not be repeated. Needs to be logged with a value:

The record view moves the animation out of the execution.
Private Boolean isoutexecute = false;

Private Viewpropertyanimatorlistener Outanimatorlistener = new Viewpropertyanimatorlistener () {
 @Override
 public void Onanimationstart (view view) {
 Isoutexecute = true;
 }

 @Override public
 void Onanimationend (view view) {
 view.setvisibility (view.gone);
 Isoutexecute = false;
 }

 @Override public
 void Onanimationcancel (view view) {
 Isoutexecute = false;
 }
};

To not write such a long paragraph in every call, we encapsulate this end code as a class, simplifying the following:

public static class Listeneranimatorendbuild {
 //record view move animation is finished.
 Private Boolean isoutexecute = false;

 Private Viewpropertyanimatorlistener Outanimatorlistener;

 Public Listeneranimatorendbuild () {
 Outanimatorlistener = new Viewpropertyanimatorlistener () {
 @Override Public
 void Onanimationstart (view view) {
 Isoutexecute = true;
 }
 @Override public
 void Onanimationend (view view) {
 view.setvisibility (view.gone);
 Isoutexecute = false;
 }

 @Override public
 void Onanimationcancel (view view) {
 Isoutexecute = false;}}
 ;
 }

 View moves the animation out of the execution. Public
 Boolean isfinish () {return
 !isoutexecute;
 }

 Return to Viewpropertyanimatorlistener. Public
 Viewpropertyanimatorlistener builds () {return
 Outanimatorlistener
 }
}

So when we use it, it's just two lines of code:

Listeneranimatorendbuild listeneranimatorendbuild = new Listeneranimatorendbuild ();

Determine whether the animation is finished:
listeneranimatorendbuild.isfinish ();

Inheriting Basicbehavior implementation Definebavior

As we've defined basicbehavior, here we just need to inherit basicbehavior to implement our animation logic:

public class Definebehavior extends basicbehavior<view> {private Listeneranimatorendbuild listeneranimatorendbu

 Ild
 Public Definebehavior (context, AttributeSet attrs) {Super (context, attrs);
 Listeneranimatorendbuild = new Listeneranimatorendbuild ();  @Override public boolean onstartnestedscroll (Coordinatorlayout coordinatorlayout, view child, view Directtargetchild,
 View target, int nestedscrollaxes) {return nestedscrollaxes = = viewcompat.scroll_axis_vertical; @Override public void Onnestedscroll (Coordinatorlayout coordinatorlayout, view child, view target, int dxconsumed, T dyconsumed, int dxunconsumed, int dyunconsumed) {//if (dyconsumed > 0 && dyunconsumed = 0) {//System.out . println ("On the slide ...")
"); }//if (dyconsumed = 0 && dyunconsumed > 0) {//System.out.println ("to the edge of the slip ...
"); }//if (dyconsumed < 0 && dyunconsumed = = 0) {//System.out.println ("down) ...
"); }//if (dyconsumed = 0 && DyuncoNsumed < 0) {//System.out.println ("to the border, still slipping ...")
");
 //Here you can write other logical animations, here just for example to write a zoom animation. if ((dyconsumed > 0 | | dyunconsumed > 0) && listeneranimatorendbuild.isfinish () && Child.getvisibil
 ity () = = view.visible) {//Downhill scalehide (Child, Listeneranimatorendbuild.build ()); else if ((dyconsumed < 0 | | dyunconsumed < 0) && child.getvisibility ()!= view.visible) {scaleshow (child
 , null);

 }
 }
}

You may be surprised, haha, don't be surprised, the encapsulation of a long time is so simple to achieve all the view support.

Third, how to use

As with the behavior provided by Google, referencing the full package name can be:

App:layout_behavior= "Com.yanzhenjie.definebehavior.behavior.DefineBehavior"

In order to be as simple as the behavior that Google provides, we can define this string in String.xml:

<string name= "Define_behavior" >com.yanzhenjie.definebehavior.behavior.DefineBehavior</string>

Time to use:

App:layout_behavior= "@string/define_behavior"

Now we replace the Floatingactionbutton in the original project with ImageView:

<imageview
 android:id= "@+id/fab"
 android:layout_width= "Wrap_content"
 Wrap_content "
 android:layout_gravity=" bottom|end "
 android:layout_margin=" 16DP "
 android:src=" @ Mipmap/ic_launcher "
 app:layout_behavior=" @string/define_behavior "
 app:layout_scrollflags=" scroll| Enteralways|snap "/>

OK, OK, the concrete effect everybody downloads the source code: Http://xiazai.jb51.net/201609/yuanma/AndroidDefineBehavior (jb51.net). rar

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.