Android開發之自定義控件與屬性動畫Animation的結合使用

一樣的,V4包,eclipse上開發,筆者是根據一位叫做yayun0516的博主進行學習以及實現,除此之外再發表自己的一些見解以及增加合適的備註。 這個想法最近另筆者特別好奇以及羨慕,自定義控件的熟悉使用應該對一名Android開發者有較高的資歷和能力要求,再加上與屬性動畫的使用,更是大大的提高了用戶的體驗度,嗯,廢話不多說,貼代碼和效果圖~


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動畫~直接貼工程吧~


發佈了36 篇原創文章 · 獲贊 46 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章