Android实现仿慕课网下拉加载动画

 更新时间:2015年07月23日 15:29:01   作者:掏富小牛  
这篇文章是我在做动画的项目中整理出来的,在eoe看了篇帖子,然后仿慕课网做了一个下拉加载动画。此功能实现方法是AnimationDrawable类进行 Animation-list中item的循环遍历图片,类似于flash里的帧帧动画,需要的朋友可以参考下

具体实现方法就不多介绍了先附上源码,相信大家都容易看的懂:

这里为了让这个动画效果可被复用,于是就继承了ImageView 去实现某些方法

 

package com.example.loading_drawable;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;

public class MyImgView extends ImageView {
// 动画图层类
private AnimationDrawable bg_anim;

public MyImgView(Context context) {
super(context, null);
initView();
}

public MyImgView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}

public MyImgView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

}
  //初始化
private void initView() {
setBackgroundResource(R.drawable.flash_anim);
bg_anim = (AnimationDrawable) getBackground();
Log.i("AAA", "iniView");
}

/**
* 开启动画效果
*/
public void startAnim() {
if (bg_anim != null) {
 bg_anim.start();
}
}

/**
* 停止动画效果
*/
public void stopAnim() {
if (bg_anim != null && bg_anim.isRunning()) {
 bg_anim.stop();
}
}

/*
* (non-Javadoc)
* 
* @see android.widget.ImageView#setVisibility(int) 当控件被显示时就调用 开启动画效果,反之
*/
@Override
public void setVisibility(int visibility) {
super.setVisibility(visibility);
if (visibility == View.VISIBLE) {
 startAnim();
} else {
 stopAnim();
}
}

}

 接下来就是:在res文件夹下新建 drawable文件夹,再此文件夹下新建 flash_anim.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/a01_02" android:duration="50"/>
<item android:drawable="@drawable/a01_04" android:duration="50"/>
<item android:drawable="@drawable/a01_06" android:duration="50"/>
<item android:drawable="@drawable/a01_08" android:duration="50"/>
<item android:drawable="@drawable/a01_10" android:duration="50"/>
<item android:drawable="@drawable/a01_12" android:duration="50"/>
<item android:drawable="@drawable/a01_14" android:duration="50"/>
<item android:drawable="@drawable/a01_16" android:duration="50"/>
<item android:drawable="@drawable/a01_25" android:duration="50"/>
<item android:drawable="@drawable/a01_26" android:duration="50"/>
<item android:drawable="@drawable/a01_27" android:duration="50"/>
<item android:drawable="@drawable/a01_28" android:duration="50"/>
<item android:drawable="@drawable/a01_30" android:duration="50"/>
<item android:drawable="@drawable/a01_31" android:duration="50"/>
<item android:drawable="@drawable/a01_32" android:duration="50"/>
<item android:drawable="@drawable/a01_41" android:duration="50"/>
<item android:drawable="@drawable/a01_42" android:duration="50"/>
<item android:drawable="@drawable/a01_43" android:duration="50"/>
<item android:drawable="@drawable/a01_44" android:duration="50"/>
<item android:drawable="@drawable/a01_45" android:duration="50"/>
<item android:drawable="@drawable/a01_46" android:duration="50"/>
<item android:drawable="@drawable/a01_47" android:duration="50"/>
<item android:drawable="@drawable/a01_48" android:duration="50"/>
<item android:drawable="@drawable/a01_57" android:duration="50"/>
<item android:drawable="@drawable/a01_58" android:duration="50"/>
<item android:drawable="@drawable/a01_59" android:duration="50"/>
<item android:drawable="@drawable/a01_60" android:duration="50"/>
<item android:drawable="@drawable/a01_61" android:duration="50"/>
<item android:drawable="@drawable/a01_62" android:duration="50"/>
<item android:drawable="@drawable/a01_63" android:duration="50"/>
<item android:drawable="@drawable/a01_64" android:duration="50"/>
</animation-list>

