设为首页 加入收藏

TOP

Android属性动画之ObjectAnimator(一)
2015-02-25 16:15:26 来源: 作者: 【 】 浏览:61
Tags:Android 属性 动画 ObjectAnimator

首先和一般的Android应用一样,我们先建一个工程,为了方便,我们的布局文件中就只添加一个ImageView和button按钮,代码如下:


? ? xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
? ? android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
? ? android:paddingRight="@dimen/activity_horizontal_margin"
? ? android:paddingTop="@dimen/activity_vertical_margin"
? ? android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:id="@+id/imageView"
? ? ? ? android:layout_alignParentTop="true"
? ? ? ? android:layout_alignParentLeft="true"
? ? ? ? android:layout_alignParentStart="true"
? ? ? ? android:src="@drawable/ic_launcher"
? ? ? ? android:onClick="imgClick"/>


? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="动画"
? ? ? ? android:id="@+id/button"
? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:layout_marginBottom="86dp"
? ? ? ? android:onClick="buttonClick"/>


下面是我们action,为了便于大家学习,我将代码分享如下:


public class MainActivity extends Activity {
? ? public ImageView imageView;
? ? public Button button;
? ? static int x = 0, xx = 0, y = 0, yy = 0;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? imageView = (ImageView)findViewById(R.id.imageView);
? ? ? ? button = (Button)findViewById(R.id.button);
? ? }


? ? public void imgClick(View view){
? ? ? ? Toast.makeText(this, "ImageView", Toast.LENGTH_SHORT).show();
? ? }
? ? public void buttonClick(View view){


//? ? ? ? xx += 20;
//? ? ? ? TranslateAnimation ta = new TranslateAnimation(x, xx, y, yy);//设置动画的偏移位移
//? ? ? ? x += 20;
//? ? ? ? ta.setDuration(1000);//设置动画的时长
//? ? ? ? ta.setFillAfter(true);//设置动画结束后停留在该位置
//? ? ? ? imageView.startAnimation(ta);


? ? ? ? //属性动画调用start()方法后是一个异步操作
//? ? ? ? ObjectAnimator.ofFloat(imageView, "translationX", 0F, 360F).setDuration(1000).start();//X轴平移旋转
//? ? ? ? ObjectAnimator.ofFloat(imageView, "translationY", 0F, 360F).setDuration(1000).start();//Y轴平移旋转
//? ? ? ? ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F).setDuration(1000).start();//360度旋转


? ? ? ? //同步动画设计
//? ? ? ? PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX", 0, 360F);
//? ? ? ? PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY", 0, 360F);
//? ? ? ? PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("rotation", 0, 360F);
//? ? ? ? ObjectAnimator.ofPropertyValuesHolder(imageView, p1, p2 ,p3).setDuration(1000).start();


? ? ? ? //通过AnimatiorSet来设计同步执行的多个属性动画
? ? ? ? ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "translationX", 0F, 360F);//X轴平移旋转
? ? ? ? ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationY", 0F, 360F);//Y轴平移旋转
? ? ? ? ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F);//360度旋转
? ? ? ? AnimatorSet set = new AnimatorSet();
? ? ? ? //set.playSequentially(animator1, animator2, animator3);//分步执行
? ? ? ? //set.playTogether(animator1, animator2, animator3);//同步执行


? ? ? ? //属性动画的执行顺序控制
? ? ? ? // 先同步执行动画animator2和animator3,然后再执行animator1
? ? ? ? set.play(animator3).with(animator1)

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android属性动画之ObjectAnimator.. 下一篇Android之自动文本输入识别提示

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: