效果图:

代码:

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. python web开发 框架 模板 MVC

    我是跟着廖雪峰老师学习的,对于我这样的纯小白来说,跟着他的网站学习,简直是被妈妈抱在怀里一样无忧无虑,这样的学习本来没有记录下来的必要,但是由于我的粗心大意,经常会出现一些错误,所以我决定把这些错误记 ...

  2. 今日SGU 5.28

    SGU 121 题意:给你一张图,问你每个顶点必须有黑白两条边(如果它的边数>=2),问你怎么染色,不行就输出no 收获:你会发现不行的情况只有一个单纯的奇数环的时候,反之我们交替染色即可 #i ...

  3. 用Google Chrome 浏览器打开Unity打包的WebGL

    方法一: 直接build and run 方法二: 步骤: 1.打开浏览器的属性 2.在目标的位置添加--allow-file-access-from-files, 注意--allow-file-ac ...

  4. LocalDateTime在spring boot中的格式化配置

    在项目中日期格式化是最常见的问题,之前涉及的 java.util.Date 和 java.util.Calendar 类易用性差,不支持时区,非线程安全,对日期的计算方式繁琐,而且容易出错,因为月份是 ...

  5. cacti1.1安装报错

    安装过程中出现以下报错: ERROR: Your MySQL TimeZone database is not populated. Please populate this database bef ...

  6. django-xadmin使用之配置页眉页脚

    环境:xadmin-for-python3 python3.5.2 django1.9.12 在模块的adminx.py文件中增加以下代码即可: # TIP 定义页头页脚 class GlobalSe ...

  7. [Poi] Use Markdown as React Components by Adding a Webpack Loader to Poi

    Poi ships with many webpack loaders included, but you may run into scenarios where you'll need to cu ...

  8. 书剑恩仇录online全套源代码(服务端+client+文档)

    书剑恩仇录online全套源代码(服务端+client+文档).vc++开发,解压后将近10G大小,眼下网上最完整版本号,包括client源代码.服务端源代码.工具源代码.sdk.文档-- <书 ...

  9. Linux企业应用--RHAS 2.1 下安装中文 Lotus Domino R 6.5 图解

    原文请到ftp.jms165.com下载,是用上传用户                                                 (RHAS3+ksnapshot+OperOff ...

  10. Debian9.5 配置x11vnc远程桌面

    x11vnc是一个VNC服务器,它允许用户远程查看并用任何VNC查看器与真实的X显示器(即与物理监视器,键盘和鼠标相对应的显示器)进行交互.虽然它的原作者Karl Runge不再开发,但LibVNC和 ...