关于Android基本动画的应用

用Android基本动画做了一个账号输入的特效,在账号框获得焦点时label上移,输入内容时,密码框下滑渐显出来,以下是实现的代码

MainActivity.class

public class MainActivity extends Activity {

	private TextView textView;
	private EditText editText1;
	private EditText editText2;
	private LinearLayout layout;
	private int height;

	private boolean hasUp = false;
	private boolean hasShow = false;

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

		layout = (LinearLayout) findViewById(R.id.layout);
		textView = (TextView) findViewById(R.id.textView);
		editText1 = (EditText) findViewById(R.id.editText1);
		editText2 = (EditText) findViewById(R.id.editText2);

		editText1.setOnFocusChangeListener(mFocusChangeListener);
		editText1.addTextChangedListener(mTextWatcher);

		int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
		editText2.measure(w, h);
		height = editText2.getMeasuredHeight() / 2;
	}

	/**
	 * 焦点监听
	 */
	private OnFocusChangeListener mFocusChangeListener = new OnFocusChangeListener() {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			if (hasFocus && !hasUp) {
				slideview(textView.getTop(), -48);
			}
		}
	};

	/**
	 * 内容监听
	 */
	private TextWatcher mTextWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if (s.length() > 0 && !hasShow) {
				showView(layout.getTop() + height, 150);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			
		}

		@Override
		public void afterTextChanged(Editable s) {
			
		}
	};

	/**
	 * 上移动画
	 * 
	 * @param p1
	 * @param p2
	 */
	public void slideview(final float p1, final float p2) {
		/* 位置移动渐变效果 */
		TranslateAnimation animation = new TranslateAnimation(0, 0, p1, p2);
		animation.setInterpolator(new OvershootInterpolator());
		animation.setDuration(400);
		animation.setStartOffset(100);
		animation.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				hasUp = true;
				int left = textView.getLeft();
				int top = textView.getTop();
				int right = textView.getRight();
				int bottom = textView.getBottom();
				textView.clearAnimation();
				textView.layout(left, top + (int) p2, right, bottom);

				LayoutParams params = (LayoutParams) textView.getLayoutParams();
				params.topMargin += p2;
				textView.setLayoutParams(params);
			}
		});
		textView.startAnimation(animation);
	}

	/**
	 * 显示动画
	 * 
	 * @param p1
	 * @param p2
	 */
	private void showView(final float p1, final float p2) {
		/* 渐变透明度渐变效果 */
		AnimationSet animationSet = new AnimationSet(true);
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		alphaAnimation.setInterpolator(new LinearInterpolator());
		alphaAnimation.setDuration(1200);
		animationSet.addAnimation(alphaAnimation);

		/* 位置移动渐变效果 */
		TranslateAnimation animation = new TranslateAnimation(0, 0, p1, p2);
		animation.setInterpolator(new LinearInterpolator());
		animation.setDuration(400);
		animation.setStartOffset(100);
		animation.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				layout.setVisibility(View.VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				hasShow = true;
				int left = layout.getLeft();
				int top = layout.getTop();
				int right = layout.getRight();
				int bottom = layout.getBottom();
				layout.clearAnimation();
				layout.layout(left, top + (int) p2, right, bottom);

				LayoutParams params = (LayoutParams) layout.getLayoutParams();
				params.topMargin += p2;
				layout.setLayoutParams(params);
			}
		});
		layout.startAnimation(animation);
		// animationSet.addAnimation(animation);
		// animationSet.setFillBefore(false);
		// animationSet.setFillAfter(true);
		// layout.startAnimation(animationSet);
	}

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginTop="60dp"
        android:orientation="vertical"
        android:visibility="gone" >

        <EditText
            android:id="@+id/editText2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="请再次输入账号"
            android:paddingTop="24dp"
            android:textColor="#333333"
            android:textColorHint="#999999"
            android:textSize="15sp" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginTop="60dp"
        android:focusableInTouchMode="true" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingTop="24dp"
            android:textColor="#333333"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:paddingBottom="12dp"
            android:paddingLeft="12dp"
            android:paddingTop="24dp"
            android:text="请输入账号"
            android:textColor="#999999"
            android:textSize="15sp" />
    </RelativeLayout>

</RelativeLayout>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值