• 5月13日学习日志


    今天我学习了安卓的帧动画。

    首先编写我们的动画文件,非常简单,先在res下创建一个anim目录,接着开始撸我们的 动画文件:miao_gif.xml: 这里的android:oneshot是设置动画是否只是播放一次,true只播放一次,false循环播放!

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false">
        <item
            android:drawable="@mipmap/img_miao1"
            android:duration="80" />
        <item
            android:drawable="@mipmap/img_miao2"
            android:duration="80" />
        <item
            android:drawable="@mipmap/img_miao3"
            android:duration="80" />
        <!--限于篇幅,省略其他item,自己补上-->
        ...
    </animation-list>

    动画文件有了,接着到我们的布局文件:activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/btn_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开始" />
    
        <Button
            android:id="@+id/btn_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止" />
    
        <ImageView
            android:id="@+id/img_show"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_gravity="center"
            android:background="@anim/miao_gif" />
        
    </LinearLayout>

    最后是我们的MainActivity.java,这里在这里控制动画的开始以及暂停:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private Button btn_start;
        private Button btn_stop;
        private ImageView img_show;
        private AnimationDrawable anim;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            bindViews();
            anim = (AnimationDrawable) img_show.getBackground();
        }
    
        private void bindViews() {
            btn_start = (Button) findViewById(R.id.btn_start);
            btn_stop = (Button) findViewById(R.id.btn_stop);
            img_show = (ImageView) findViewById(R.id.img_show);
            btn_start.setOnClickListener(this);
            btn_stop.setOnClickListener(this);
        }
        
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_start:
                    anim.start();
                    break;
                case R.id.btn_stop:
                    anim.stop();
                    break;
            }
        }
    }
  • 相关阅读:
    ConcurrentHashMap实现原理--转载
    java原子操作的实现原理--转载
    Catch Me If You ... Can't Do Otherwise--转载
    微框架spark--api开发利器
    Exploring Micro-frameworks: Spring Boot--转载
    简约之美jodd--props属性使用
    jodd-cache集锦
    java根据url获取完整域名
    Jodd-vtor验证框架
    倒排列表求交集算法 包括baeza yates的交集算法
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910628.html
Copyright © 2020-2023  润新知