可扩展的TextView,ExpandableTextView与Scroller类的使用

转载时请注明出处,尊重他人的劳动成果,谢谢。

废话不多说,先上图演示下成果(图有些丑,别见怪):

可扩展的TextView,ExpandableTextView与Scroller类的使用

最近一直在研究Scroller类的使用方法,看了很多遍别人的例子总是感觉不得要领,最后还是自己实践一下,这个控件的灵感来源于*上一个人的提问,就是这种可扩展的TextView,当然,人家那个很好看,那时候感觉很神奇,自从知道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());
	}
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/slide"
    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=".MainActivity" >

    <com.sahadev.sildingfinishlayout.ExpandableTextView
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#a6c" />

</RelativeLayout>

package com.sahadev.sildingfinishlayout;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;

public class MainActivity extends Activity {
	private ExpandableTextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		textView = (ExpandableTextView) findViewById(R.id.hello);

		textView.setText("近日,多家媒体报道,山西最大的民营企业联盛集团董事长邢利斌被警方带据分析,邢利斌这次被警方带走,很可能和联盛集团债台高筑有关。不过短短几年,山西煤老板为何就和金融机构从蜜月期走到了剑拔弩张的田地?");
		textView.setTextSize(30);
		textView.setTextColor(Color.WHITE);
		textView.setTextViewPadding(15);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

由于还不太会往GitHub上传东西,大家就将就着把代码拷贝运行吧,有什么疑问请在下面留言。

上一篇:webpack4配置vue环境和一些小坑。


下一篇:MaxCompute产品消费相关文章合集