Android开发之自定义控件与属性动画Animation的结合使用
2015-09-03 17:11
399 查看
一样的,V4包,eclipse上开发,笔者是根据一位叫做yayun0516的博主进行学习以及实现,除此之外再发表自己的一些见解以及增加合适的备注。 这个想法最近另笔者特别好奇以及羡慕,自定义控件的熟悉使用应该对一名Android开发者有较高的资历和能力要求,再加上与属性动画的使用,更是大大的提高了用户的体验度,嗯,废话不多说,贴代码和效果图~
MineActivity.java类,也就是重写EditText,
MainActivity.java:
activity_main.xml:
不会***gif动画~直接贴工程吧~
MineActivity.java类,也就是重写EditText,
public class MineActivity extends EditText { private Drawable mRightDrawable; private Boolean isHasFocus; public MineActivity(Context context) { // 回调父类方法 super(context); initView(); } // AttributeSet用来完成控件类构造函数,并在构造函数中将自定义控件类中变量与attrs.xml中的属性连接起来 public MineActivity(Context context, AttributeSet attributeSet) { super(context, attributeSet); initView(); } public MineActivity(Context context, AttributeSet attributeSet, int defstyle) { super(context, attributeSet, defstyle); initView(); } private void initView() { // Returns drawables for the left, top, right, and bottom borders. Drawable[] drawables = this.getCompoundDrawables(); // 取得right位置的Drawable // 即我们在布局文件中设置的android:drawableRight mRightDrawable = drawables[2]; // 焦点变化监听 this.setOnFocusChangeListener(new FocusChangeListenerImpl()); // 设置文字变化监听 this.addTextChangedListener(new TextWatcherImpl()); // 初始化时让右边clean图标不可见 setClearDrawableVisible(false); } private class FocusChangeListenerImpl implements OnFocusChangeListener { public void onFocusChange(View v, boolean hasFocus) { isHasFocus = hasFocus; if (isHasFocus) { boolean isVisible = getText().toString().length() >= 1; setClearDrawableVisible(isVisible); } else { setClearDrawableVisible(false); } } } // 当输入结束后判断是否显示右边clean的图标,一样的焦点判断 private class TextWatcherImpl implements TextWatcher { public void afterTextChanged(Editable s) { boolean isVisible = getText().toString().length() >= 1; setClearDrawableVisible(isVisible); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } } // 隐藏或显示右边clean的图标,很简单的焦点判断 protected void setClearDrawableVisible(boolean isVisible) { Drawable rightDrawable; if (isVisible) { rightDrawable = mRightDrawable; } else { rightDrawable = null; } // 使用代码设置该控件left, top, right, and bottom处的图标 setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], rightDrawable, getCompoundDrawables()[3]); } // 显示动画 public void setShakeAnimation() { //调用shakeAnimation this.startAnimation(shakeAnimation(5)); } // CycleTimes动画重复的次数 public Animation shakeAnimation(int CycleTimes) { //移动位置 Animation translateAnimation = new TranslateAnimation(0, 10, 0, 10); translateAnimation.setInterpolator(new CycleInterpolator(CycleTimes)); //重复时间 translateAnimation.setDuration(500); return translateAnimation; } }
MainActivity.java:
public class MainActivity extends Activity { private MineActivity usernameDeletableEditText; private MineActivity passwordDeletableEditText; private Button btn_logButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); usernameDeletableEditText=(MineActivity)findViewById(R.id.det_test); passwordDeletableEditText=(MineActivity)findViewById(R.id.user_password_input); btn_logButton=(Button)findViewById(R.id.btn_login); btn_logButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { String string1=usernameDeletableEditText.getText().toString(); String string2=passwordDeletableEditText.getText().toString(); if(TextUtils.isEmpty(string1)){ usernameDeletableEditText.setShakeAnimation();//设置动画 }else if(TextUtils.isEmpty(string2)){ passwordDeletableEditText.setShakeAnimation();//设置动画 } } }); } }
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" tools:context=".MainActivity" > <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:gravity="center" android:text="登 录" android:textSize="25sp" /> <com.jw.try_item1.MineActivity android:id="@+id/det_test" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/title" android:layout_margin="20dp" android:drawableLeft="@drawable/login_icon_user_f" android:drawableRight="@drawable/login_icon_user_f" android:ems="10" android:hint="请输入帐号名" /> <com.jw.try_item1.MineActivity android:id="@+id/user_password_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/det_test" android:layout_margin="20dp" android:layout_marginTop="10dp" android:drawableLeft="@drawable/login_icon_password_f" android:drawableRight="@drawable/login_icon_password_f" android:ems="10" android:hint="请输入密码" android:inputType="textPassword" android:singleLine="true" /> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/user_password_input" android:layout_margin="20dp" android:gravity="center" android:text="Login" /> </RelativeLayout>
不会***gif动画~直接贴工程吧~
相关文章推荐
- Android笔记:如何写出一个侧滑菜单
- android4.0 MediaPlayer的notify监听机制的全面剖析
- android中ListView的定位:使用setSelectionFromTop
- Android学习之文件存储方式
- 切换Android SDK Manager下载源
- Android适配器-附万能适配器工具类
- 关于Android studio彻底修改包名的解决办法
- Android ——fragment
- android使用smb协议在线播放视频(二)
- Android源码环境配置之Ubuntu 14.04 LTS x64
- Android 四大组件之Service解析
- PULL解析
- android 工具 Draw 9-patch 和去黑边
- 正确使用Android性能分析工具——TraceView
- Android开发 OAuth认证
- Android:OnClickListener cannot be resolved to a type
- Android:problem opening wizard the selected wizard could not be started
- android短信拦截实现的两种方法
- Android 文本阅读以及TextView指定字符高亮显示
- Android基础之intent-filter、action、category标签使用