温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android如何实现带动画效果的可点击展开TextView

发布时间:2021-04-16 11:30:22 来源:亿速云 阅读:413 作者:小新 栏目:移动开发

这篇文章将为大家详细讲解有关Android如何实现带动画效果的可点击展开TextView,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

效果图:

收起(默认)效果:

Android如何实现带动画效果的可点击展开TextView

点击展开后的效果:

Android如何实现带动画效果的可点击展开TextView

源码:

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 android:id="@+id/activity_main"
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 >

 <ScrollView
  android:id="@+id/sv"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >

  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#f6f6f6"
   android:orientation="vertical"
   android:padding="5dp">

   <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:maxLines="1"
    android:text="简介"
    android:textColor="#000000"
    android:textSize="20sp"/>

   <TextView
    android:id="@+id/tv_des"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#666666"
    android:textSize="18sp"/>

   <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <ImageView
     android:id="@+id/iv_des_arrow"
     android:layout_width="20dp"
     android:layout_height="20dp"
     android:layout_alignParentEnd="true"
     android:background="@mipmap/arrow_down"/>
   </RelativeLayout>

  </LinearLayout>

 </ScrollView>
</LinearLayout>

功能实现:

package com.cnfol.demo;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener {

 private TextView tv_des;

 private ImageView iv_des_arrow;

 private boolean isExpandDes = false;//是否展开整个描述

 private int minHeight = 0;
 private int maxHeight = 0;

 private ScrollView scrollView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  scrollView = (ScrollView) findViewById(R.id.sv);

  tv_des = (TextView) findViewById(R.id.tv_des);
  tv_des.setOnClickListener(this);

  iv_des_arrow = (ImageView) findViewById(R.id.iv_des_arrow);
  iv_des_arrow.setOnClickListener(this);

  String s = "中华人民共和国,简称中国,位于亚洲东部,太平洋西岸, 是工人阶级领导的、以工农联盟为基础的人民民主专政的社会主义国家。\n" +
    "\n" +
    "1949年(己丑年)10月1日成立, 以五星红旗为国旗, 《义勇军进行曲》为国歌, 国徽内容包括国旗、天安门、齿轮和麦稻穗, 首都北京, 省级行政区划为23个省、5个自治区、4个直辖市、2个特别行政区, 是一个以汉族为主体民族,由56个民族构成的统一多民族国家,汉族占总人口的91.51%。\n" +
    "\n" +
    "新中国成立后随即开展经济恢复与建设,1953年开始三大改造, 到1956年确立了社会主义制度,进入社会主义探索阶段。 文化大革命之后开始改革开放,逐步确立了中国特色社会主义制度。中国陆地面积约960万平方公里,大陆海岸线1.8万多千米,岛屿岸线1.4万多千米,内海和边海的水域面积约470多万平方千米。海域分布有大小岛屿7600多个,其中台湾岛最大,面积35798平方千米。同14国接壤,与8国海上相邻。中国是四大文明古国之一, 有着悠久的历史文化。是世界国土面积第三大的国家,世界第一大人口国家,与英、法、美、俄并为联合国安理会五大常任理事国。\n" +
    "\n" +
    "中国是世界第二大经济体,世界第一贸易大国,世界第一大外汇储备国, 世界第一大钢铁生产国和世界第一大农业国,世界第一大粮食总产量国以及世界上经济成长最快的国家之一。";
  tv_des.setText(s);
  tv_des.setMaxLines(3);

  tv_des.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
   @Override
   public void onGlobalLayout() {
    //一般用完之后,立即移除该监听
    tv_des.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    minHeight = tv_des.getMeasuredHeight();//获取3行时候的高度

    tv_des.setMaxLines(Integer.MAX_VALUE);//会全部显示内容
    tv_des.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
     @Override
     public void onGlobalLayout() {
      //一般用完之后,立即移除该监听
      tv_des.getViewTreeObserver().removeGlobalOnLayoutListener(this);
      maxHeight = tv_des.getMeasuredHeight();//获取总高度

      if (minHeight == maxHeight) {
       //最大高度和最小高度一样。说明设置的默认显示行数,已经可以把所有数据全部显示
       iv_des_arrow.setVisibility(View.GONE);
      }

      tv_des.getLayoutParams().height = minHeight;
      tv_des.requestLayout();//让tv_des显示为3行的高度
     }
    });
   }
  });

 }

 @Override
 public void onClick(View v) {

  switch (v.getId()) {

   case R.id.tv_des:
   case R.id.iv_des_arrow:
    ValueAnimator desAnimator = null;
    if (isExpandDes) {
     desAnimator = ValueAnimator.ofInt(maxHeight, minHeight);
    } else {
     desAnimator = ValueAnimator.ofInt(minHeight, maxHeight);
    }
    desAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
     @Override
     public void onAnimationUpdate(ValueAnimator animator) {
      int currentHeight = (Integer) animator.getAnimatedValue();
      tv_des.getLayoutParams().height = currentHeight;
      tv_des.requestLayout();

      //只有展开动画的时候才需要内容向上滚动,收缩动画的时候是不需要滚动的
      if (!isExpandDes) {
       int scrollY = currentHeight - minHeight;
       scrollView.scrollBy(0, scrollY);
      }
     }
    });
    desAnimator.setDuration(300);
    desAnimator.addListener(new DesAnimListener());
    desAnimator.start();
    break;

  }

 }

 /**
  * 描述区域动画的监听
  *
  * @author Administrator
  */
 class DesAnimListener implements Animator.AnimatorListener {
  @Override
  public void onAnimationCancel(Animator arg0) {
  }

  @Override
  public void onAnimationEnd(Animator arg0) {
   isExpandDes = !isExpandDes;
   iv_des_arrow.setBackgroundResource(isExpandDes ? R.mipmap.arrow_up : R.mipmap.arrow_down);
  }

  @Override
  public void onAnimationRepeat(Animator arg0) {
  }

  @Override
  public void onAnimationStart(Animator arg0) {
  }
 }

}

关于“Android如何实现带动画效果的可点击展开TextView”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI