安卓逐帧动画
来源:互联网 发布:补水面膜 知乎 编辑:程序博客网 时间:2024/04/30 20:21
通常有2种实现方式,代码或者XML
(1)、XML结合java代码 方式实现
在res目录下新建anim文件夹,右键选`
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <item android:drawable="@drawable/horn0" android:duration="500" /> <item android:drawable="@drawable/horn1" android:duration="500" /> <item android:drawable="@drawable/horn2" android:duration="500" /> <item android:drawable="@drawable/horn3" android:duration="500" /> <item android:drawable="@drawable/horn4" android:duration="500" /></animation-list>
然后在代码中设置
private void playImgAnim(ImageView imageView) { animationDrawable = new AnimationDrawable(); imageView.setBackgroundResource(R.anim.frame_animation); animationDrawable = (AnimationDrawable) imageView.getBackground(); if(!animationDrawable.isRunning())animationDrawable.start(); }
@Overridepublic void onStop() { super.onStop(); if (animationDrawable.isRunning()) animationDrawable.stop();}
(2)全代码实现
public class MainActivity extends Activity { private AnimationDrawable mAnimationDrawable; private Drawable mDrawable; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mAnimationDrawable = new AnimationDrawable(); img = (ImageView) findViewById(R.id.imageView1); } public void click(View view) { switch (view.getId()) { case R.id.btn1: start(); break; case R.id.btn2: stop(); break; } } private void stop() { if(mAnimationDrawable.isRunning()) { mAnimationDrawable.stop(); } } private void start() { for (int i = 0; i < 5; i++) { //第一个参数为ID名,第二个为资源属性是ID或者是Drawable,第三个为包名 int resourcesId = getResources().getIdentifier("horn" + i, "drawable", this.getPackageName()); mDrawable = getResources().getDrawable(resourcesId); mAnimationDrawable.addFrame(mDrawable, 500); } mAnimationDrawable.setOneShot(false); img.setBackgroundDrawable(mAnimationDrawable); if(!mAnimationDrawable.isRunning()){ mAnimationDrawable.start(); } }}
//xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <Button android:id="@+id/btn1" android:onClick="click" android:text="start" android:layout_alignParentBottom="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn2" android:onClick="click" android:text="stop" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
0 0
- 安卓逐帧动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 动画
- 怎么把word转成pdf格式查看
- EXFAT和NTFS哪个好
- Linux使用dd命令测试硬盘读写速度
- Android Studio导入第三方类库的方法
- java判断字符串是否为空
- 安卓逐帧动画
- TI开发系列讲座
- 使用jacob将word转成PDF
- [译]Apply和数组:三个技巧
- 新极客头条:带你发现这个世界是什么,哪些问题更重要
- The line endings in the following file are not consistent.
- 10行代码编写一个c++服务器
- 流媒体相关资源
- ROS学习手记 - 5 理解ROS中的基本概念_Services and Parameters