帧动画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=".Main2Activity">
<Button
android:text="开始"
android:id="@+id/bntstart"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="停止"
android:id="@+id/bntstop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:background="@drawable/frame_xml"
android:id="@+id/iv1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Activity
public class Main2Activity extends AppCompatActivity {
@ViewInject(R.id.iv1)
ImageView imageView;
AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
x.view().inject(this);
animationDrawable = (AnimationDrawable) imageView.getBackground();
}
@Event({R.id.bntstart,R.id.bntstop})
private void getclick(View view){
switch (view.getId()){
case R.id.bntstart:
if (!animationDrawable.isRunning()){
animationDrawable.start();
}
break;
case R.id.bntstop:
if (animationDrawable.isRunning()){
animationDrawable.stop();
}
break;
}
}
}
效果
帧动画java创建
布局
<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=".Main2Activity">
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iv1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Activity
public class Main2Activity extends AppCompatActivity {
@ViewInject(R.id.iv1)
ImageView imageView;
@ViewInject(R.id.b1)
Button button;
@ViewInject(R.id.b2)
Button button1;
AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
x.view().inject(this);
initAnimation();
initListener();
}
private void initListener() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!animationDrawable.isRunning()){
animationDrawable.start();
}
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (animationDrawable.isRunning()){
animationDrawable.stop();
}
}
});
}
private void initAnimation() {
animationDrawable = new AnimationDrawable();
animationDrawable.addFrame(getResources().getDrawable(R.drawable.t1),50);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.t2),50);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.t3),50);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.t4),50);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.nv),50);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.nv1),50);
imageView.setImageDrawable(animationDrawable);
}
}
效果
补间动画
布局
<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=".MainActivity">
<Button
android:text="透明动画"
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="旋转动画"
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="缩放动画"
android:id="@+id/b3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="平移动画"
android:id="@+id/b4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:background="@drawable/bus"
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ImageView imageView;
AnimationDrawable animationDrawable;
int i=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.iv1);
findViewById(R.id.b1).setOnClickListener(this);
findViewById(R.id.b2).setOnClickListener(this);
findViewById(R.id.b3).setOnClickListener(this);
findViewById(R.id.b4).setOnClickListener(this);
animationDrawable = new AnimationDrawable();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.b1://透明动画
imageView.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.alphe));
break;
case R.id.b2://旋转动画
imageView.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate));
break;
case R.id.b3://缩放动画
//方法1
// imageView.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.scale));
//方法2
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 3.0f, 1.0f, 4.0f, ScaleAnimation.ABSOLUTE, 100, ScaleAnimation.ABSOLUTE, 100);
scaleAnimation.setFillBefore(true);
scaleAnimation.setRepeatMode(ScaleAnimation.REVERSE);
scaleAnimation.setDuration(5000);
scaleAnimation.setRepeatCount(5);
scaleAnimation.setInterpolator(new BounceInterpolator());
scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Toast.makeText(MainActivity.this, "开始", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(MainActivity.this, "结束", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationRepeat(Animation animation) {
Toast.makeText(MainActivity.this, "次数"+i++, Toast.LENGTH_SHORT).show();
}
});
imageView.startAnimation(scaleAnimation);
break;
case R.id.b4://平移动画
imageView.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.trag));
break;
}
}
}
anim
透明
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="5000"
android:fillBefore="true"
>
</alpha>
旋转
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="1.0"
android:toDegrees="720"
android:pivotX="200"
android:pivotY="200"
android:fillBefore="true"
android:duration="5000"
>
</rotate>
缩放
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYScale="1.0"
android:fromXScale="1.0"
android:toYScale="4.0"
android:toXScale="3.0"
android:pivotY="100"
android:pivotX="100"
android:duration="5000"
android:fillBefore="true"
>
</scale>
平移
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0"
android:fromXDelta="0"
android:toXDelta="100"
android:toYDelta="0"
android:fillBefore="true"
android:duration="5000"
>
</translate>
效果