Android 之自定義加載幀動畫

uuon9915 8年前發布 | 6K 次閱讀 安卓開發 Android開發 移動開發

Android 動畫主要分為幀動畫和屬性動畫,主要的區別和關系不在這敘述,這里主要用實際例子說明幀動畫的使用。

首先,先來個圖看下效果圖:

這里寫圖片描述

點擊停止加載 按鈕時,加載動畫停止,按鈕變為開始加載按鈕,再次點擊,加載動畫開始。很簡單,下面來看代碼實現。

首先,需要有一系列的圖片,幀動畫嘛,就是跟動畫片一樣,一幀一幀播放看起來比較流暢。

這里寫圖片描述

接下來得創建一個xml文件把這些圖片放到播放行列里邊吧,于是就有了animation的xml文件

沒有什么特殊的,就是每個圖片持續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.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);
 } 
}
}); 
}

代碼很簡單,主要是一個實現的過程。

 

來自:http://www.jianshu.com/p/1e6d377e8e48

 

 本文由用戶 uuon9915 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!