关于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
- 关于Android基本动画的应用
- 基本的android 动画
- Android动画的应用
- Android动画的基本使用
- 关于Android中的Rotate旋转动画应用
- 关于动画的一些基本网站
- 关于Android中的的动画
- Android动画之RotateAnimation的基本用法
- Android属性动画的基本用法
- Android中属性动画的基本用法
- Android 四种基本的动画效果
- Android基本的动画& animation工具类
- Android属性动画的基本使用
- Android中补间动画的基本使用
- Android属性动画的基本用法
- android动画的使用(上-基本用法)
- Android属性动画(ObjectAnimator)的基本用法
- android中的动画以及基本的形式
- spring,hibernate处理Lob类型数据
- Android开发之对话框
- Linux / UNIX: Find out or determine if process pid is running
- SQL Developer 警告--无法安装某些模块
- poj1753
- 关于Android基本动画的应用
- C++:数据类型
- 第七章,类和对象
- 第三周项目1、项目2: 顺序表的基本运算、建立“顺序表”算法库
- c#实现简单计算器
- linux常用c函数 进程操作篇 http://blog.chinaunix.net/uid-25906157-id-3136757.html
- 马哥Linux运维笔记---1
- 03-Block的使用场景
- C++11:并发、多线程