TextView的折叠展开(动画效果)

1 篇文章 0 订阅

activity_main.XML布局文件:

< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     xmlns:tools = "http://schemas.android.com/tools"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
     android:paddingLeft = "@dimen/activity_horizontal_margin"
     android:paddingRight = "@dimen/activity_horizontal_margin"
     android:paddingTop = "@dimen/activity_vertical_margin"
     tools:context = ".MainActivity" >
 
     < TextView
         android:id = "@+id/tv_1"
         android:text = "@string/hello_world"
         android:layout_centerHorizontal = "true"
         android:textSize = "30sp"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content" />
 
     < TextView
         android:id = "@+id/adjust_text"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_below = "@+id/tv_1"
         />
 
     < ImageView
         android:id = "@+id/turn_over_icon"
         android:layout_width = "fill_parent"
         android:layout_height = "wrap_content"
         android:layout_centerInParent = "true"
         android:layout_below = "@id/adjust_text"
         android:src = "@mipmap/text_ic_expand"
         android:visibility = "gone"
         />
 
</ RelativeLayout >
主要为了展示功能,布局方面简单为主,用了两个textview(helloword和长文本),imageview(箭头)

MainActivity文件:

package com.expandtextview;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 {
     private TextView mTextView;    //文本域
     private ImageView mImageView;   //翻转icon
     int maxLine= 5 ;   //TextView设置默认最大展示行数为5
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         mTextView= (TextView) findViewById(R.id.adjust_text);
         mImageView= (ImageView) findViewById(R.id.turn_over_icon);
         mTextView.setText(getText(R.string.content)); //设置文本内容
         mTextView.setHeight(mTextView.getLineHeight() * maxLine);   //设置默认显示高度
         //根据高度来控制是否展示翻转icon
         mTextView.post( new Runnable() {
             @Override
             public void run() {
                 mImageView.setVisibility(mTextView.getLineCount() >= maxLine ? View.VISIBLE : View.GONE);
             }
         });
 
         mImageView.setOnClickListener( new MyTurnListener());   //翻转监听
 
     }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.menu_main, menu);
         return true ;
     }
 
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();
 
         //noinspection SimplifiableIfStatement
         if (id == R.id.action_settings) {
             return true ;
         }
 
         return super .onOptionsItemSelected(item);
     }
 
     private class MyTurnListener  implements View.OnClickListener {
 
         boolean isExpand;   //是否翻转
 
         @Override
         public void onClick(View v) {
             isExpand=!isExpand;
             mTextView.clearAnimation();   //清除动画
             final int tempHight;
             final int startHight=mTextView.getHeight();   //起始高度
             int durationMillis =  200 ;
 
             if (isExpand){
                 /**
                  * 折叠效果,从长文折叠成短文
                  */
 
                 tempHight = mTextView.getLineHeight() * mTextView.getLineCount() - startHight;   //为正值,长文减去短文的高度差
                 //翻转icon的180度旋转动画
                 RotateAnimation animation =  new RotateAnimation( 0 180 , Animation.RELATIVE_TO_SELF,  0 .5f, Animation.RELATIVE_TO_SELF,  0 .5f);
                 animation.setDuration(durationMillis);
                 animation.setFillAfter( true );
                 mImageView.startAnimation(animation);
             } else {
                 /**
                  * 展开效果,从短文展开成长文
                  */
                 tempHight = mTextView.getLineHeight() * maxLine - startHight; //为负值,即短文减去长文的高度差
                 //翻转icon的180度旋转动画
                 RotateAnimation animation =  new RotateAnimation( 180 0 , Animation.RELATIVE_TO_SELF,  0 .5f, Animation.RELATIVE_TO_SELF,  0 .5f);
                 animation.setDuration(durationMillis);
                 animation.setFillAfter( true );
                 mImageView.startAnimation(animation);
             }
 
             Animation animation =  new Animation() {
                 //interpolatedTime 为当前动画帧对应的相对时间,值总在0-1之间
                 protected void applyTransformation( float interpolatedTime, Transformation t) {  //根据ImageView旋转动画的百分比来显示textview高度,达到动画效果
                     mTextView.setHeight(( int ) (startHight + tempHight * interpolatedTime)); //原始长度+高度差*(从0到1的渐变)即表现为动画效果
 
                 }
             };
             animation.setDuration(durationMillis);
             mTextView.startAnimation(animation);
 
         }
     }
}

这里在重要地方都做了注释讲解,非常直观。

其中mTextView的post方法这么做的原因在于,在OnCreate方法中定义设置的textView不会马上渲染并显示,所以textview的getLineCount()获取到的值一般都为零,因此使用post会在其绘制完成后来对mImageView进行显示控制。

参考stackoverflow 上的讲解how to use getlincount in textview

http://stackoverflow.com/questions/11467632/how-to-use-getlinecount-in-textview-android

出处:http://www.androidchina.net/3398.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值