本人小白,写的不好,小点声骂啊!
最近在项目中使用到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>
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; } } }
点击打开链接