Sometimes you need to click on a view to animate the expansion and contraction of a view so that you can customize the view directly.
In this example, the inherited framelayout is used to implement the custom Expandview. The following sections describe each part to implement the class and how to use the custom view.
As follows:
Effect not expanded:
Folding the effect up in the shrink:
The effect has been expanded:
Custom Unwind class: Expandview implementation:
PackageCom.czm.customview;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;Importandroid.view.animation.Animation;Importandroid.view.animation.AnimationUtils;Importandroid.widget.FrameLayout;Importandroid.widget.LinearLayout; Public classExpandviewextendsframelayout{PrivateAnimation mexpandanimation; PrivateAnimation mcollapseanimation; Private BooleanMisexpand; PublicExpandview (Context context) { This(Context,NULL); //TODO auto-generated Constructor stub } PublicExpandview (Context context, AttributeSet attrs) { This(Context, attrs,0); //TODO auto-generated Constructor stub } PublicExpandview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); //TODO auto-generated Constructor stubInitexpandview (); } Private voidInitexpandview () {Layoutinflater.from (GetContext ()). Inflate (R.layout.layout_expand, This,true); Mexpandanimation=animationutils.loadanimation (GetContext (), R.anim.expand); Mexpandanimation.setanimationlistener (NewAnimation.animationlistener () {@Override Public voidOnanimationstart (Animation Animation) {//TODO auto-generated Method Stub} @Override Public voidonanimationrepeat (Animation Animation) {//TODO auto-generated Method Stub} @Override Public voidonanimationend (Animation Animation) {setvisibility (view.visible); } }); Mcollapseanimation=animationutils.loadanimation (GetContext (), r.anim.collapse); Mcollapseanimation.setanimationlistener (NewAnimation.animationlistener () {@Override Public voidOnanimationstart (Animation Animation) {//TODO auto-generated Method Stub} @Override Public voidonanimationrepeat (Animation Animation) {//TODO auto-generated Method Stub} @Override Public voidonanimationend (Animation Animation) {setvisibility (view.invisible); } }); } Public voidcollapse () {if(Misexpand) {Misexpand=false; Clearanimation (); Startanimation (mcollapseanimation); } } Public voidexpand () {if(!Misexpand) {Misexpand=true; Clearanimation (); Startanimation (mexpandanimation); } } Public BooleanIsexpand () {returnMisexpand; } Public voidSetcontentview () {View View=NULL; View= Layoutinflater.from (GetContext ()). Inflate (R.layout.layout_expand,NULL); Removeallviews (); AddView (view); }}
Corresponding UI profile: Layout_expand.xml implementation:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"Android:background= "#63A90A"android:gravity= "Center_horizontal" > <TextView Android:id= "@+id/enterlesson"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Project List 1"android:textsize= "14SP"Android:singleline= "true"android:gravity= "Center"Android:textcolor= "#FFFFFF"android:drawablepadding= "10DP"Android:layout_marginbottom= "10DP"Android:layout_margintop= "10DP"/> <TextView Android:id= "@+id/enterlesson"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Project List 2"android:textsize= "14SP"Android:singleline= "true"android:gravity= "Center"Android:textcolor= "#FFFFFF"android:drawablepadding= "10DP"Android:layout_marginbottom= "10DP"Android:layout_margintop= "10DP"/> <TextView Android:id= "@+id/enterlesson"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Project List 3"android:textsize= "14SP"Android:singleline= "true"android:gravity= "Center"Android:textcolor= "#FFFFFF"android:drawablepadding= "10DP"Android:layout_marginbottom= "10DP"Android:layout_margintop= "10DP"/> <TextView Android:id= "@+id/enterlesson"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Project List 4"android:textsize= "14SP"Android:singleline= "true"android:gravity= "Center"Android:textcolor= "#FFFFFF"android:drawablepadding= "10DP"Android:layout_marginbottom= "10DP"Android:layout_margintop= "10DP"/> </LinearLayout>
To expand the animation code:
<?xml version= "1.0" encoding= "Utf-8"?>
<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<scale
android:duration= "200"
Android:fromxscale= "1."
Android:fromyscale= ". 0"
android:pivotx= "50%"
Android:pivoty= "0%"
Android:toxscale= "1."
Android:toyscale= "1."/>
</set>
Shrink Stack Code:
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <scale android:duration= " android:fromxscale" = "1." Android:fromyscale= "1." android:pivotx= "50%" android:pivoty= "0%" android:toxscale= "1." Android:toyscale= "0."/></set>
How do I use the above custom Expandview class? is divided into two steps:
(1) Define the view in the UI configuration file:
<linearlayout
android:id= "@+id/layout_title"
android:layout_width= "fill_parent"
Android:layout_ height= "50DP"
android:background= "#63A90A"
android:orientation= "horizontal"
android:gravity= "center"
<textview
android:id= "@+id/textview_title"
android:layout_width= "Wrap_content"
Android: layout_height= "Wrap_content"
android:textcolor= "#FFFFFF"
android:textsize= "22SP"
android:text= "click Down to expand "
/>
<imageview
android:id=" @+id/imageview_state "
android:layout_width=" 15DP "
Android: layout_height= "15DP"
android:layout_marginleft= "2DP"
android:src= "@drawable/expand"
/>
</ Linearlayout>
<com.czm.customview.ExpandView android:id= "@+id/expandview" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:background= "#00FF00" android:visibility= "Invisible" android:layout_below= "@+id/layout_title" Android:layout_marginbottom= "150DP" android:clickable= "true"/>
(1) Refer to the Expandview class in the Java class:
PrivateExpandview Mexpandview; PrivateLinearLayout mlinearlayout; PrivateTextView Mtextview; PrivateImageView Mimageview; Public voidInitexpandview () {mlinearlayout=(LinearLayout) Findviewbyid (r.id.layout_title); Mtextview=(TextView) Findviewbyid (r.id.textview_title); Mimageview=(ImageView) Findviewbyid (r.id.imageview_state); Mexpandview=(Expandview) Findviewbyid (R.id.expandview); Mexpandview.setcontentview (); Mlinearlayout.setclickable (true); Mlinearlayout.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub if(Mexpandview.isexpand ()) {mexpandview.collapse (); Mtextview.settext ("Click Down to expand"); Mimageview.setimagedrawable (Getresources (). getdrawable (R.drawable.expand)); }Else{Mexpandview.expand (); Mtextview.settext ("Click to fold Up"); Mimageview.setimagedrawable (Getresources (). getdrawable (R.drawable.collapse)); } } }); }