这样就基本搞定了,接下来就要在main中调用自定义的main就可以;如下:

package com.example.loading_drawable;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

/**
* @author Administrator 慕课网下拉刷新进度显示控件
* 
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 LinearLayout rootLayout = new LinearLayout(this);
 rootLayout.setOrientation(LinearLayout.VERTICAL);
 rootLayout.setLayoutParams(new LinearLayout.LayoutParams(
   LinearLayout.LayoutParams.MATCH_PARENT,
   LinearLayout.LayoutParams.MATCH_PARENT));
 rootLayout.setGravity(Gravity.CENTER);

 Button btn = new Button(this);
 btn.setText("展现动画");

 final MyImgView imgView = new MyImgView(MainActivity.this);
 imgView.setLayoutParams(new LinearLayout.LayoutParams(
   LinearLayout.LayoutParams.WRAP_CONTENT,
   LinearLayout.LayoutParams.WRAP_CONTENT));
 imgView.setVisibility(View.GONE);

 rootLayout.addView(btn);
 rootLayout.addView(imgView);

 setContentView(rootLayout);

 btn.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View arg0) {
   imgView.setVisibility(View.VISIBLE);
  }
 });
}
}

这里是用自定义代码布局文件做的,布局方便,插件代码整合,如上所述,这个动画就完成了,只在需要的地方设置imgview为显示,动画就会开启,隐藏动画就会被关闭。

具体内容到此为止,希望大家能够喜欢。

相关文章

  • android使用viewpager计算偏移量实现选项卡功能

    android使用viewpager计算偏移量实现选项卡功能

    这篇文章主要为大家详细介绍了android使用viewpager计算偏移量实现选项卡功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Android消息通知Notification常用方法(发送消息和接收消息)

    Android消息通知Notification常用方法(发送消息和接收消息)

    最近在做消息通知类Notification的相关业务,利用闲暇时间总结一下,主要分为两部分来记录:发送消息和接收消息,对Android消息通知相关知识感兴趣的朋友一起看看吧
    2024-02-02
  • Android App开发的自动化测试框架UI Automator使用教程

    Android App开发的自动化测试框架UI Automator使用教程

    UI Automator为Android程序的UI开发提供了测试环境,这里我们就来看一下Android App开发的自动化测试框架UI Automator使用教程,需要的朋友可以参考下
    2016-07-07
  • Android连接MySQL数据库并进行增删改查操作示例讲解

    Android连接MySQL数据库并进行增删改查操作示例讲解

    这篇文章主要介绍了Android 连接MySQL数据库并进行增删改查操作示例讲解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Android 通过cmake的方式接入opencv的方法步骤

    Android 通过cmake的方式接入opencv的方法步骤

    这篇文章主要介绍了Android 通过cmake的方式接入opencv的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Android Studio Menu选择菜单的建立方法

    Android Studio Menu选择菜单的建立方法

    这篇文章主要介绍了Android Studio Menu选择菜单的建立,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • android自定义左侧滑出菜单效果

    android自定义左侧滑出菜单效果

    这篇文章主要为大家详细介绍了android自定义左侧滑出菜单效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • 使用Flutter定位包获取地理位置

    使用Flutter定位包获取地理位置

    本文详细讲解了使用Flutter定位包获取地理位置的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-11-11
  • Android实现好看的微信聊天气泡效果

    Android实现好看的微信聊天气泡效果

    在聊天类应用中,通常用气泡作为聊天内容的背景色,比如微信的聊天背景,别人发过来的是白色的气泡,自己发的是绿色的气泡。本文将用Android实现好看的微信聊天气泡效果,感兴趣的可以了解一下
    2022-06-06
  • Android自定义View实现圆形进度条

    Android自定义View实现圆形进度条

    这篇文章主要为大家详细介绍了Android自定义View实现圆形进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06

最新评论