关于Android基本动画的应用

来源:互联网 发布:2017广联达软件多少钱 编辑:程序博客网 时间:2024/06/03 07:42

用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;@Overrideprotected 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() {@Overridepublic void onFocusChange(View v, boolean hasFocus) {if (hasFocus && !hasUp) {slideview(textView.getTop(), -48);}}};/** * 内容监听 */private TextWatcher mTextWatcher = new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {if (s.length() > 0 && !hasShow) {showView(layout.getTop() + height, 150);}}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic 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() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic 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() {@Overridepublic void onAnimationStart(Animation animation) {layout.setVisibility(View.VISIBLE);}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic 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
原创粉丝点击