zoukankan      html  css  js  c++  java
  • 缩放文本框ExpandTextView

    效果图:

    这里写图片描写叙述

    代码:

    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;
                }
            });
        }
  • 相关阅读:
    手把手教你用jQuery Mobile做相册
    一篇文章教会你用Python爬取淘宝评论数据(写在记事本)
    [C#] (原创)一步一步教你自定义控件——01,TrackBar
    性能测试系列(4)-进程和线程的区别
    熊海CMS_1.0 代码审计
    Web Security Academy ___XXE injection___Lab
    关于路径存储的常见优化——前向星与链式前向星
    2020.7.27考试D1T2:方块消除(Block)
    2020.7.27考试D1T1:Cow Pie Treasures
    【DP水题】P4823 [TJOI2013]拯救小矮人
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6992098.html
Copyright © 2011-2022 走看看