效果图:

代码:

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.TextView; /**
* Created by pengkv on 16/1/11.
*/
public class ExpandTextView extends TextView implements View.OnClickListener { private int mMaxCount;//记录文本框的最大行数
private boolean isSinleLine = true;//是否是单行显示
private boolean isFitst = true;//是否是第一次測量 public ExpandTextView(Context context) {
super(context);
setOnClickListener(this);
} public ExpandTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setOnClickListener(this);
} public ExpandTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnClickListener(this);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (isFitst) {//假设是第一次測量,记录最大行数。測量后设置成单行
mMaxCount = getLineCount();
setEllipsize(TextUtils.TruncateAt.END);
setSingleLine();
isFitst = false;
}
} @Override
public void onClick(View v) { ValueAnimator animator;
final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();
if (isSinleLine) {//设置展开动画的位移
setSingleLine(false);
animator = ValueAnimator.ofInt(getLineHeight(), mMaxCount * getLineHeight());
} else {//设置收缩动画的位移
animator = ValueAnimator.ofInt(mMaxCount * getLineHeight(), getLineHeight());
}
//属性动画的使用方法
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
params.height = (int) animation.getAnimatedValue();
setLayoutParams(params);
}
});
animator.setInterpolator(new DecelerateInterpolator());
animator.setDuration(300);
animator.setTarget(this);
animator.start(); animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (!isSinleLine) {//在动画结束后设置成单行,避免效果异常
setSingleLine();
}
isSinleLine = !isSinleLine;
}
});
}
}

XML使用:

//注意:不要在xml的view.ExpandTextView里面定义singleLine、ellipsize属性,否则会异常
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/str1"
android:textSize="18sp" /> <view.ExpandTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/str2"
android:textColor="#edd206"
android:textSize="18sp" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/str3"
android:textSize="18sp" /> </LinearLayout>

Tip:

代码中涉及到的属性动画建议參考这里

优化:

//点击事件的处理能够换成以下这样的更简洁的方式。

    @Override
public void onClick(View v) { final ObjectAnimator animator;
if (isSinleLine) {
setSingleLine(false);
animator = ObjectAnimator.ofInt(this, "height", getLineHeight(), mMaxCount * getLineHeight());
} else {
animator = ObjectAnimator.ofInt(this, "height", mMaxCount * getLineHeight(), getLineHeight());
} animator.setDuration(300).start();
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (!isSinleLine) {//在动画结束后设置成单行,避免效果异常
setSingleLine();
}
isSinleLine = !isSinleLine;
}
});
}

缩放文本框ExpandTextView的更多相关文章

  1. JS案例 - 可自动伸缩高度的textarea文本框

    文本框的默认现象: textarea如果设置cols和rows来规定textarea的尺寸,那么textarea的默认宽高是这俩属性设置的值,可以通过鼠标拖拽缩放文本框的尺寸. textarea如果设 ...

  2. 使用 CSS 去掉 iPhone 网页上按钮的超大圆角以及文本框圆角默认样式

    使用 iPhone 上的浏览器去浏览网页的时候,按钮总是显示超大圆角且颜色由上而下渐变的样式,显得超级恶心,而且文本框也会有一定的圆角,但是我们自己定义 border-radius 也没有效果,经过搜 ...

  3. 根据窗体自动调整控件及文本框记住上次填写内容Demo

    第一次写文章,组词难免没有不通之处... 最近常用到Winform根据窗体大小自动调整空间大小及字体.文本框记住上次填写内容待下次输入某一段时候自动跳出上次输入内容.于是就随便把两个问题放到同一个de ...

  4. 禁止多行文本框textarea拖拽

    禁止多行文本框textarea拖拽: textarea { resize: none; } resize这个是用于元素缩放,它可以取以下几个值: none 默认值 both 允许水平方向及垂直方向缩放 ...

  5. (转)完美解决 Android WebView 文本框获取焦点后自动放大有关问题

    完美解决 Android WebView 文本框获取焦点后自动放大问题 前几天在写一个项目时,要求在项目中嵌入一个WebView 本来很快就完成了,测试也没有问题.但发给新加坡时,他们测试都会出现文本 ...

  6. 文本框控件JTextField和JTextArea的使用

    -----------------siwuxie095                             工程名:TestUI 包名:com.siwuxie095.ui 类名:TestTextF ...

  7. Tkinter--Text文本框样例

    #-*- coding:utf-8 -*- """ Text 文本框样例 实现功能有:Ctrl+a全选文本, 竖向滚动条,横向滚动条(不自动换行) 自动缩放 有谁知道全选 ...

  8. C#用户自定义控件(含源代码)-透明文本框

    using System; using System.Collections; using System.ComponentModel; using System.Drawing; using Sys ...

  9. delphi 可以自定义边框的文本框TSkinNormalEdit思路(QQ2011风格)

    需求: QQ我的资料中基本资料窗体中的文本框: 正常状态下,文本框只有一条看起来只有一个像素的边框,边框的颜色从上到下由深到浅的渐变,当鼠标定位到该文本框时,其边框会变粗,而且边框的颜色加亮显示 如下 ...

随机推荐

  1. CF37E Trial for Chief(最短路)

    题意 题意是给你一张 NMNMNM 的图,每个点有黑色和白色,初始全为白色,每次可以把一个相同颜色的连续区域染色,求最少的染色次数:(n,m<=50) 题解 转化为最短路.对于每一个点与它相邻的 ...

  2. Rman备份及不完全恢复操作

    最后更新时间:2018/12/18 启用归档 --检查是否为归档 SQL> archive log list; Database log mode              No Archive ...

  3. vmware下minimum安装centos后配置网络

    原文出处:http://www.cnblogs.com/lanhj/p/3785553.html 昨晚用vmware安装centos时选择minimum安装,目的熟悉基本配置. 上来想yum一个vim ...

  4. Ubuntu14.04下安装和&quot;激活&quot;Office2010ProPlus与Visio2010(15.11.20Updated)

    本人用Ubuntu的时候全然没有打游戏的欲望,故而能够更高效的工作. 尽管说LibreOffice.WPS等等有Ubuntu版本号,可是用着还是没有微软的Office顺手,故而折腾了一下怎样安装Off ...

  5. node.js操作Cookie

    node.js操作Cookie http://www.tuicool.com/articles/F3UF7n

  6. Gym - 100338E Numbers 贪心

    Gym - 100338E 题意:给你n,k问在1-n中能整出k的字典序最小的数.范围1018 思路:比较简单的贪心了,枚举10的幂m,然后加上k-m%k, 更新答案就可以了,数据一定要用unsign ...

  7. 关键字&标识符

    关键字:被Java语言赋予了特殊含义,用做专门用途的字符串(单词) 保留字: 标识符:凡是自己可以起名字的地方都叫标识符 命名的规则:(一定要遵守,不遵守就会报编译的错误)由26个英文字母大小写,0- ...

  8. 打包,VS 之 InstallShield Limited Edition for Visual Studio 2015 图文教程

    转载收藏于 https://www.cnblogs.com/xinaixia/p/5473815.html 从Visual Studio 2012开始,微软就把自家原来的安装与部署工具彻底废掉了,转而 ...

  9. python音频处理相关类库

    一.eyeD3 以下是eyed3的官方介绍 eyeD3 is a Python tool for working with audio files, specifically mp3 files co ...

  10. 如何形象的解释 webhook 这个词

    就是一个 callback,只不过 callback 的操作是发送指定的 HTTP request 给一个指定的地址. callback 就是由甲传给乙,乙处理完之后通知甲传过来的方法或者请求甲方的 ...