AnimationDrawable-帧动画
帧动画的实现还是比较简单的,使用场景就是在一些需要有动态变化的图标时会用到。
首先需要在drawable下面添加location_loading.xml文件,主要是图片的轮播。
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:duration="350" android:drawable="@drawable/cam_dual_effect_acquiring_gps_1_icn" />
<item android:duration="350" android:drawable="@drawable/cam_dual_effect_acquiring_gps_2_icn" />
<item android:duration="350" android:drawable="@drawable/cam_dual_effect_acquiring_gps_3_icn" />
</animation-list>
我们可以通过两种方法引入这个轮播图片,一个是ImageView的src标签。另一个是progressbar。我们介绍一下progressbar
<ProgressBar
android:id="@+id/location_progressbar"
android:layout_width="20dp"
android:layout_height="20dp"
android:indeterminate="false"
android:indeterminateDrawable="@drawable/location_loading" />
对于使用imageView显示的方法
public void SearchedLocIcon() {
locimg.setImageResource(R.drawable.location_loading_done);
AnimationDrawable anim = (AnimationDrawable) locimg.getDrawable();
anim.start();
}
思路还是很简单的,这个是博主要做的一个相机上的功能的demo。详细代码在下面github上。
public class MainActivity extends AppCompatActivity implements LocationIconCallback.Callback{
private static final String TAG = "MainActivity";
public ImageView locimg;
public boolean status = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locimg = (ImageView) findViewById(R.id.loc_status_icon);
LocationIconCallback.setCallback(this);
}
//点击开始动画
public void onTouchCall(View v) {
if (status == false){
LocationIconCallback.startLocSeachIcon();
status = true;
} else {
LocationIconCallback.SearchedLocIcon();
status = false;
}
}
@Override
public void startLocSeachIcon() {
locimg.setImageResource(R.drawable.location_loading);
AnimationDrawable anim = (AnimationDrawable) locimg.getDrawable();
anim.start();
}
@Override
public void SearchedLocIcon() {
locimg.setImageResource(R.drawable.location_loading_done);
AnimationDrawable anim = (AnimationDrawable) locimg.getDrawable();
anim.start();
}
@Override
public void stopLocIcon() {
}
}
/**
* Created by xiangzaixiansheng on 19-7-2.
*/
public class LocationIconCallback {
private static Callback mCallback;
public interface Callback {
//搜索中动画
void startLocSeachIcon();
//搜索到动画
void SearchedLocIcon();
//关闭动画
void stopLocIcon();
}
public static void startLocSeachIcon(){
mCallback.startLocSeachIcon();
}
public static void SearchedLocIcon() {
mCallback.SearchedLocIcon();
}
public static void stopLocIcon(){
mCallback.stopLocIcon();
}
public static void setCallback(Callback Callback) {
mCallback = Callback;
}
}