【ANDROID自定義控件】可擴展的TextView,ExpandableTextView與Scroller類的使用
package com.sahadev.sildingfinishlayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.TextView;
public class ExpandableTextView extends RelativeLayout {
private TextView mTextView;
private Button mButton;
private int mTextViewId = 567576458;// 這裏注意不要隨便填一個簡單的數字,可能會和R中的ID衝突造成無效
private Scroller mScroller;
private int mHeight, mWidthMeasureSpec, mButtonHeight, paddingSize = 1;
private boolean isExpanded, WSettedFlag, HSettedFlag, onceFlag;
private int times = 2;// 縮小的倍數,默認2倍
public ExpandableTextView(Context context) {
this(context, null);
}
public ExpandableTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ExpandableTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mTextView = new TextView(context);
mTextView.setId(mTextViewId);
mButton = new Button(context);
mButton.setText("擴 展");
mScroller = new Scroller(context);
LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, mTextViewId);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!isExpanded) {
// 展開來
mScroller.startScroll(0, mHeight / times, 0, mHeight / times);
postInvalidate();
isExpanded = true;
} else {
// 收回去
mScroller.startScroll(0, mHeight, 0, -mHeight / times);
postInvalidate();
isExpanded = false;
}
}
});
addView(mTextView);
addView(mButton, lp);
}
@Override
public void computeScroll() {
super.computeScroll();
if (mScroller.computeScrollOffset()) {
mTextView.setHeight(mScroller.getCurrY());
postInvalidate();
return;
}
}
public void setTimes(int times) {
if (times == 0) {
throw new ArithmeticException("倍數不能為0");
}
this.times = times;
}
public void setTextViewPadding(int pixels) {
mTextView.setPadding(pixels, pixels, pixels, 0);
paddingSize = pixels;
}
public void setButtonTips(CharSequence text) {
mButton.setText(text);
}
public void setText(CharSequence text) {
mTextView.setText(text);
}
public void setTextColor(int color) {
mTextView.setTextColor(color);
}
public void setTextSize(float size) {
mTextView.setTextSize(size);
}
public void setBackgroundColor(int color) {
mTextView.setBackgroundColor(color);
}
public void setWidth(int width) {
mWidthMeasureSpec = width;
mTextView.setWidth(width - paddingSize * 2);
WSettedFlag = true;
}
public void setHeight(int height) {
mHeight = (height - mButtonHeight) * 2;
HSettedFlag = true;
}
/* onMeasure方法在重繪的時候會一直被調用 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
/* 此步驟隻用執行一次,獲取到textView的寬度以及Button的高度以及一些初始化的值 */
if (!WSettedFlag) {
WSettedFlag = true;
mWidthMeasureSpec = mTextView.getMeasuredWidth();
}
if (!HSettedFlag) {
HSettedFlag = true;
mHeight = mTextView.getMeasuredHeight();
OtherTools.showLog("mHeight----" + mHeight);
}
if (!onceFlag) {
onceFlag = true;
mButtonHeight = mButton.getMeasuredHeight();
OtherTools.showLog("mButtonHeight----" + mButtonHeight);
// mTextView.setHeight(mHeight / times + mButtonHeight >
// heightMeasureSpec ? heightMeasureSpec - mButtonHeight : mHeight /
// times);
mTextView.setHeight(mHeight / times);
}
// int tempHeight = mHeight / 2 + mButtonHeight;
// tempHeight = tempHeight > heightMeasureSpec ? heightMeasureSpec :
// tempHeight;
setMeasuredDimension(mWidthMeasureSpec, mButtonHeight + mTextView.getMeasuredHeight());
}
}
最後更新:2017-04-03 12:55:39