Android实现带动画效果的可点击展开TextView
作者:u014620028 时间:2023-08-06 05:42:08
本文为大家分享了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
0
投稿
猜你喜欢
Android应用程序模型之应用程序,任务,进程,线程分析
2021-10-09 02:17:45
java实现单链表倒转的方法
2023-04-04 04:44:01
Java多线程ThreadAPI详细介绍
2023-04-14 12:19:18
Maven的使用之继承与聚合
2023-08-14 21:47:14
浅谈java中Math.random()与java.util.random()的区别
2023-11-26 16:37:16
分析Java中的类加载问题
2023-09-03 19:37:04
Java+MySQL实现学生信息管理系统源码
2023-11-28 04:29:31
java数学工具类Math详解(round方法)
2022-09-10 17:01:00
Java class文件格式之访问标志信息_动力节点Java学院整理
2022-10-31 18:57:29
Spring Security权限想要细化到按钮实现示例
2022-10-14 20:52:38
SpringBoot深入探究四种静态资源访问的方式
2021-11-30 02:42:46
C#通过反射创建自定义泛型
2022-12-30 07:12:38
MyBatis-plus实现逆向生成器
2021-08-26 16:37:14
Java利用TreeUtils工具类实现列表转树
2021-10-02 03:28:00
Android开发之imageView图片按比例缩放的实现方法
2023-01-09 21:52:51
轻量级声明式的Http库——Feign的独立使用
2022-06-05 04:36:19
解决idea web 配置相对路径问题
2022-01-12 06:48:40
C#回收机制之资源回收托管
2022-03-04 13:18:20
详解Java枚举为什么是单例模式的最佳选择
2022-07-16 20:42:47
大白话讲解C# 中的委托
2023-02-03 02:06:41