带动画的点击可展开TextView

来源:互联网 发布:眼睛很亮女孩子知乎 编辑:程序博客网 时间:2024/05/17 01:41

效果图:
收起(默认)效果:
这里写图片描述

点击展开后的效果:
这里写图片描述

源码:
布局:

<?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) {        }    }}
0 0