Android动画之图片动画(四)

现在使ImageView中的图片可以动起来

1.在drawable-mdpi文件夹下加入图片,并加入一个xml文件,文件如下

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   <item android:drawable="@drawable/a" android:duration="500"/>   
  4.   <item android:drawable="@drawable/b" android:duration="500"/>   
  5.   <item android:drawable="@drawable/c" android:duration="500"/>   
  6.   <item android:drawable="@drawable/d" android:duration="500"/>   
  7.   
  8. </animation-list>  


2.代码如下

[java]  view plain copy
  1. iv.setBackgroundResource(R.drawable.anim);  
  2.             AnimationDrawable an=(AnimationDrawable)iv.getBackground();  
  3.             an.start();  


其实可以用一个线程加Handler来实现动画的,在线程中隔一定时间发送消息,更改ImageView的图片。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Android中的属性动画(Property Animation)来实现图片旋转动画。具体实现步骤如下: 1.在res/anim文件夹下创建rotate.xml文件,内容如下: ```xml <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:repeatCount="0" android:duration="1000" /> ``` 其中,fromDegrees和toDegrees分别表示旋转的起点角度和终点角度;pivotX和pivotY表示旋转中心点的位置;repeatCount表示动画重复次数,这里设置为0表示不重复;duration表示动画的持续时间,这里设置为1000毫秒。 2.在代码中找到要进行旋转动画的ImageView控件,使用如下代码进行动画设置: ```java ImageView imageView = findViewById(R.id.image_view); ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0, 360); rotationAnimator.setDuration(1000); rotationAnimator.setRepeatCount(ValueAnimator.INFINITE); rotationAnimator.setInterpolator(new LinearInterpolator()); rotationAnimator.start(); ``` 其中,R.id.image_view表示ImageView控件的ID;ObjectAnimator是属性动画中的旋转动画,ofFloat方法中的第一个参数表示要进行动画的控件,第二个参数表示动画类型(这里是旋转),第三个参数表示旋转起始角度,第四个参数表示旋转终止角度;setDuration方法表示动画持续时间;setRepeatCount方法设置动画重复次数,这里设置为无限循环;setInterpolator方法设置动画插值器,这里设置为线性插值器;start方法开始动画。 这样就可以实现图片旋转动画了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值