当前位置:K88软件开发文章中心编程语言APP编程Android01 → 文章内容

8.4.3 Android动画合集之属性动画-初见

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-12 6:27:45

tivity.java部分的代码,其实都是大同小异的~public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_one; private Button btn_two; private Button btn_three; private Button btn_four; private Button btn_five; private LinearLayout ly_root; private TextView tv_pig; private int height; private ObjectAnimator animator1; private ObjectAnimator animator2; private ObjectAnimator animator3; private ObjectAnimator animator4; private AnimatorSet animSet; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindViews(); initAnimator(); } private void bindViews() { ly_root = (LinearLayout) findViewById(R.id.ly_root); btn_one = (Button) findViewById(R.id.btn_one); btn_two = (Button) findViewById(R.id.btn_two); btn_three = (Button) findViewById(R.id.btn_three); btn_four = (Button) findViewById(R.id.btn_four); btn_five = (Button) findViewById(R.id.btn_five); tv_pig = (TextView) findViewById(R.id.tv_pig); height = ly_root.getHeight(); btn_one.setOnClickListener(this); btn_two.setOnClickListener(this); btn_three.setOnClickListener(this); btn_four.setOnClickListener(this); btn_five.setOnClickListener(this); tv_pig.setOnClickListener(this); } //初始化动画 private void initAnimator() { animator1 = ObjectAnimator.ofFloat(tv_pig, "alpha", 1f, 0f, 1f, 0f, 1f); animator2 = ObjectAnimator.ofFloat(tv_pig, "rotation", 0f, 360f, 0f); animator3 = ObjectAnimator.ofFloat(tv_pig, "scaleX", 2f, 4f, 1f, 0.5f, 1f); animator4 = ObjectAnimator.ofFloat(tv_pig, "translationY", height / 8, -100, height / 2); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_one: animator1.setDuration(3000l); animator1.start(); break; case R.id.btn_two: animator2.setDuration(3000l); animator2.start(); break; case R.id.btn_three: animator3.setDuration(3000l); animator3.start(); break; case R.id.btn_four: animator4.setDuration(3000l); animator4.start(); break; case R.id.btn_five: //将前面的动画集合到一起~ animSet = new AnimatorSet(); animSet.play(animator4).with(animator3).with(animator2).after(animator1); animSet.setDuration(5000l); animSet.start(); break; case R.id.tv_pig: Toast.makeText(MainActivity.this, "不愧是coder-pig~", Toast.LENGTH_SHORT).show(); break; } }}用法也非常简单,上面涉及到的组合动画我们下面讲~4.组合动画与AnimatorListener从上面两个例子中我们都体验了一把组合动画,用到了AnimatorSet这个类!我们调用的play()方法,然后传入第一个开始执行的动画,此时他会返回一个Builder类给我们:接下来我们可以调用Builder给我们提供的四个方法,来组合其他的动画:after(Animator anim) 将现有动画插入到传入的动画之后执行after(long delay) 将现有动画延迟指定毫秒后执行before(Animator anim) 将现有动画插入到传入的动画之前执行with(Animator anim) 将现有动画和传入的动画同时执行嗯,很简单,接下来要说下动画事件的监听,上面我们ValueAnimator的监听器是AnimatorUpdateListener,当值状态发生改变时候会回调onAnimationUpdate方法!除了这种事件外还有:动画进行状态的监听~ AnimatorListener,我们可以调用addListener方法添加监听器,然后重写下面四个回调方法:onAnimationStart():动画开始onAnimationRepeat():动画重复执行onAnimationEnd():动画结束onAnimationCancel():动画取消没错,加入你真的用AnimatorListener的话,四个方法你都要重写,当然和前面的手势那一节一样,Android已经给我们提供好一个适配器类:AnimatorListenerAdapter,该类中已经把每个接口方法都实现好了,所以我们这里只写一个回调方法也可以额!5.使用XML来编写动画使用XML来编写动画,画的时间可能比Java代码长一点,但是重用起来就轻松很多!对应的XML标签分别为:<animator><objectAnimator><set>相关的属性解释如下:android:ordering:指定动画的播放顺序:sequentially(顺序执行),together(同时执行)android:duration:动画的持续时间android:propertyName="x":这里的x,还记得上面的"alpha"吗?加载动画的那个对象里需要定义getx和setx的方法,objectAnimator就是通过这里来修改对象里的值的!android:valueFrom="1" :动画起始的初始值android:valueTo="0" :动画结束的最终值android:valueType="floatType":变化值的数据类型使用例子如下:①从0到100平滑过渡的动画:<animator xmlns:android="http://schemas.android.com/apk/res/android" android:valueFrom="0" android:valueTo="100" android:valueType="intType"/>②将一个视图的alpha属性从1变成0:<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:valueFrom="1" android:valueTo="0" android:valueType="floatType" android:propertyName="alpha"/>③set动画使用演示:

上一页  [1] [2] [3] [4]  下一页


8.4.3 Android动画合集之属性动画-初见