效果图:

代码:

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. zabbix 使用自带模板监控mysql

    1.这里可以采用zabbix自带的mysql模版,但是也需要在mysql服务器上准备获取mysql status的脚本chk_mysql.sh,zabbix通过调用这个脚本来获取mysql的运行信息. ...

  2. echarts 初始化失败问题。

    dom 实例容器,一般是一个具有高宽的div元素. 注:如果div是隐藏的,ECharts 可能会获取不到div的高宽导致初始化失败,这时候可以明确指定div的style.width和style.he ...

  3. 20180929 北京大学 人工智能实践:Tensorflow笔记05

    (完)

  4. oracle基础入门(四)

    一:其实oracle的语法跟sql servce 挺像的只有一些个别的差异而已 1):安装Oracle的数据库一般它的表中会自带了两站表: 是 emp(员工表) , dept(部门) 单表查询 sel ...

  5. Spring学习总结(3)——Spring配置文件详解

    Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程序员必须学会并灵活应用这份"图纸&quo ...

  6. centos7 安装好python3 yum报错

    解决方法: 修改两个地方 vi /usr/bin/yum 将最前面的改为#! /usr/bin/python2 vi /usr/libexec/urlgrabber-ext-down #! /usr/ ...

  7. 【转载】jQuery弹出层始终垂直居中于当前屏幕

    一般网站上肯定有一些弹出框,不论弹出框的大小,都需要他在当前窗口垂直居中.之前手上就有一个jQuery的例子,后来才发现,他只能在第一屏垂直居中,如果滑动滚动条,弹出的框就在上方,不是很方便.请教朋友 ...

  8. Restricted Boltzmann Machines

    转自:http://deeplearning.net/tutorial/rbm.html http://blog.csdn.net/mytestmy/article/details/9150213 能 ...

  9. Coderfroces 862 C. Mahmoud and Ehab and the xor

    C. Mahmoud and Ehab and the xor Mahmoud and Ehab are on the third stage of their adventures now. As ...

  10. 系统管理员的 SELinux 指南:这个大问题的 42 个答案

    安全.坚固.遵从性.策略是末世中系统管理员的四骑士.除了我们的日常任务之外 —— 监控.备份.实施.调优.更新等等 —— 我们还需要负责我们的系统安全.即使这些系统是第三方提供商告诉我们该禁用增强安全 ...