Android 之自定义加载帧动画

来源:互联网 发布:caffe matlab 编译 编辑:程序博客网 时间:2024/05/21 10:18

android 动画主要分为帧动画和属性动画,主要的区别和关系不在这叙述,这里主要用实际例子说明帧动画的使用。

首先,先来个图看下效果图:
这里写图片描述

点击停止加载 按钮时,加载动画停止,按钮变为开始加载按钮,再次点击,加载动画开始。很简单,下面来看代码实现。

首先,需要有一系列的图片,帧动画嘛,就是跟动画片一样,一帧一帧播放看起来比较流畅。
这里写图片描述

接下来得创建一个xml文件把这些图片放到播放行列里边吧,于是就有了animation的xml文件

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"                android:oneshot="false">    <item  android:drawable="@drawable/progress_loading_001" android:duration="100"/>    <item  android:drawable="@drawable/progress_loading_002" android:duration="100"/>    <item  android:drawable="@drawable/progress_loading_003" android:duration="100"/>    <item  android:drawable="@drawable/progress_loading_004" android:duration="100"/>    <item  android:drawable="@drawable/progress_loading_005" android:duration="100"/>    <item  android:drawable="@drawable/progress_loading_006" android:duration="100"/>    <item  android:drawable="@drawable/progress_loading_007" android:duration="100"/>    <item  android:drawable="@drawable/progress_loading_008" android:duration="100"/>    <item  android:drawable="@drawable/progress_loading_009" android:duration="100"/>    <item  android:drawable="@drawable/progress_loading_0010" android:duration="100"/>    <item  android:drawable="@drawable/progress_loading_0011" android:duration="100"/>    <item  android:drawable="@drawable/progress_loading_0012" android:duration="100"/></animation-list>

没有什么特殊的,就是每个图片持续100ms,挨着来就行了,这些图都是大神做出来的。

下一步,就是应用了,需要在布局文件中,用这个动画:

<com.example.zhaoweiwei.myprogressview.ProgressView        android:id="@+id/progressview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:visibility="gone"        android:background="@drawable/progress_animation"/>    <Button        android:id="@+id/button_progressview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/progressview"        android:layout_centerInParent="true"        android:text="开始加载"/>

自定义一个progressView,设置背景为刚刚写好的动画文件。看到了,这里使用的是自定义控件,这个自定义控件是继承自Imageview的:

package com.example.zhaoweiwei.myprogressview;import android.content.Context;import android.graphics.drawable.AnimationDrawable;import android.util.AttributeSet;import android.view.View;import android.widget.ImageView;/** * Created by zhaoweiwei on 2016/11/1. */public class ProgressView extends ImageView {    private AnimationDrawable animationDrawable;    public ProgressView(Context context) {        super(context);        initView();    }    public ProgressView(Context context, AttributeSet attrs) {        super(context, attrs);        initView();    }    public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initView();    }    private void initView() {        animationDrawable = (AnimationDrawable) getBackground();    }    @Override    public void setVisibility(int visibility) {        if (getVisibility() != visibility) {            super.setVisibility(visibility);            if (visibility == GONE || visibility == INVISIBLE) {                stopAnim();            } else {                startAnim();            }        }    }    private void startAnim() {        if (animationDrawable == null) {            initView();        }        if (animationDrawable != null) {            animationDrawable.start();        }    }    private void stopAnim() {        if (animationDrawable == null) {            initView();        }        if (animationDrawable != null) {            animationDrawable.stop();        }    }}

这里用到了两个方法,一个是startAnim,一个是stopAnim,当该控件可见时,启动动画,否则关闭动画,利用的是AnimationDrawable对象。

然后就在activity里进行逻辑实现就行了:

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        progressView = (ProgressView) findViewById(R.id.progressview);        button = (Button) findViewById(R.id.button_progressview);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (VISIBLE == progressView.getVisibility()) {                    button.setText("开始加载");                    progressView.setVisibility(INVISIBLE);                } else {                    button.setText("停止加载");                    progressView.setVisibility(VISIBLE);                }            }        });    }

代码很简单,主要是一个实现的过程。

0 0
原创粉丝点击