TextView展开加动画效果实现

来源:互联网 发布:怎么上badoo网络 编辑:程序博客网 时间:2024/05/16 17:14
package com.example.textviewdemo;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.view.ViewTreeObserver;import android.view.View.OnClickListener;import android.view.ViewTreeObserver.OnGlobalLayoutListener;import android.widget.TextView;import android.widget.RelativeLayout.LayoutParams;/** *  * @author Teiron *  */@SuppressLint("HandlerLeak")public class TextViewActivity extends Activity implements OnClickListener {protected static final String TAG = "TextViewActivity";protected static final int UPDATE_SELF   = 0;protected static final int OFFSET        = -35;//指定负数protected static int       INIT_LINES    = 3;protected static final int REFRESH_TIME  = 10;private TextView mTVcontent;protected int totalHeigh;private boolean isExpanded;Handler mHandler = new Handler() {private int offset;private int height;private int initHeight;public void dispatchMessage(android.os.Message msg) {switch (msg.what) {case UPDATE_SELF:if (msg.arg1 != 0) {offset = msg.arg1;mTVcontent.setMaxLines(Integer.MAX_VALUE);height     = mTVcontent.getMeasuredHeight();initHeight = mTVcontent.getLineHeight()*INIT_LINES;//Log.e(TAG,height+"::offset__"+offset);}height += offset;if (height > totalHeigh) {height = totalHeigh;}if (height < initHeight) {height = initHeight-offset;}LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, height);mTVcontent.setLayoutParams(params);if (height < totalHeigh && height > initHeight) {mHandler.sendEmptyMessageDelayed(UPDATE_SELF, REFRESH_TIME);}break;};}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView();findviewById();setListener();processData();}private void processData() {ViewTreeObserver vto = mTVcontent.getViewTreeObserver();vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {@SuppressLint("NewApi")public void onGlobalLayout() {totalHeigh = mTVcontent.getHeight();if(INIT_LINES>mTVcontent.getMaxLines()){INIT_LINES = mTVcontent.getMaxLines();}mTVcontent.setMaxLines(INIT_LINES);mTVcontent.getViewTreeObserver().removeGlobalOnLayoutListener(this);}});}private void toggle() {if (!mHandler.hasMessages(UPDATE_SELF)) {Log.e("TAG", "HANDLER MESSAGE");Message msg = Message.obtain();msg.what = UPDATE_SELF;if (!isExpanded) {isExpanded = !isExpanded;msg.arg1 = Math.abs(OFFSET);} else {isExpanded = !isExpanded;msg.arg1 = OFFSET;}mHandler.sendMessage(msg);}}private void setListener() {mTVcontent.setOnClickListener(this);}private void findviewById() {mTVcontent = (TextView) findViewById(R.id.textview);}private void setContentView() {setContentView(R.layout.activity_main);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.textview:toggle();break;}}}
改天把它改成自定义控件;
原创粉丝点击