以下我会写出我所知道的两种方式:
- 帧动画实现的第一种方式的步骤:
(1)找一些图片放到drawable文件夹下
(2)在drawable文件夹下通过xml文件生成动画 创建的就是一个animation_list的一个xml文件
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >//设置是否循环播放,默认的是循环播放,false为只播放一次
<item
android:drawable="@drawable/a"
android:duration="50"/>
<item
android:drawable="@drawable/b"
android:duration="50"/>
<item
android:drawable="@drawable/c"
android:duration="50"/>
</animation-list>
(3)找到咱们写的那个动画xml
(4)通过imageView设置一个背景图
(5)通过动画对象的start(),stop()方法来开启或者是停止动画。
xml 文件:
activity_main3.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".otherActivity.Main3Activity">
<Button
android:id="@+id/bt_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始动画" />
<Button
android:id="@+id/bt_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结束动画" />
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/anim" />
</LinearLayout>
java 文件:
Main3Activity.java
public class Main3Activity extends AppCompatActivity implements View.OnClickListener {
/**
* 开始动画
*/
private Button mBtStart;
/**
* 结束动画
*/
private Button mBtEnd;
private ImageView mIv;
private AnimationDrawable animal;
private AnimationDrawable mDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
initView();
}
private void initView() {
mIv = (ImageView) findViewById(R.id.iv);
mBtStart = (Button) findViewById(R.id.bt_start);
mBtStart.setOnClickListener(this);
mBtEnd = (Button) findViewById(R.id.bt_end);
mBtEnd.setOnClickListener(this);
// setAnimationResourceOne();
setAnimationResourceTwo();
}
private void setAnimationResourceOne() {
// 使用 AnimationDrawable类,调用start() 和 stop() 方法
animal = (AnimationDrawable) mIv.getBackground();
}
private void setAnimationResourceTwo() {
// 通过逐帧动画的资源文件获得AnimationDrawable示例
mDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.anim);
// 把AnimationDrawable设置为ImageView的背景
mIv.setBackgroundDrawable(mDrawable);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.bt_start:
// animal.start();
start();
break;
case R.id.bt_end:
// animal.stop();
stop();
break;
}
}
@SuppressLint("WrongConstant")
protected void start() {
if (mDrawable != null && !mDrawable.isRunning()) {
mDrawable.start();
Toast.makeText(Main3Activity.this, "开始播放", 0).show();
Log.i("main", "index 为5的帧持续时间为:" + mDrawable.getDuration(5) + "毫秒");
Log.i("main", "当前AnimationDrawable一共有" + mDrawable.getNumberOfFrames() + "帧");
}
}
@SuppressLint("WrongConstant")
protected void stop() {
if (mDrawable != null && mDrawable.isRunning()) {
mDrawable.stop();
Toast.makeText(Main3Activity.this, "停止播放", 0).show();
}
}
}
- 帧动画实现的第二种方式的步骤:
利用代码的形式实现:
(1)创建图片动画的对象
(2)一帧一帧的添加图片,并且设置了显示时间
(3)设置是否为循环播放 默认的就是只播放一次
(4)开启动画,或者停止动画
xml 文件:
activity_main3.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".otherActivity.Main3Activity">
<Button
android:id="@+id/bt_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始动画" />
<Button
android:id="@+id/bt_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结束动画" />
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="100dp"
/>
</LinearLayout>
java 文件
Main3Activity.java
public class Main3Activity extends AppCompatActivity implements View.OnClickListener {
/**
* 开始动画
*/
private Button mBtStart;
/**
* 结束动画
*/
private Button mBtEnd;
private ImageView mIv;
private AnimationDrawable animal;
private AnimationDrawable mDrawable;
private AnimationDrawable mAnimationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
initView();
}
private void initView() {
mIv = (ImageView) findViewById(R.id.iv);
mBtStart = (Button) findViewById(R.id.bt_start);
mBtStart.setOnClickListener(this);
mBtEnd = (Button) findViewById(R.id.bt_end);
mBtEnd.setOnClickListener(this);
setAnimationResourceThree();
}
private void setAnimationResourceThree() {
mAnimationDrawable = new AnimationDrawable();
// 为AnimationDrawable添加动画帧
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.a), 80);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.b), 80);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.c), 80);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.d), 80);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.e), 80);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.f), 80);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.g), 80);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.h), 80);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.i), 80);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.j), 80);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.k), 80);
// 设置为循环播放
mAnimationDrawable.setOneShot(false);
// 设置ImageView的背景为AnimationDrawable
mIv.setBackgroundDrawable(mAnimationDrawable);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.bt_start:
startAnimationThree();
break;
case R.id.bt_end:
stopAnimationThree();
break;
}
}
@SuppressLint("WrongConstant")
protected void startAnimationThree() {
if (mAnimationDrawable != null && !mAnimationDrawable.isRunning()) {
mAnimationDrawable.start();
Toast.makeText(Main3Activity.this, "开始播放", 0).show();
Log.i("main", "index 为5的帧持续时间为:" + mAnimationDrawable.getDuration(5) + "毫秒");
Log.i("main", "当前AnimationDrawable一共有" + mAnimationDrawable.getNumberOfFrames() + "帧");
}
}
@SuppressLint("WrongConstant")
protected void stopAnimationThree() {
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
Toast.makeText(Main3Activity.this, "停止播放", 0).show();
}
}
两种实现方式的区别:xml文件生成的动画默认的是循环播放,java代码生成的动画默认是只播放一次
注意:如果在一个项目文件夹下有两个或两个以上的Activity要在清单文件下注册声明
网友评论