仿qq搜索动画效果

在使用QQ的时候发现搜索的动画效果很好,所以就模仿做了一个,原本打算用于实际项目,但是也未使用到,所以将该demo抽出来写个博客,这个demo是基于自定义项目栏而非actionbar

先上图:

搜索框获得焦点后,顶部搜索测试或慢慢向上收索,动画完成后pop页面出现,退出也是一样


中间的过渡效果:


顶部项目栏收索的·动画:

package com.tencentsearch.activity;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.view.animation.Animation.AnimationListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {

	@ViewInject(R.id.tsearch_view)
	private View tsearch_view;
	@ViewInject(R.id.tsearch_ll)
	private LinearLayout tsearch_ll;
	@ViewInject(R.id.tsearch_edt)
	private EditText tsearch_edt;

	@SuppressLint("HandlerLeak")
	private Handler cmHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				collapse(tsearch_ll);
				break;

			case 2:
				expand(tsearch_ll);
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ViewUtils.inject(this);

		init();
	}

	private void init() {
		tsearch_edt.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View view, boolean focus) {
				if (focus) {
					cmHandler.sendEmptyMessage(1);
				}
			}
		});
	}

	public void expand(final View v) {
		v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		final int targtetHeight = 100;
		v.getLayoutParams().height = 0;
		v.setVisibility(View.VISIBLE);
		Animation a = new Animation() {
			@Override
			protected void applyTransformation(float interpolatedTime, Transformation t) {
				v.getLayoutParams().height = interpolatedTime == 1 ? targtetHeight : (int) (targtetHeight * interpolatedTime);
				v.requestLayout();
			}

			@Override
			public boolean willChangeBounds() {
				return true;
			}
		};
		a.setDuration(150);
		v.startAnimation(a);
	}

	public void collapse(final View v) {
		final int initialHeight = v.getMeasuredHeight();
		Animation a = new Animation() {
			@Override
			protected void applyTransformation(float interpolatedTime, Transformation t) {
				if (interpolatedTime == 1) {
					v.setVisibility(View.GONE);
				} else {
					v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
					v.requestLayout();
				}
			}

			@Override
			public boolean willChangeBounds() {
				return true;
			}
		};
		a.setDuration(150);
		v.startAnimation(a);
		a.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation arg0) {

			}

			@Override
			public void onAnimationRepeat(Animation arg0) {

			}

			@Override
			public void onAnimationEnd(Animation arg0) {
				new PopSearch(MainActivity.this, tsearch_view, cmHandler);
				tsearch_edt.clearFocus();
			}
		});
	}

}

demo地址:http://download.csdn.net/detail/u011566000/8400373


猜你喜欢

转载自blog.csdn.net/u011566000/article/details/43193489
今日推荐