效果图:

代码:

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. 【Educational Codeforces Round 37 E】Connected Components?

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] bfs. 用一个链表来记录哪些点已经确定在某一个联通快里了. 一开始每个点都能用. 然后从第一个点开始进行bfs. 然后对于它的所有 ...

  2. ECNUOJ 2619 询问

    询问 Time Limit:2000MS Memory Limit:65536KBTotal Submit:286 Accepted:70 Description  Pollux最近对字符串匹配很感兴 ...

  3. 以Append方式打开文件,设置偏移量无效

    #include<stdio.h> int main() { FILE * fd = fopen("btoo1.c", "ab+"); fpos_t ...

  4. CSS3 实现RSS图标

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>CSS3 实现RSS图标&l ...

  5. Windows系统时间同步出错解决办法(w32tm /register按回车,可能是为了解决时间COM注册的问题)

    有时候我们设置本地时间与Internet时间同步时,经常连接服务器time.windows.com超时,导致时间同步失败,解决办法如下: 利用快捷键"Win+R"调出运行框,输入: ...

  6. 在IIS中给某一个网站添加binding的坑

    以http为例, 假如ip地址从局域网的地址172.31.212.20改为127.0.0.1 但是网站没法访问了http://172.31.212.20/chile.backoffice/  必须通过 ...

  7. legend---三、方法集思路

    legend---三.方法集思路 一.总结 一句话总结:其实也就是工具包思路,会极大的简化编程,清晰逻辑 1.多if转换成简洁单if怎么实现? 下面这段代码是错的,if的这种写法只适合直接return ...

  8. poj--1459--Power Network(最大流,超级源超级汇)

    Power Network Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit ...

  9. 学习 shell —— 编写基本脚本

    set:查看环境变量: 0. 简单说明 一般而言,shell 会通过 PATH 变量来查找命令,如果要执行用户编写的脚本(未添加进 PATH 路径),还需两步操作: 需要 $ ./xx 为需执行该脚本 ...

  10. Vsftp权限控制(持续增加中)

    把用户限制在自己的home目录中,例如限制用户Leon只能访问/home/Leon目录下的文件,不允许访问上级目录. 先打开配置文件 vi /etc/vsftpd/vsftpd.conf 第一种方法: ...