第一次在csdn上写文章,心情还有点小激动哈!主要是来了新公司暂时没有任务,倒是给了我一个demo让我看看,里面牵扯到了动画的处理,正好扔了安卓半年多了,写篇博客帮自己回忆回忆。不喜勿喷
上面这个截图是从http://developer.android.com/guide/topics/graphics/prop-animation.html#property-vs-view中截下来的。
主要意思是说View animation只是将view在另一个地方重画了。而真正的控件位置却没有变化。比如你让一个按钮动画移动至里一个位置,按钮的位置是变了,但是你响应按钮点击事件的地方没有变。而property animation在移动过程中却会将按钮整个移动,自然包括其按钮点击事件的响应位置。好了,说了这么多。我的表达能力差,大家也不一定能明白,上一段代码大家看一下吧。
这是mainActivity的代码:
package com.test.anima;
import com.animationtest.R;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
public class ActivityMain extends Activity implements OnClickListener {
private Button btn_startAnima;
private Button btn_stopAnima;
private Button btn_trigger1;
private Button btn_trigger2;
private final ValueAnimator valueAnimator = ValueAnimator.ofInt(10, 800);;
String flag1="view animation";
String flag2 ="property animation";
private Animation translateAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
btn_trigger1 = (Button) findViewById(R.id.btn_trigger1);
btn_trigger2 = (Button) findViewById(R.id.btn_trigger2);
btn_startAnima = (Button) findViewById(R.id.btn_startanim);
btn_stopAnima = (Button) findViewById(R.id.btn_stopanim);
btn_startAnima.setOnClickListener(this);
btn_stopAnima.setOnClickListener(this);
btn_trigger1.setOnClickListener(this);
btn_trigger2.setOnClickListener(this);
}
private void startProperAnimation() {
// btn_startAnima.setX(0);
// btn_startAnima.setY(btn_startAnima.getHeight());
valueAnimator.setDuration(20000);
valueAnimator.setTarget(btn_trigger2);
valueAnimator.setRepeatCount(Animation.INFINITE);
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int a = (Integer) (valueAnimator.getAnimatedValue());
// btn_trigger2.setX(a);
btn_trigger2.setY(a);
}
});
valueAnimator.start();
}
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
btn_trigger1.setText(flag1 + "点我呀");
break;
case 1:
btn_trigger2.setText(flag2+ "点我呀");
default:
break;
}
}
};
public void startViewAnimation() {
// btn_startAnima.setX(0);
// btn_startAnima.setY((float) (btn_trigger1.getHeight()+10.0));
translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0f,
Animation.ABSOLUTE, 500f, Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, 480f);
translateAnimation.setDuration(20000);
translateAnimation.setRepeatCount(Animation.INFINITE);
btn_trigger1.startAnimation(translateAnimation);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_trigger1:
btn_trigger1.setText(flag1 + "点到了");
handler.sendEmptyMessageDelayed(0, 500);
case R.id.btn_trigger2:
btn_trigger2.setText(flag2 + "点到了");
handler.sendEmptyMessageDelayed(1, 500);
break;
case R.id.btn_startanim:
startViewAnimation();
startProperAnimation();
break;
case R.id.btn_stopanim:
valueAnimator.cancel();
translateAnimation.cancel();
break;
default:
break;
}
}
}
layout的XML代码比较简单:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="@+id/btn_trigger1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view animation" />
<Button
android:id="@+id/btn_trigger2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="proper animation" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<Button
android:id="@+id/btn_startanim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始动画" />
<Button
android:id="@+id/btn_stopanim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止动画" />
</LinearLayout>
</LinearLayout>
注:由于property animation要求Android API为11,所以运行时一定要是Android3.0以上的版本。
运行时可以看到进行propertyanimation的按钮在运动的过程中点击也有反应,而进行viewAnimation的按钮则是在运动过程中无反应,但是在按钮的初始位置(进行动画之前)上点击有反应。
点击开始动画后开始 点击运行中的property animation的按钮
点击运行中的view animation按钮无反应,只有点击图中黄色框部分有反应
好了,第一篇博客就到此结束啦!