1. 程式人生 > >android之仿微博Textview的伸縮效果

android之仿微博Textview的伸縮效果

package com.example.testdemo;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	public static final String MainActivity = "MainActivity";
	/** 預設展示最大行數3行 */
	private static final int VIDEO_CONTENT_DESC_MAX_LINE = 3;
	/** 擴充 */
	private static final int SHOW_CONTENT_NONE_STATE = 0;//
	/** 收縮狀態 */
	private static final int SHRINK_UP_STATE = 1;//
	/** 展開狀態 */
	private static final int SPREAD_STATE = 2;// 展開狀態
	/** 預設收起狀態 */
	private static int mState = SHRINK_UP_STATE;// 預設收起狀態

	/** 展示文字內容 */
	private TextView mContentText;//
	/** 展示更多 */
	private RelativeLayout rlMoreParent;//
	/** 展開 */
	private ImageView mImageSpread;//
	/** 收起 */
	private ImageView mImageShrinkUp;//

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initData();
	}

	private void initView() {
		mContentText = (TextView) findViewById(R.id.text_content);
		rlMoreParent = (RelativeLayout) findViewById(R.id.show_more);
		mImageSpread = (ImageView) findViewById(R.id.spread);
		mImageShrinkUp = (ImageView) findViewById(R.id.shrink_up);
		rlMoreParent.setOnClickListener(this);

	}

	private void initData() {
		// 超過3行 其實是8行
		setTextViewText(mContentText, getString(R.string.txt_info));
		// 3行
		// setTextViewText(mContentText,
		// "蘋果公司在美國舊金山召開2014年度全球開發者大會。在全球開發者大會之前,就有預測稱此次蘋果會有一些硬體產品亮相,甚至是iWatch智慧手錶、iPhone 6,因為");
		// 1行
		// setTextViewText(mContentText, "asdhfahsdhf");
	}

	/**
	 * 
	 * be used for :設定是否可伸縮
	 * 
	 * @author zhongwr
	 * @2015-11-18
	 * @param mContentText
	 * @param content
	 */
	private void setTextViewText(TextView mContentText, String content) {
		mContentText.setText(content);
		// 等待textview繪畫完後,linecount才會準確,否則首次獲取都是0
		mContentText.post(new StrecthRunnable(mContentText));
	}

	class StrecthRunnable implements Runnable {
		private TextView tvStrecth;

		public StrecthRunnable(TextView tvStrecth) {
			this.tvStrecth = tvStrecth;
		}

		@Override
		public void run() {
			if (isStrecthShrink(tvStrecth)) {
				tvStrecth.setMaxLines(VIDEO_CONTENT_DESC_MAX_LINE);
				tvStrecth.setEllipsize(TruncateAt.END);
				rlMoreParent.setVisibility(View.VISIBLE);
				mImageShrinkUp.setVisibility(View.GONE);
				mImageSpread.setVisibility(View.VISIBLE);
			} else {
				rlMoreParent.setVisibility(View.GONE);
			}
		}

	}

	/**
	 * 
	 * be used for :判斷是否內容夠長,以至於達到伸縮效果
	 * 
	 * @author zhongwr
	 * @2015-11-18
	 * @param mContentText
	 */
	private boolean isStrecthShrink(TextView mContentText) {
		int lines = mContentText.getLineCount();
		Log.d(MainActivity, "lines =" + lines);
		if (lines > VIDEO_CONTENT_DESC_MAX_LINE) {// 大於3行才有伸縮效果
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.show_more: {
			if (mState == SPREAD_STATE) {// 收縮:由展開到收縮的操作
				mContentText.setMaxLines(VIDEO_CONTENT_DESC_MAX_LINE);
				mContentText.requestLayout();
				mImageShrinkUp.setVisibility(View.GONE);
				mImageSpread.setVisibility(View.VISIBLE);
				mState = SHRINK_UP_STATE;
			} else if (mState == SHRINK_UP_STATE) {// 展開:有收縮到展開的操作
				mContentText.setMaxLines(Integer.MAX_VALUE);
				mContentText.requestLayout();
				mImageShrinkUp.setVisibility(View.VISIBLE);
				mImageSpread.setVisibility(View.GONE);
				mState = SPREAD_STATE;
			}
			break;
		}
		default: {
			break;
		}
		}
	}

}