NineOldAndroids在低版本系統的使用中點擊事件滯留在原來位置的處理

本人小白,寫的不好,小點聲罵啊!

最近在項目中使用到NineOldAndroids這個庫,官方說這個支持到1.0的系統,姑且就相信了,就用這個庫開始碼,到後來一測試發現,Android2.3的機子,按鈕平移之後,點擊事件還停留在原來的位置,我那個鬱悶阿,整整找了一晚上也沒解決,後來同事給了我一個demo,以下上代碼:

首先是佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.chen.propertyanimator.MainActivity" >

    <Button
        android:id="@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="移動Button2"/>
    
    <LinearLayout  
        android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <Button
        android:id="@+id/btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我將會" />
    
    </LinearLayout>

</LinearLayout>

然後是Activity,其實最主要的是在動畫完成的時候,通過手動的方式把按鈕重新描繪到你要移動的地方,這樣事件就不會停留在原來的位置上了.

package com.chen.propertyanimator;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.view.ViewHelper;

public class MainActivity extends ActionBarActivity  implements OnClickListener{
	private Button btn1;
	private Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=(Button) findViewById(R.id.btn_1);
        btn2=(Button) findViewById(R.id.btn_2);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        
        
    }
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_1:
			ValueAnimator animator=ObjectAnimator.ofFloat(btn2, "x", ViewHelper.getX(btn2),ViewHelper.getX(btn2)+100);
			animator.setDuration(500);
			animator.addListener(new AnimatorListener() {
				
				@Override
				public void onAnimationStart(Animator arg0) {
					
				}
				
				@Override
				public void onAnimationRepeat(Animator arg0) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onAnimationEnd(Animator arg0) {//處理的重點,具體的代碼需要自己根據動畫的效果來決定
					int left=btn2.getLeft()+100;
					int top=btn2.getTop();
					int width = btn2.getWidth();
					int height = btn2.getHeight();
					btn2.clearAnimation();
					btn2.layout(left, top, left+width, top+height);
				}
				
				@Override
				public void onAnimationCancel(Animator arg0) {
					// TODO Auto-generated method stub
				}
			});
			animator.start();
			break;
		case R.id.btn_2:
			Toast.makeText(this, "I am here!", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
	}
}


關於View有關的知識,像view.layout()方法阿什麼的,請移步到

點擊打開鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章