Android TextView Multi-text folding expansion effect _android

Source: Internet
Author: User
Tags gettext visibility

Recently do the project, the effect chart to use TextView folding, more than a certain number of lines, it will fold up, click can be expanded. On the Internet to find some effect, I have slightly modified. To share with netizens.
References: http://www.jb51.net/article/95544.htm

First: through multiple layout combinations
Approximate steps:
-Define layouts, vertical linear linearlayout layouts, TextView, and ImageView. Define the basic components in the layout.
-Sets the height of the TextView to the specified number of lines * Row height. The reason for not using maxline is that Maxline controls the number of lines that display text, and it is inconvenient to use animations to expand the entire contents. So the height of the TextView here is also due to the wrap_content.
-Add a Click event to the entire layout and bind the animation. Click, if TextView is not expanded to its actual height, imageview rotation, otherwise back to the specified number of rows * Row height, imageview rotation retract.
Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/"
 Android "xmlns:more=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools " Android:id= "@+id/activity_main" android:layout_width= "match_parent" android:layout_height= "Match_parent" Android : orientation= "Vertical" tools:context= "com.example.my.textviewdemotest.MainActivity" > <textview android:id= "@+id/textview1" android:layout_width= "match_parent" android:layout_height= "wrap_content" "android:textColor=" @ Android:color/black "android:textsize=" 18SP "> </TextView> <relativelayout android:layout_width=" Match_ Parent "android:layout_height=" wrap_content "> <textview android:id=" @+id/expand_text "android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:text=" more "android:textsize=" 18SP "android:visibility=" Gone "/> <imageview android:id=" @+id/expand_view1 "Android:layout"_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "true" Android: Paddingbottom= "5dip" android:paddingleft= "5dip" android:paddingright= "5dip" android:paddingtop= "5dip" android:src=
 "@drawable/ic_expand_more_red_700_24dp" android:visibility= "Gone"/> </RelativeLayout> <!--The second method of--> <com.example.my.textviewdemotest.textmoretextview android:layout_width= "Match_parent" android:layout_height= " Wrap_content "android:ellipsize=" End "more:maxline=" 2 "more:text=" @string/text "more:textcolor=" @android: color/

 Black "more:textsize=" 18dip "> </com.example.my.textviewdemotest.TextMoreTextView> </LinearLayout>

Core code:

Package com.example.my.textviewdemotest;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import android.view.animation.Animation;
Import android.view.animation.RotateAnimation;
Import android.view.animation.Transformation;
Import Android.widget.ImageView;

