Android 之自定义加载帧动画

uuon9915 7年前
   <p>Android 动画主要分为帧动画和属性动画,主要的区别和关系不在这叙述,这里主要用实际例子说明帧动画的使用。</p>    <p>首先,先来个图看下效果图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/5e255e736730ea2894d1277bc9a0c5a8.png"></p>    <p style="text-align:center">这里写图片描述</p>    <p>点击停止加载 按钮时,加载动画停止,按钮变为开始加载按钮,再次点击,加载动画开始。很简单,下面来看代码实现。</p>    <p>首先,需要有一系列的图片,帧动画嘛,就是跟动画片一样,一帧一帧播放看起来比较流畅。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/54eb3159ed3b97bc32ecdc2452c4d18d.png"></p>    <p style="text-align:center">这里写图片描述</p>    <p>接下来得创建一个xml文件把这些图片放到播放行列里边吧,于是就有了animation的xml文件</p>    <p>没有什么特殊的,就是每个图片持续100ms,挨着来就行了,这些图都是大神做出来的。</p>    <p>下一步,就是应用了,需要在布局文件中,用这个动画:</p>    <pre>  <code class="language-java"><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="开始加载"/></code></pre>    <p>自定义一个progressView,设置背景为刚刚写好的动画文件。看到了,这里使用的是自定义控件,这个自定义控件是继承自Imageview的:</p>    <pre>  <code class="language-java">package com.example.zhaoweiwei.myprogressview;    import android.content.Context;  import android.graphics.drawable.AnimationDrawable;  import android.util.AttributeSet;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()        }     }  }</code></pre>    <p>这里用到了两个方法,一个是startAnim,一个是stopAnim,当该控件可见时,启动动画,否则关闭动画,利用的是AnimationDrawable对象。</p>    <p>然后就在activity里进行逻辑实现就行了:</p>    <pre>  <code class="language-java">@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);   }   }  });   }</code></pre>    <p>代码很简单,主要是一个实现的过程。</p>    <p> </p>    <p>来自:http://www.jianshu.com/p/1e6d377e8e48</p>    <p> </p>