android笔记--加载框
package com.fuda.ui; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView; import java.util.LinkedList; import com.fuda.R; public class MessageBar { public interface OnMessageClickListener { void onMessageClick(Parcelable token);
} private static final String STATE_MESSAGES = "net.simonvt.messagebar.MessageBar.messages";
private static final String STATE_CURRENT_MESSAGE = "net.simonvt.messagebar.MessageBar.currentMessage"; private static final int ANIMATION_DURATION = 600; private static final int HIDE_DELAY = 5000; private View mContainer; private TextView mTextView; private TextView mButton; private LinkedList<Message> mMessages = new LinkedList<Message>(); private Message mCurrentMessage; private boolean mShowing; private OnMessageClickListener mClickListener; private Handler mHandler; private AlphaAnimation mFadeInAnimation; private AlphaAnimation mFadeOutAnimation; public MessageBar(Activity activity) {
ViewGroup container = (ViewGroup) activity.findViewById(android.R.id.content);
View v = activity.getLayoutInflater().inflate(R.layout.mb__messagebar, container);
init(v);
} public MessageBar(View v) {
init(v);
} private void init(View v) {
mContainer = v.findViewById(R.id.mbContainer);
mContainer.setVisibility(View.GONE);
mTextView = (TextView) v.findViewById(R.id.mbMessage);
mButton = (TextView) v.findViewById(R.id.mbButton);
mButton.setOnClickListener(mButtonListener); mFadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
mFadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
mFadeOutAnimation.setDuration(ANIMATION_DURATION);
mFadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
} @Override
public void onAnimationEnd(Animation animation) {
Message nextMessage = mMessages.poll(); if (nextMessage != null) {
show(nextMessage);
} else {
mCurrentMessage = null;
mContainer.setVisibility(View.GONE);
mShowing = false;
}
} @Override
public void onAnimationRepeat(Animation animation) {
}
}); mHandler = new Handler();
} public void show(String message) {
show(message, null);
} public void show(String message, String actionMessage) {
show(message, actionMessage, 0);
} public void show(String message, String actionMessage, int actionIcon) {
show(message, actionMessage, actionIcon, null);
} public void show(String message, String actionMessage, int actionIcon, Parcelable token) {
Message m = new Message(message, actionMessage, actionIcon, token);
if (mShowing) {
mMessages.add(m);
} else {
show(m);
}
} private void show(Message message) {
show(message, false);
} private void show(Message message, boolean immediately) {
mShowing = true;
mContainer.setVisibility(View.VISIBLE);
mCurrentMessage = message;
mTextView.setText(message.mMessage);
if (message.mActionMessage != null) {
mTextView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
mButton.setVisibility(View.VISIBLE);
mButton.setText(message.mActionMessage); mButton.setCompoundDrawablesWithIntrinsicBounds(message.mActionIcon, 0, 0, 0);
} else {
mTextView.setGravity(Gravity.CENTER);
mButton.setVisibility(View.GONE);
} if (immediately) {
mFadeInAnimation.setDuration(0);
} else {
mFadeInAnimation.setDuration(ANIMATION_DURATION);
}
mContainer.startAnimation(mFadeInAnimation);
mHandler.postDelayed(mHideRunnable, HIDE_DELAY);
} private final View.OnClickListener mButtonListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mClickListener != null && mCurrentMessage != null) {
mClickListener.onMessageClick(mCurrentMessage.mToken);
mCurrentMessage = null;
mHandler.removeCallbacks(mHideRunnable);
mHideRunnable.run();
}
}
}; public void setOnClickListener(OnMessageClickListener listener) {
mClickListener = listener;
} public void clear() {
mMessages.clear();
mHideRunnable.run();
} private final Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
mContainer.startAnimation(mFadeOutAnimation);
}
}; public void onRestoreInstanceState(Bundle state) {
Message currentMessage = state.getParcelable(STATE_CURRENT_MESSAGE);
if (currentMessage != null) {
show(currentMessage, true);
Parcelable[] messages = state.getParcelableArray(STATE_MESSAGES);
for (Parcelable p : messages) {
mMessages.add((Message) p);
}
}
} public Bundle onSaveInstanceState() {
Bundle b = new Bundle(); b.putParcelable(STATE_CURRENT_MESSAGE, mCurrentMessage); final int count = mMessages.size();
final Message[] messages = new Message[count];
int i = 0;
for (Message message : mMessages) {
messages[i++] = message;
} b.putParcelableArray(STATE_MESSAGES, messages); return b;
} /**
*
* @author Administrator
* http://www.2cto.com/kf/201205/132814.html
*/
private static class Message implements Parcelable { final String mMessage; final String mActionMessage; final int mActionIcon; final Parcelable mToken; public Message(String message, String actionMessage, int actionIcon, Parcelable token) {
mMessage = message;
mActionMessage = actionMessage;
mActionIcon = actionIcon;
mToken = token;
} public Message(Parcel p) {
mMessage = p.readString();
mActionMessage = p.readString();
mActionIcon = p.readInt();
mToken = p.readParcelable(null);
} public void writeToParcel(Parcel out, int flags) {
out.writeString(mMessage);
out.writeString(mActionMessage);
out.writeInt(mActionIcon);
out.writeParcelable(mToken, 0);
} public int describeContents() {
return 0;
} public static final Parcelable.Creator<Message> CREATOR = new Parcelable.Creator<Message>() {
public Message createFromParcel(Parcel in) {
return new Message(in);
} public Message[] newArray(int size) {
return new Message[size];
}
};
}
}
//调用
private MessageBar mMessageBar;
public void showMessage(String message) {
mMessageBar.show(message, "关闭", R.drawable.ic_messagebar_undo, new Bundle());
}
android笔记--加载框的更多相关文章
- ios新手开发——toast提示和旋转图片加载框
不知不觉自学ios已经四个月了,从OC语法到app开发,过程虽然枯燥无味,但是结果还是挺有成就感的,在此分享我的ios开发之路中的小小心得~废话不多说,先上我们今天要实现的效果图: 有过一点做APP经 ...
- Android--自定义加载框
1,在网上看了下好看的加载框,看了一下,挺好看的,再看了下源码,就是纯paint画出来的,再加上属性动画就搞定了 再来看一下我们的源码 LvGhost.java package com.qianmo. ...
- 使用Dialog实现全局Loading加载框
Dialog实现全局Loading加载框 很多人在实现Loading加载框的时候,都是在当前的页面隐藏一个Loading布局,需要加载的时候,显示出来,加载完再隐藏 使用Dialog实现Loading ...
- mui---取消掉默认加载框
我们在进行打开页面新页面的时候,在APP中会在中间有一个加载框,考虑到用户体验,要取消掉,具体方法是,对openWindow进行配置: 具体参考:http://dev.dcloud.net.cn/mu ...
- Android图片加载框架最全解析(七),实现带进度的Glide图片加载功能
我们的Glide系列文章终于要进入收尾篇了.从我开始写这个系列的第一篇文章时,我就知道这会是一个很长的系列,只是没有想到竟然会写这么久. 在前面的六篇文章中,我们对Glide的方方面面都进行了学习,包 ...
- 加载框(loading)
一般在用户提交数据或者新加载页面时,请求服务器的过程,页面没有响应,但是用户并不知道,此时在发生什么.这时,就需要loading框给用户提示,增加用户体验. 1.引入loading.css. html ...
- Android ListView加载更多
先看效果: ListView的footer布局: <?xml version="1.0" encoding="utf-8"?> <Relati ...
- [转载] Android动态加载Dex机制解析
本文转载自: http://blog.csdn.net/wy353208214/article/details/50859422 1.什么是类加载器? 类加载器(class loader)是 Java ...
- Android图片加载库的理解
前言 这是“基础自测”系列的第三篇文章,以Android开发需要熟悉的20个技术点为切入点,本篇重点讲讲Android中的ImageLoader这个库的一些理解,在Android上最让人头疼是 ...
随机推荐
- <转>SQL 左外连接,右外连接,全连接,内连接
本文节选自:https://www.cnblogs.com/youzhangjin/archive/2009/05/22/1486982.html 连接条件可在FROM或WHERE子句中指 ...
- JAVA中线程池的简单使用
比如现在有10个线程,但每次只想运行3个线程,当这3个线程中的任何一个运行完后,第4个线程接着补上.这种情况可以使用线程池来解决,线程池用起来也相当的简单,不信,你看: package com.dem ...
- php 执行命令函数
/** Method to execute a command in the terminal Uses : 1. system 2. passthru 3. exec 4. shell_exec * ...
- java string截取两个字符串之间的值
java string截取两个字符串之间的值 import java.util.regex.Matcher; import java.util.regex.Pattern; public class ...
- Linux CentOS 7.x/6.x/5.x 导入epel源
How to Enable EPEL Repository for RHEL/CentOS 7.x/6.x/5.x vim /etc/yum.repos.d/CentOS-Base.repo 取消注释 ...
- git使一个非仓库型的工程可以推送
git config receive.denycurrentbranch false
- Innodb后台线程
1.maste thread 负责将缓冲池中的数据异步刷新到磁盘,保证数据的一致性. 2.IO Thread负责IO请求的回调处理.1.0版本之前有4个IO Thread,负责write.read.i ...
- org.hibernate.exception.ConstraintViolationException: could not insert:
org.hibernate.exception.ConstraintViolationException: could not insert: 报错原由于xxx.hbm.xml文件里的主键类型设置有问 ...
- SharePoint 2010、2013多个域之间互信(Domain Trust)的设计与实施
在现实的业务场景中,有时为了更好的管理域用户和服务.我们往往会创建多个分散式的域,每个域的Administrator专注于维护特定域中的用户和资源,Administrator也可以定义安全策略,比如账 ...
- javascript的事件机制(百度文库)
http://wenku.baidu.com/view/9c8761e1524de518964b7d65.html http://wenku.baidu.com/view/1c3d7228bd6478 ...