Android的帧动画补间动画,新手复习

前几天使用到了,顺便自己也复习下。

MainActivity.java的代码

public class MainActivity extends Activity implements OnClickListener{

private Button btnZhen;
private Button btnBuJian;
private Button btnGif;
private ImageView iv = null;
private ImageView ivBuJian = null;
private AnimationDrawable ad = null;//动画绘制对象

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


btnZhen=(Button) findViewById(R.id.btnZhen);
btnZhen.setOnClickListener(this);
btnBuJian=(Button) findViewById(R.id.btnBuJian);
btnBuJian.setOnClickListener(this);

btnGif=(Button) findViewById(R.id.btnGif);
btnGif.setOnClickListener(this);
//帧动画
iv=(ImageView) findViewById(R.id.ivZhen);//绑定
iv.setBackgroundResource(R.drawable.myzhendonghua);//设置背景资源
ad = (AnimationDrawable) iv.getBackground();//动画读取到背景

//补间动画
ivBuJian=(ImageView) findViewById(R.id.ivBuJian);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnZhen:
ad.start();

break;
case R.id.btnGif:
Intent i = new Intent();
i.setClassName(MainActivity.this, "com.example.y2_android_ch8_kehou.GifViewActivity");
startActivity(i);
break;
case R.id.btnBuJian:
AnimationSet set = new AnimationSet(true);
//平移
/*TranslateAnimation ani = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, 
Animation.RELATIVE_TO_SELF, 0.5f, 
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
ani.setDuration(1000);*/
//透明度
AlphaAnimation ani1 = new AlphaAnimation(1.0f, 0.2f);
ani1.setDuration(1200);
ani1.setRepeatCount(5);
ani1.setRepeatMode(Animation.RESTART);
//缩放
/* ScaleAnimation ani2 = new ScaleAnimation(0.5f, 2.0f, 0.5f, 2.0f, 
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.0f);
ani2.setDuration(200);
ani2.setRepeatCount(3);
ani2.setRepeatMode(Animation.RESTART);*/
//旋转
RotateAnimation ani3 = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
ani3.setDuration(800);
ani3.setRepeatCount(5);
ani3.setRepeatMode(Animation.RESTART);
/*set.addAnimation(ani);*/
set.addAnimation(ani1);
/*set.addAnimation(ani2);*/
set.addAnimation(ani3);
ivBuJian.startAnimation(set);
break;
default:
break;
}
}
}


另外的再放上GifViewActivity.java的代码


public class GifViewActivity extends Activity {


// 借助Movie实现 播放gif格式图
private Movie mMovie;
private long mMovieStart;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_gif_view);
setContentView(new CustomGifView(this));


}


class CustomGifView extends View {
public CustomGifView(Context context) {
super(context);
mMovie = Movie.decodeStream(getResources().openRawResource(
R.drawable.gundong));
}


public void onDraw(Canvas canvas) {
long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}
if (mMovie != null) {
int dur = mMovie.duration();
if (dur == 0) {
dur = 1000;
}
int relTime = (int) ((now - mMovieStart) % dur);
mMovie.setTime(relTime);
mMovie.draw(canvas, 0, 0);
invalidate();
}
};
}
}


 

网盘地址:

http://pan.baidu.com/s/17A98E

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值