Android图片动画播放
16lz
2021-01-26
这个例子是一帧一帧的播放20张图片,通过两个按钮控制播放的开始和停止
frame.xml文件
- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageViewandroid:id="@+id/imageview"
- android:layout_width="200px"
- android:layout_height="200px"
- android:src="@drawable/anim_01"
- android:scaleType="fitCenter"
- android:layout_centerHorizontal="true"
- android:background="#ffffff"/>
- <Buttonandroid:id="@+id/start"
- android:text="开始动画"
- android:layout_below="@id/imageview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <Buttonandroid:id="@+id/stop"
- android:text="结束动画"
- android:layout_below="@id/start"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </RelativeLayout>
其中,ImageView中的android:scaleType属性(ImageView.setScaleType(ImageView.ScaleType))说明:
CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示 FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片不按比例
- publicclassFrameActivityextendsActivity{
- privateImageViewimageView;
- privateButtonstart;
- privateButtonstop;
- privateAnimationDrawableanimationDrawable;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.frame);
- imageView=(ImageView)findViewById(R.id.imageview);
- start=(Button)findViewById(R.id.start);
- start.setOnClickListener(start_listener);
- stop=(Button)findViewById(R.id.stop);
- stop.setOnClickListener(stop_listener);
- animationDrawable=newAnimationDrawable();
- //添加每一帧动画
- for(inti=1;i<=20;i++){
- intid=getResources().getIdentifier(
- "anim"+(i>9?"_":"_0")+i,"drawable","com.will.frame");
- animationDrawable.addFrame(getResources().getDrawable(id),150);
- }
- //设置手否重复播放,false为重复
- animationDrawable.setOneShot(false);
- imageView.setImageDrawable(animationDrawable);
- }
- //开始播放
- Button.OnClickListenerstart_listener=newButton.OnClickListener(){
- @Override
- publicvoidonClick(Viewarg0){
- animationDrawable.start();
- }
- };
- //停止播放
- Button.OnClickListenerstop_listener=newButton.OnClickListener(){
- @Override
- publicvoidonClick(Viewarg0){
- animationDrawable.stop();
- }
- };
- }
更多相关文章
- Android学习笔记10——Android的调试
- Android(安卓)PreferenceActivity简介
- drawable(hdpi,ldpi,mdpi)的区别
- Android实现一行有好几个TextView 第一个TextView使用省略
- Bitmap.Options和LruCache——Android高效加载大图、多图解决方
- Picasso源码分析
- Android(安卓)背景图片重复 Background repeat
- Android获取视频文件某一帧并设置图片
- Android(安卓)Toast 部分手机无法显示问题(如小米手机)解决