Import Android.widget.TextView;
 public class Mainactivity extends Appcompatactivity implements View.onclicklistener {TextView Text1;
 ImageView MImageView1;
 TextView Expandtext;

 Textmoretextview Text2; Boolean isexpand;//whether the state that has been expanded private int maxdescripline = 3; TextView default maximum number of display rows private int deltavalue;//The default height, which is the height private int startvalue;//The front determined by Maxline and the starting height private int Durationmil
 Lis = 350;//animation duration @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.activity_main);
 Text1 = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
 Text2= (Textmoretextview) Findviewbyid (R.id.text_textview); Expandtext = (TeXtview) Findviewbyid (R.id.expand_text);
 MImageView1 = (ImageView) Findviewbyid (R.ID.EXPAND_VIEW1);

 Mimageview1.setonclicklistener (this);
 Text1.settext (GetText (R.string.text));
 The second type can be directly set in the text//Text2.settext (GetText (R.string.text)); Here you can set the height of the text according to the actual situation, make a judgment (may be text only one line, will occupy Maxdescripline line) Text1.setheight (Text1.getlineheight () * Maxdescripline
 ); Text1.post (New Runnable () {@Override public void run () {mimageview1.setvisibility (Text1.getlinecount () > Maxdescri Pline?
 View.VISIBLE:View.GONE); Expandtext.setvisibility (Text1.getlinecount () > Maxdescripline?
 View.VISIBLE:View.GONE);
 }
 });
 @Override public void OnClick (View v) {switch (V.getid ()) {case R.id.expand_view1:zhedie (Text1, mImageView1);
 Break
 } private void Zhedie (final TextView text, ImageView imageview) {isexpand =!isexpand;
 Text.clearanimation ();
 Startvalue = Text.getheight (); if (isexpand) {/** * Folding animation * from the actual height retracted starting height * * * deltavalue = text.getlineheight () * Text.getliNecount ()-startvalue; rotateanimation animation = new Rotateanimation (0, 180, animation.relative_to_self, 0.5f, Animation.relative_to_self,
 0.5f);
 Animation.setduration (Durationmillis);
 Animation.setfillafter (TRUE);
 Imageview.startanimation (animation);
 Expandtext.settext ("Close Up");
 else {/** * Expand animation * from initial height to actual height * * deltavalue = text.getlineheight () * maxdescripline-startvalue; rotateanimation animation = new Rotateanimation (180, 0, Animation.relative_to_self, 0.5f, Animation.relative_to_self,
 0.5f);
 Animation.setduration (Durationmillis);
 Animation.setfillafter (TRUE);
 Imageview.startanimation (animation);
 Expandtext.settext ("more");  } Animation Animation = new Animation () {protected void applytransformation (float interpolatedtime, transformation t) {
 Displays the TextView height according to the percentage of the ImageView rotation animation to achieve animation effect text.setheight ((int) (Startvalue + deltavalue * interpolatedtime));
 }
 };
 Animation.setduration (Durationmillis);
 Text.startanimation (animation);

 }
}

The second method, if used in more places can save a lot of redundant code: the specific steps are not directly analyzed.
Core code:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
 <declare-styleable name= "Moretextstyle" >
 <attr name= "textsize" format= "Dimension"/> <attr name= "
 textcolor" format= "Color"/>
 <attr name= "Maxline" format= "integer"/> <attr name=
 "text" format= "string"/>
 </ Declare-styleable>
</resources>
Package com.example.my.textviewdemotest;
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.graphics.Color;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import Android.util.TypedValue;
Import android.view.Gravity;
Import Android.view.View;
Import Android.view.ViewTreeObserver;
Import android.view.animation.Animation;
Import android.view.animation.RotateAnimation;
Import android.view.animation.Transformation;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;

Import Android.widget.TextView;
 public class Textmoretextview extends LinearLayout {protected TextView contentview;

 protected ImageView Expandview;
 protected int textcolor;
 protected float textsize;
 protected int maxline;

 protected String text; public int defaulttextcolor = color.black;//default text color public int defaulttextsize = 12; Default text size public int defaultline = 3; Default number of rows public textmoretextview (context, AttributeSet attrs) {Super (context, aTTRS);
 Initalize ();
 Initwithattrs (context, attrs);
 Bindlistener (); } protected void Initwithattrs (context context, AttributeSet attrs) {TypedArray a = Context.obtainstyledattributes (att
 RS, R.styleable.moretextstyle);
 int textcolor = A.getcolor (R.styleable.moretextstyle_textcolor, Defaulttextcolor);
 Textsize = A.getdimensionpixelsize (r.styleable.moretextstyle_textsize, defaulttextsize);
 Maxline = A.getint (R.styleable.moretextstyle_maxline, defaultline);
 Text = a.getstring (r.styleable.moretextstyle_text);
 Bindtextview (TextColor, TEXTSIZE, maxline, text);
 A.recycle ();
 } protected void Initalize () {setorientation (VERTICAL);
 Setgravity (Gravity.right);
 Contentview = new TextView (GetContext ());
 AddView (Contentview, layoutparams.match_parent, layoutparams.wrap_content);
 Expandview = new ImageView (GetContext ());
 int padding = dip2px (GetContext (), 5);
 expandview.setpadding (padding, padding, padding, padding); Expandview.setimageresource (r.drawable.ic_expand_more_reD_700_24DP);
 Layoutparams LLP = new Layoutparams (layoutparams.wrap_content, layoutparams.wrap_content);
 AddView (Expandview, LLP);
 } protected void Bindtextview (int color, float size, final int line, String text) {contentview.settextcolor (color);
 Contentview.settextsize (typedvalue.complex_unit_px, size);
 Contentview.settext (text);
 Viewtreeobserver observer = Contentview.getviewtreeobserver (); Observer.addongloballayoutlistener (New Viewtreeobserver.ongloballayoutlistener () {// Judge write in this method can get Contentview.getlinecount (), otherwise return 0; @Override public void Ongloballayout () {Viewtreeobserver Obs = content
 View.getviewtreeobserver ();
 Obs.removeglobalonlayoutlistener (this); if (Contentview.getlinecount () < line) {Contentview.setheight (Contentview.getlineheight ()) *
 Contentview.getlinecount ());
  else {contentview.setheight (contentview.getlineheight () * line);
 Bindlistener ()///Only executes this method if the number of rows is greater than the set line number, or there will be bugs if you make an adjustment. }//log.e ("AAA", "bindTextView111:" + contentview.getlinecount ();//Return 0, why}}); Post (new Runnable () {@Override public void run () {expandview.setvisibility (Contentview.getlinecount () > line?
 View.VISIBLE:View.GONE);
 LOG.E ("AAA", "Run:" +contentview.getlinecount ());
 }
 });

 } protected void Bindlistener () {Setonclicklistener (new Onclicklistener () {Boolean isexpand;
 @Override public void OnClick (View v) {isexpand =!isexpand;
 Contentview.clearanimation ();
 final int deltavalue;
 Final int startvalue = Contentview.getheight ();
 int durationmillis = 350;
  if (isexpand) {deltavalue = Contentview.getlineheight () * Contentview.getlinecount ()-startvalue; rotateanimation animation = new Rotateanimation (0, 180, animation.relative_to_self, 0.5f, Animation.relative_to_self,
  0.5f);
  Animation.setduration (Durationmillis);
  Animation.setfillafter (TRUE);
 Expandview.startanimation (animation);
  else {deltavalue = Contentview.getlineheight () * maxline-startvalue; rotateanimation animation = new Rotateanimation (180, 0, Animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);
  Animation.setduration (Durationmillis);
  Animation.setfillafter (TRUE);
 Expandview.startanimation (animation); } Animation Animation = new Animation () {protected void applytransformation (float interpolatedtime, transformation t)
  {contentview.setheight ((int) (Startvalue + deltavalue * interpolatedtime));
 }

 };
 Animation.setduration (Durationmillis);
 Contentview.startanimation (animation);
 }
 });
 Public TextView Gettextview () {return contentview;
 public void SetText (Charsequence charsequence) {contentview.settext (charsequence); public static int dip2px (context context, float Dipvalue) {final float scale = context.getresources (). Getdisplaymetr
 ICS (). Density;
 return (int) (Dipvalue * scale + 0.5f);

 }
}

This class is written in this way. The call is referenced directly in the layout file.

SOURCE Download: Http://xiazai.jb51.net/201610/yuanma/Androidtextview (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.

Related Article

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.