Android自定義控件——ExpandTextView

本文介紹一個(gè)這樣?jì)饍旱腡extView,如圖:



點(diǎn)擊的時(shí)候TextView會(huì)伸縮,有種Clip的感覺,原理也很簡單,就是把兩個(gè)TextView重疊起來,一個(gè)現(xiàn)實(shí)固定的行數(shù)的文本,另一個(gè)現(xiàn)實(shí)全部的文本,初始化的時(shí)候,容器的高度等于固定文本的高度,完全展開時(shí)的高度等于文本顯示全的高度,過程用一個(gè)動(dòng)畫控制就可以了。下面貼代碼:

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by mingwei on 1/20/16.
 */
public class ExpandTextView extends RelativeLayout {

    private TextView mText;

    private TextView mExpandText;

    private int mTextColor = Color.GRAY;

    private int mTextLine = 1;

    private int mStart;

    private int mEnd;

    private boolean isFirst = true;

    private boolean isExpand = false;

    public ExpandTextView(Context context) {
        this(context, null);
    }

    public ExpandTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final ViewGroup.LayoutParams params = getLayoutParams();
        if (isFirst) {
            isFirst = false;
            mText.post(new Runnable() {
                @Override
                public void run() {
                    mStart = mText.getLineHeight() * mText.getLineCount();
                    params.height = mStart;
                    setLayoutParams(params);
                }
            });
            mExpandText.post(new Runnable() {
                @Override
                public void run() {
                    mEnd = mExpandText.getLineHeight() * mExpandText.getLineCount();
                }
            });

        }

    }

    public ExpandTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        mText = new TextView(context, attrs);
        mText.setTextColor(mTextColor);
        mText.setEllipsize(TextUtils.TruncateAt.END);
        mText.setMaxLines(mTextLine);
        addView(mText, params);
        mExpandText = new TextView(context);
        mExpandText.setTextColor(Color.TRANSPARENT);
        addView(mExpandText, params);
    }

    public void setText(String text) {
        isFirst = true;
        mText.setText(text);
        mExpandText.setText(text);
        requestLayout();
    }

    public void setTextColor(int color) {
        mTextColor = color;
        mText.setTextColor(color);
    }

    public void setTextSize(int size) {
        isFirst = true;
        mText.setTextSize(size);
        mExpandText.setTextSize(size);
        requestLayout();
    }

    public void setTextMaxLine(int num) {
        mTextLine = num;
        mText.setMaxLines(num);
    }

    public void setGravity(int gravity) {
        mText.setGravity(gravity);
        mExpandText.setGravity(gravity);
    }

    public void setEllipsize(TextUtils.TruncateAt ell) {
        mText.setEllipsize(ell);
    }

    public void setTextLineSpacingExtra(float spac) {
        mText.setLineSpacing(spac, 1.0f);
        mExpandText.setLineSpacing(spac, 1.0f);
    }

    public TextView text() {
        return mText;
    }

    public TextView expandText() {
        return mExpandText;
    }

    public int line() {
        return mTextLine;
    }

    public boolean isExpand() {
        return isExpand;
    }

    public void expand() {
        if (!isExpand) {
            isExpand = true;
            mText.setTextColor(Color.TRANSPARENT);
            mExpandText.setTextColor(mTextColor);
            Animation animation = new Animation() {
                @Override
                protected void applyTransformation(float interpolatedTime, Transformation t) {
                    ViewGroup.LayoutParams params = ExpandTextView.this.getLayoutParams();
                    params.height = mStart + (int) ((mEnd - mStart) * interpolatedTime);
                    setLayoutParams(params);
                }
            };
            animation.setDuration(500);
            startAnimation(animation);
        }

    }

    @Override
    public void setLayoutParams(ViewGroup.LayoutParams params) {
        super.setLayoutParams(params);
    }

    public void shrink() {
        if (isExpand) {
            isExpand = false;
            Animation animation = new Animation() {
                @Override
                protected void applyTransformation(float interpolatedTime, Transformation t) {
                    ViewGroup.LayoutParams params = ExpandTextView.this.getLayoutParams();
                    params.height = mStart + (int) ((mEnd - mStart) * (1 - interpolatedTime));
                    setLayoutParams(params);
                }
            };
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    mText.setTextColor(mTextColor);
                    mExpandText.setTextColor(Color.TRANSPARENT);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            animation.setDuration(500);
            startAnimation(animation);
        }
    }

    public void switchs() {
        if (isExpand) {
            shrink();
        } else {
            expand();
        }
    }


}

使用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.ming.expandtextview.MainActivity">


    <Button
        android:id="@+id/expand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="Expand" />

    <Button
        android:id="@+id/strink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/expand"
        android:layout_toRightOf="@+id/expand"
        android:text="Strink" />

    <com.ming.expandtextview.ExpandTextView
        android:id="@+id/expandtextview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/expand">
    </com.ming.expandtextview.ExpandTextView>
</RelativeLayout>

Activity.java

public class MainActivity extends AppCompatActivity {

    private Button mBtnExpand;
    private Button mBtnStrink;
    private ExpandTextView mExpandTextview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtnExpand = (Button) findViewById(R.id.expand);
        mBtnStrink = (Button) findViewById(R.id.strink);
        mExpandTextview = (ExpandTextView) findViewById(R.id.expandtextview);
        mExpandTextview.setTextColor(Color.BLACK);
        mExpandTextview.setTextSize(18);
        mExpandTextview.setTextMaxLine(2);
        mExpandTextview.setText("科技本身蘊(yùn)含著一種碾壓一切的力量,而這種力量會(huì)導(dǎo)致我們加速奔向某種設(shè)定好的結(jié)局。" +
                "這篇文章嘗試預(yù)測未來最可能的5種結(jié)局:黃金時(shí)代,虛擬世界,冷平衡,生化人,大寂滅。");
        mBtnExpand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mExpandTextview.expand();

                //或者mExpandTextview.switchs();
            }
        });
        mBtnStrink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mExpandTextview.shrink();
            }
        });
    }
}

DEMO下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,126評(píng)論 1 92
  • RelativeLayout 第一類:屬性值為true可false android:layout_centerHr...
    兀兀沙彌閱讀 3,132評(píng)論 0 15
  • 安卓常用控件及其常用屬性 TextView android:id 這是唯一地標(biāo)識(shí)控件的ID。 android:ca...
    icechao閱讀 1,192評(píng)論 0 16
  • 選擇qi:是表達(dá)式 標(biāo)簽選擇器 類選擇器 屬性選擇器 繼承屬性: color,font,text-align,li...
    wzhiq896閱讀 2,114評(píng)論 0 2
  • 今天早上起床我們吃完早點(diǎn),開始我們第二天的旅程。我們坐上車把車駛向延安的寶塔山,一路很是期待會(huì)有什么美景和名勝...
    劉云旭媽媽閱讀 250評(píng)論 0 0

友情鏈接更多精彩內(nèi)容