关于Android基本动画的应用
2015-09-21 20:02
513 查看
用Android基本动画做了一个账号输入的特效,在账号框获得焦点时label上移,输入内容时,密码框下滑渐显出来,以下是实现的代码
MainActivity.class
activity_main.xml
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>
相关文章推荐
- 最流行的android组件大全
- Android开发之对话框
- Android基础入门教程——2.4.6 ListView的数据更新问题
- Android Text Layout 框架
- Android Handler 异步消息处理机制 《第一行代码》
- Android 发送Post请求上传图片(以InputStream为数据交互)
- Android的权限机制总结
- Android和C#实时视频传输Demo
- listview 侧滑删除 仿qq
- android root权限破解分析
- Android drawable文件夹对应分辨率
- Android双SD卡路径获取
- Android自动化测试之monkeyrunner基本要素(七)
- Android自动化测试之MonkeyRunner工具(六)
- viewpager、fragment滑动切换卡顿问题
- Android自动化测试之虚拟机中软件安装方法(四)
- Android自动化测试之如何安装Android虚拟机(三)
- Android自动化测试之环境搭建(二)
- Android使用Ant打包所遇到问题的解决方法
- Android自动化测试之自动化测试基本原理(一)