1.日志工具类--》Log.java
package com.zhy.utils;
import android.util.Log;
/**
* Log统一管理类
*
*
*
*/
public class L
{
private L()
{
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
public static boolean isDebug = true;// 是否需要打印bug,可以在application的onCreate函数里面初始化
private static final String TAG = "way";
// 下面四个是默认tag的函数
public static void i(String msg)
{
if (isDebug)
Log.i(TAG, msg);
}
public static void d(String msg)
{
if (isDebug)
Log.d(TAG, msg);
}
public static void e(String msg)
{
if (isDebug)
Log.e(TAG, msg);
}
public static void v(String msg)
{
if (isDebug)
Log.v(TAG, msg);
}
// 下面是传入自定义tag的函数
public static void i(String tag, String msg)
{
if (isDebug)
Log.i(tag, msg);
}
public static void d(String tag, String msg)
{
if (isDebug)
Log.i(tag, msg);
}
public static void e(String tag, String msg)
{
if (isDebug)
Log.i(tag, msg);
}
public static void v(String tag, String msg)
{
if (isDebug)
Log.i(tag, msg);
}
}
package com.zhy.utils;
import android.content.Context;
import android.widget.Toast;
/**
* Toast统一管理类
*
*/
public class T
{
private T()
{
/** cannot be instantiated**/
throw new UnsupportedOperationException("cannot be instantiated");
}
public static boolean isShow = true;
/**
* 短时间显示Toast
*
* @param context
* @param message
*/
public static void showShort(Context context, CharSequence message)
{
if (isShow)
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
/**
* 短时间显示Toast
*
* @param context
* @param message
*/
public static void showShort(Context context, int message)
{
if (isShow)
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
/**
* 长时间显示Toast
*
* @param context
* @param message
*/
public static void showLong(Context context, CharSequence message)
{
if (isShow)
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
/**
* 长时间显示Toast
*
* @param context
* @param message
*/
public static void showLong(Context context, int message)
{
if (isShow)
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
/**
* 自定义显示Toast时间
*
* @param context
* @param message
* @param duration
*/
public static void show(Context context, CharSequence message, int duration)
{
if (isShow)
Toast.makeText(context, message, duration).show();
}
/**
* 自定义显示Toast时间
*
* @param context
* @param message
* @param duration
*/
public static void show(Context context, int message, int duration)
{
if (isShow)
Toast.makeText(context, message, duration).show();
}
}
package com.zhy.utils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
public class SPUtils
{
/**
* 保存在手机里面的文件名
*/
public static final String FILE_NAME = "share_data";
/**
* 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
*
* @param context
* @param key
* @param object
*/
public static void put(Context context, String key, Object object)
{
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if (object instanceof String)
{
editor.putString(key, (String) object);
} else if (object instanceof Integer)
{
editor.putInt(key, (Integer) object);
} else if (object instanceof Boolean)
{
editor.putBoolean(key, (Boolean) object);
} else if (object instanceof Float)
{
editor.putFloat(key, (Float) object);
} else if (object instanceof Long)
{
editor.putLong(key, (Long) object);
} else
{
editor.putString(key, object.toString());
}
SharedPreferencesCompat.apply(editor);
}
/**
* 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
*
* @param context
* @param key
* @param defaultObject
* @return
*/
public static Object get(Context context, String key, Object defaultObject)
{
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
if (defaultObject instanceof String)
{
return sp.getString(key, (String) defaultObject);
} else if (defaultObject instanceof Integer)
{
return sp.getInt(key, (Integer) defaultObject);
} else if (defaultObject instanceof Boolean)
{
return sp.getBoolean(key, (Boolean) defaultObject);
} else if (defaultObject instanceof Float)
{
return sp.getFloat(key, (Float) defaultObject);
} else if (defaultObject instanceof Long)
{
return sp.getLong(key, (Long) defaultObject);
}
return null;
}
/**
* 移除某个key值已经对应的值
* @param context
* @param key
*/
public static void remove(Context context, String key)
{
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.remove(key);
SharedPreferencesCompat.apply(editor);
}
/**
* 清除所有数据
* @param context
*/
public static void clear(Context context)
{
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.clear();
SharedPreferencesCompat.apply(editor);
}
/**
* 查询某个key是否已经存在
* @param context
* @param key
* @return
*/
public static boolean contains(Context context, String key)
{
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
return sp.contains(key);
}
/**
* 返回所有的键值对
*
* @param context
* @return
*/
public static Map getAll(Context context)
{
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
return sp.getAll();
}
/**
* 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
*
* @author zhy
*
*/
private static class SharedPreferencesCompat
{
private static final Method sApplyMethod = findApplyMethod();
/**
* 反射查找apply的方法
*
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Method findApplyMethod()
{
try
{
Class clz = SharedPreferences.Editor.class;
return clz.getMethod("apply");
} catch (NoSuchMethodException e)
{
}
return null;
}
/**
* 如果找到则使用apply执行,否则使用commit
*
* @param editor
*/
public static void apply(SharedPreferences.Editor editor)
{
try
{
if (sApplyMethod != null)
{
sApplyMethod.invoke(editor);
return;
}
} catch (IllegalArgumentException e)
{
} catch (IllegalAccessException e)
{
} catch (InvocationTargetException e)
{
}
editor.commit();
}
}
}
package com.zhy.utils;
import android.content.Context;
import android.util.TypedValue;
/**
* 常用单位转换的辅助类
*
*
*
*/
public class DensityUtils
{
private DensityUtils()
{
/** cannot be instantiated **/
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* dp转px
*
* @param context
* @param val
* @return
*/
public static int dp2px(Context context, float dpVal)
{
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}
/**
* sp转px
*
* @param context
* @param val
* @return
*/
public static int sp2px(Context context, float spVal)
{
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, context.getResources().getDisplayMetrics());
}
/**
* px转dp
*
* @param context
* @param pxVal
* @return
*/
public static float px2dp(Context context, float pxVal)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (pxVal / scale);
}
/**
* px转sp
*
* @param fontScale
* @param pxVal
* @return
*/
public static float px2sp(Context context, float pxVal)
{
return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
}
}
package com.zhy.utils;
import java.io.File;
import android.os.Environment;
import android.os.StatFs;
/**
* SD卡相关的辅助类
*
*
*
*/
public class SDCardUtils
{
private SDCardUtils()
{
/** cannot be instantiated **/
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 判断SDCard是否可用
*
* @return
*/
public static boolean isSDCardEnable()
{
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}
/**
* 获取SD卡路径
*
* @return
*/
public static String getSDCardPath()
{
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator;
}
/**
* 获取SD卡的剩余容量 单位byte
*
* @return
*/
public static long getSDCardAllSize()
{
if (isSDCardEnable())
{
StatFs stat = new StatFs(getSDCardPath());
// 获取空闲的数据块的数量
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
// 获取单个数据块的大小(byte)
long freeBlocks = stat.getAvailableBlocks();
return freeBlocks * availableBlocks;
}
return 0;
}
/**
* 获取指定路径所在空间的剩余可用容量字节数,单位byte
*
* @param filePath
* @return 容量字节 SDCard可用空间,内部存储可用空间
*/
public static long getFreeBytes(String filePath)
{
// 如果是sd卡的下的路径,则获取sd卡可用容量
if (filePath.startsWith(getSDCardPath()))
{
filePath = getSDCardPath();
} else
{// 如果是内部存储的路径,则获取内存存储的可用容量
filePath = Environment.getDataDirectory().getAbsolutePath();
}
StatFs stat = new StatFs(filePath);
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
return stat.getBlockSize() * availableBlocks;
}
/**
* 获取系统存储路径
*
* @return
*/
public static String getRootDirectoryPath()
{
return Environment.getRootDirectory().getAbsolutePath();
}
}
package com.zhy.utils;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
/**
* 获得屏幕相关的辅助类
*
*
*
*/
public class ScreenUtils
{
private ScreenUtils()
{
/** cannot be instantiated **/
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 获得屏幕高度
*
* @param context
* @return
*/
public static int getScreenWidth(Context context)
{
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
/**
* 获得屏幕宽度
*
* @param context
* @return
*/
public static int getScreenHeight(Context context)
{
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}
/**
* 获得状态栏的高度
*
* @param context
* @return
*/
public static int getStatusHeight(Context context)
{
int statusHeight = -1;
try
{
Class> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
statusHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e)
{
e.printStackTrace();
}
return statusHeight;
}
/**
* 获取当前屏幕截图,包含状态栏
*
* @param activity
* @return
*/
public static Bitmap snapShotWithStatusBar(Activity activity)
{
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
view.destroyDrawingCache();
return bp;
}
/**
* 获取当前屏幕截图,不包含状态栏
*
* @param activity
* @return
*/
public static Bitmap snapShotWithoutStatusBar(Activity activity)
{
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return bp;
}
}
package com.zhy.utils;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
/**
* 跟App相关的辅助类
*
*
*
*/
public class AppUtils
{
private AppUtils()
{
/**cannot be instantiated **/
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 获取应用程序名称
*/
public static String getAppName(Context context)
{
try
{
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
int labelRes = packageInfo.applicationInfo.labelRes;
return context.getResources().getString(labelRes);
} catch (NameNotFoundException e)
{
e.printStackTrace();
}
return null;
}
/**
* [获取应用程序版本名称信息]
*
* @param context
* @return 当前应用的版本名称
*/
public static String getVersionName(Context context)
{
try
{
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
return packageInfo.versionName;
} catch (NameNotFoundException e)
{
e.printStackTrace();
}
return null;
}
}
package com.zhy.utils;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* 打开或关闭软键盘
*
* @author zhy
*
*/
public class KeyBoardUtils
{
/**
* 打卡软键盘
*
* @param mEditText
* 输入框
* @param mContext
* 上下文
*/
public static void openKeybord(EditText mEditText, Context mContext)
{
InputMethodManager imm = (InputMethodManager) mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
}
/**
* 关闭软键盘
*
* @param mEditText
* 输入框
* @param mContext
* 上下文
*/
public static void closeKeybord(EditText mEditText, Context mContext)
{
InputMethodManager imm = (InputMethodManager) mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}
}
9.网络相关工具类,NetUtils.java
package com.zhy.utils;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* 跟网络相关的工具类
*
*
*
*/
public class NetUtils
{
private NetUtils()
{
/** cannot be instantiated **/
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 判断网络是否连接
*
* @param context
* @return
*/
public static boolean isConnected(Context context)
{
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != connectivity)
{
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (null != info && info.isConnected())
{
if (info.getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
return false;
}
/**
* 判断是否是wifi连接
*/
public static boolean isWifi(Context context)
{
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null)
return false;
return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
}
/**
* 打开网络设置界面
*/
public static void openSetting(Activity activity)
{
Intent intent = new Intent("/");
ComponentName cm = new ComponentName("com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(cm);
intent.setAction("android.intent.action.VIEW");
activity.startActivityForResult(intent, 0);
}
}
/**
* 安装.apk文件
*
* @param context
*/
public void install(Context context, String fileName)
{
if (TextUtils.isEmpty(fileName) || context == null)
{
return;
}
try
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
context.startActivity(intent);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 安装.apk文件
*
* @param context
*/
public void install(Context context, File file)
{
try
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
}
catch (Exception e)
{
e.printStackTrace();
}
}
package com.github.lazylibrary.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class WiFiUtil {
/**
* 判断是否连接WIFI
* @param context 上下文
* @return boolean
*/
public static boolean isWifiConnected(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI);
if (wifiNetworkInfo.isConnected()) {
return true;
}
return false;
}
}
package com.github.lazylibrary.util;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
/**
* 流转换成字符串
*/
public class StreamUtils {
/**
* @param inputStream inputStream
* @return 字符串转换之后的
*/
public static String streamToString(InputStream inputStream) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, len);
out.flush();
}
String result = out.toString();
out.close();
inputStream.close();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
package com.github.lazylibrary.util;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
/**
* 视图动画工具箱,提供简单的控制视图的动画的工具方法
*
* @author zhenguo
*/
public final class ViewAnimationUtils {
/**
* Don't let anyone instantiate this class.
*/
private ViewAnimationUtils() {
throw new Error("Do not need instantiate!");
}
// /**
// * 默认动画持续时间
// */
// public static final long DEFAULT_ANIMATION_DURATION = 300;
/*
* ************************************************************* 视图透明度渐变动画
* ********************************************************************
*/
/**
* 将给定视图渐渐隐去(view.setVisibility(View.INVISIBLE))
*
* @param view 被处理的视图
* @param isBanClick 在执行动画的过程中是否禁止点击
* @param durationMillis 持续时间,毫秒
* @param animationListener 动画监听器
*/
public static void invisibleViewByAlpha(final View view,
long durationMillis, final boolean isBanClick,
final AnimationListener animationListener) {
if (view.getVisibility() != View.INVISIBLE) {
view.setVisibility(View.INVISIBLE);
AlphaAnimation hiddenAlphaAnimation = AnimationUtils
.getHiddenAlphaAnimation(durationMillis);
hiddenAlphaAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
if (isBanClick) {
view.setClickable(false);
}
if (animationListener != null) {
animationListener.onAnimationStart(animation);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
if (animationListener != null) {
animationListener.onAnimationRepeat(animation);
}
}
@Override
public void onAnimationEnd(Animation animation) {
if (isBanClick) {
view.setClickable(true);
}
if (animationListener != null) {
animationListener.onAnimationEnd(animation);
}
}
});
view.startAnimation(hiddenAlphaAnimation);
}
}
/**
* 将给定视图渐渐隐去(view.setVisibility(View.INVISIBLE))
*
* @param view 被处理的视图
* @param durationMillis 持续时间,毫秒
* @param animationListener 动画监听器
*/
public static void invisibleViewByAlpha(final View view,
long durationMillis, final AnimationListener animationListener) {
invisibleViewByAlpha(view, durationMillis, false, animationListener);
}
/**
* 将给定视图渐渐隐去(view.setVisibility(View.INVISIBLE))
*
* @param view 被处理的视图
* @param durationMillis 持续时间,毫秒
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void invisibleViewByAlpha(final View view,
long durationMillis, boolean isBanClick) {
invisibleViewByAlpha(view, durationMillis, isBanClick, null);
}
/**
* 将给定视图渐渐隐去(view.setVisibility(View.INVISIBLE))
*
* @param view 被处理的视图
* @param durationMillis 持续时间,毫秒
*/
public static void invisibleViewByAlpha(final View view, long durationMillis) {
invisibleViewByAlpha(view, durationMillis, false, null);
}
/**
* 将给定视图渐渐隐去(view.setVisibility(View.INVISIBLE)),
* 默认的持续时间为DEFAULT_ALPHA_ANIMATION_DURATION
*
* @param view 被处理的视图
* @param isBanClick 在执行动画的过程中是否禁止点击
* @param animationListener 动画监听器
*/
public static void invisibleViewByAlpha(final View view,
boolean isBanClick, final AnimationListener animationListener) {
invisibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION,
isBanClick, animationListener);
}
/**
* 将给定视图渐渐隐去(view.setVisibility(View.INVISIBLE)),
* 默认的持续时间为DEFAULT_ALPHA_ANIMATION_DURATION
*
* @param view 被处理的视图
* @param animationListener 动画监听器
*/
public static void invisibleViewByAlpha(final View view,
final AnimationListener animationListener) {
invisibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION,
false, animationListener);
}
/**
* 将给定视图渐渐隐去(view.setVisibility(View.INVISIBLE)),
* 默认的持续时间为DEFAULT_ALPHA_ANIMATION_DURATION
*
* @param view 被处理的视图
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void invisibleViewByAlpha(final View view, boolean isBanClick) {
invisibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION,
isBanClick, null);
}
/**
* 将给定视图渐渐隐去(view.setVisibility(View.INVISIBLE)),
* 默认的持续时间为DEFAULT_ALPHA_ANIMATION_DURATION
*
* @param view 被处理的视图
*/
public static void invisibleViewByAlpha(final View view) {
invisibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION,
false, null);
}
/**
* 将给定视图渐渐隐去最后从界面中移除(view.setVisibility(View.GONE))
*
* @param view 被处理的视图
* @param durationMillis 持续时间,毫秒
* @param isBanClick 在执行动画的过程中是否禁止点击
* @param animationListener 动画监听器
*/
public static void goneViewByAlpha(final View view, long durationMillis,
final boolean isBanClick, final AnimationListener animationListener) {
if (view.getVisibility() != View.GONE) {
view.setVisibility(View.GONE);
AlphaAnimation hiddenAlphaAnimation = AnimationUtils
.getHiddenAlphaAnimation(durationMillis);
hiddenAlphaAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
if (isBanClick) {
view.setClickable(false);
}
if (animationListener != null) {
animationListener.onAnimationStart(animation);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
if (animationListener != null) {
animationListener.onAnimationRepeat(animation);
}
}
@Override
public void onAnimationEnd(Animation animation) {
if (isBanClick) {
view.setClickable(true);
}
if (animationListener != null) {
animationListener.onAnimationEnd(animation);
}
}
});
view.startAnimation(hiddenAlphaAnimation);
}
}
/**
* 将给定视图渐渐隐去最后从界面中移除(view.setVisibility(View.GONE))
*
* @param view 被处理的视图
* @param durationMillis 持续时间,毫秒
* @param animationListener 动画监听器
*/
public static void goneViewByAlpha(final View view, long durationMillis,
final AnimationListener animationListener) {
goneViewByAlpha(view, durationMillis, false, animationListener);
}
/**
* 将给定视图渐渐隐去最后从界面中移除(view.setVisibility(View.GONE))
*
* @param view 被处理的视图
* @param durationMillis 持续时间,毫秒
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void goneViewByAlpha(final View view, long durationMillis,
final boolean isBanClick) {
goneViewByAlpha(view, durationMillis, isBanClick, null);
}
/**
* 将给定视图渐渐隐去最后从界面中移除(view.setVisibility(View.GONE))
*
* @param view 被处理的视图
* @param durationMillis 持续时间,毫秒
*/
public static void goneViewByAlpha(final View view, long durationMillis) {
goneViewByAlpha(view, durationMillis, false, null);
}
/**
* 将给定视图渐渐隐去最后从界面中移除(view.setVisibility(View.GONE)),
* 默认的持续时间为DEFAULT_ALPHA_ANIMATION_DURATION
*
* @param view 被处理的视图
* @param isBanClick 在执行动画的过程中是否禁止点击
* @param animationListener 动画监听器
*/
public static void goneViewByAlpha(final View view,
final boolean isBanClick, final AnimationListener animationListener) {
goneViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION,
isBanClick, animationListener);
}
/**
* 将给定视图渐渐隐去最后从界面中移除(view.setVisibility(View.GONE)),
* 默认的持续时间为DEFAULT_ALPHA_ANIMATION_DURATION
*
* @param view 被处理的视图
* @param animationListener 动画监听器
*/
public static void goneViewByAlpha(final View view,
final AnimationListener animationListener) {
goneViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, false,
animationListener);
}
/**
* 将给定视图渐渐隐去最后从界面中移除(view.setVisibility(View.GONE)),
* 默认的持续时间为DEFAULT_ALPHA_ANIMATION_DURATION
*
* @param view 被处理的视图
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void goneViewByAlpha(final View view, final boolean isBanClick) {
goneViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION,
isBanClick, null);
}
/**
* 将给定视图渐渐隐去最后从界面中移除(view.setVisibility(View.GONE)),
* 默认的持续时间为DEFAULT_ALPHA_ANIMATION_DURATION
*
* @param view 被处理的视图
*/
public static void goneViewByAlpha(final View view) {
goneViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION, false,
null);
}
/**
* 将给定视图渐渐显示出来(view.setVisibility(View.VISIBLE))
*
* @param view 被处理的视图
* @param durationMillis 持续时间,毫秒
* @param isBanClick 在执行动画的过程中是否禁止点击
* @param animationListener 动画监听器
*/
public static void visibleViewByAlpha(final View view, long durationMillis,
final boolean isBanClick, final AnimationListener animationListener) {
if (view.getVisibility() != View.VISIBLE) {
view.setVisibility(View.VISIBLE);
AlphaAnimation showAlphaAnimation = AnimationUtils
.getShowAlphaAnimation(durationMillis);
showAlphaAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
if (isBanClick) {
view.setClickable(false);
}
if (animationListener != null) {
animationListener.onAnimationStart(animation);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
if (animationListener != null) {
animationListener.onAnimationRepeat(animation);
}
}
@Override
public void onAnimationEnd(Animation animation) {
if (isBanClick) {
view.setClickable(true);
}
if (animationListener != null) {
animationListener.onAnimationEnd(animation);
}
}
});
view.startAnimation(showAlphaAnimation);
}
}
/**
* 将给定视图渐渐显示出来(view.setVisibility(View.VISIBLE))
*
* @param view 被处理的视图
* @param durationMillis 持续时间,毫秒
* @param animationListener 动画监听器
*/
public static void visibleViewByAlpha(final View view, long durationMillis,
final AnimationListener animationListener) {
visibleViewByAlpha(view, durationMillis, false, animationListener);
}
/**
* 将给定视图渐渐显示出来(view.setVisibility(View.VISIBLE))
*
* @param view 被处理的视图
* @param durationMillis 持续时间,毫秒
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void visibleViewByAlpha(final View view, long durationMillis,
final boolean isBanClick) {
visibleViewByAlpha(view, durationMillis, isBanClick, null);
}
/**
* 将给定视图渐渐显示出来(view.setVisibility(View.VISIBLE))
*
* @param view 被处理的视图
* @param durationMillis 持续时间,毫秒
*/
public static void visibleViewByAlpha(final View view, long durationMillis) {
visibleViewByAlpha(view, durationMillis, false, null);
}
/**
* 将给定视图渐渐显示出来(view.setVisibility(View.VISIBLE)),
* 默认的持续时间为DEFAULT_ALPHA_ANIMATION_DURATION
*
* @param view 被处理的视图
* @param animationListener 动画监听器
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void visibleViewByAlpha(final View view,
final boolean isBanClick, final AnimationListener animationListener) {
visibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION,
isBanClick, animationListener);
}
/**
* 将给定视图渐渐显示出来(view.setVisibility(View.VISIBLE)),
* 默认的持续时间为DEFAULT_ALPHA_ANIMATION_DURATION
*
* @param view 被处理的视图
* @param animationListener 动画监听器
*/
public static void visibleViewByAlpha(final View view,
final AnimationListener animationListener) {
visibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION,
false, animationListener);
}
/**
* 将给定视图渐渐显示出来(view.setVisibility(View.VISIBLE)),
* 默认的持续时间为DEFAULT_ALPHA_ANIMATION_DURATION
*
* @param view 被处理的视图
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void visibleViewByAlpha(final View view,
final boolean isBanClick) {
visibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION,
isBanClick, null);
}
/**
* 将给定视图渐渐显示出来(view.setVisibility(View.VISIBLE)),
* 默认的持续时间为DEFAULT_ALPHA_ANIMATION_DURATION
*
* @param view 被处理的视图
*/
public static void visibleViewByAlpha(final View view) {
visibleViewByAlpha(view, AnimationUtils.DEFAULT_ANIMATION_DURATION,
false, null);
}
/*
* ************************************************************* 视图移动动画
* ********************************************************************
*/
/**
* 视图移动
*
* @param view 要移动的视图
* @param fromXDelta X轴开始坐标
* @param toXDelta X轴结束坐标
* @param fromYDelta Y轴开始坐标
* @param toYDelta Y轴结束坐标
* @param cycles 重复
* @param durationMillis 持续时间
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void translate(final View view, float fromXDelta,
float toXDelta, float fromYDelta, float toYDelta, float cycles,
long durationMillis, final boolean isBanClick) {
TranslateAnimation translateAnimation = new TranslateAnimation(
fromXDelta, toXDelta, fromYDelta, toYDelta);
translateAnimation.setDuration(durationMillis);
if (cycles > 0.0) {
translateAnimation.setInterpolator(new CycleInterpolator(cycles));
}
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
if (isBanClick) {
view.setClickable(false);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (isBanClick) {
view.setClickable(true);
}
}
});
view.startAnimation(translateAnimation);
}
/**
* 视图移动
*
* @param view 要移动的视图
* @param fromXDelta X轴开始坐标
* @param toXDelta X轴结束坐标
* @param fromYDelta Y轴开始坐标
* @param toYDelta Y轴结束坐标
* @param cycles 重复
* @param durationMillis 持续时间
*/
public static void translate(final View view, float fromXDelta,
float toXDelta, float fromYDelta, float toYDelta, float cycles,
long durationMillis) {
translate(view, fromXDelta, toXDelta, fromYDelta, toYDelta, cycles,
durationMillis, false);
}
/**
* 视图摇晃
*
* @param view 要摇动的视图
* @param fromXDelta X轴开始坐标
* @param toXDelta X轴结束坐标
* @param cycles 重复次数
* @param durationMillis 持续时间
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void shake(View view, float fromXDelta, float toXDelta,
float cycles, long durationMillis, final boolean isBanClick) {
translate(view, fromXDelta, toXDelta, 0.0f, 0.0f, cycles,
durationMillis, isBanClick);
}
/**
* 视图摇晃
*
* @param view 要摇动的视图
* @param fromXDelta X轴开始坐标
* @param toXDelta X轴结束坐标
* @param cycles 重复次数
* @param durationMillis 持续时间
*/
public static void shake(View view, float fromXDelta, float toXDelta,
float cycles, long durationMillis) {
translate(view, fromXDelta, toXDelta, 0.0f, 0.0f, cycles,
durationMillis, false);
}
/**
* 视图摇晃,默认摇晃幅度为10,重复7次
*
* @param view view
* @param cycles 重复次数
* @param durationMillis 持续时间
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void shake(View view, float cycles, long durationMillis,
final boolean isBanClick) {
translate(view, 0.0f, 10.0f, 0.0f, 0.0f, cycles, durationMillis,
isBanClick);
}
/**
* 视图摇晃,默认摇晃幅度为10,持续700毫秒
*
* @param view view
* @param cycles 重复次数
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void shake(View view, float cycles, final boolean isBanClick) {
translate(view, 0.0f, 10.0f, 0.0f, 0.0f, cycles, 700, isBanClick);
}
/**
* 视图摇晃,默认摇晃幅度为10
*
* @param view view
* @param cycles 重复次数
* @param durationMillis 持续时间
*/
public static void shake(View view, float cycles, long durationMillis) {
translate(view, 0.0f, 10.0f, 0.0f, 0.0f, cycles, durationMillis, false);
}
/**
* 视图摇晃,默认摇晃幅度为10,重复7次
*
* @param view view
* @param durationMillis 持续时间
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void shake(View view, long durationMillis,
final boolean isBanClick) {
translate(view, 0.0f, 10.0f, 0.0f, 0.0f, 7, durationMillis, isBanClick);
}
/**
* 视图摇晃,默认摇晃幅度为10,持续700毫秒
*
* @param view 要摇动的视图
* @param cycles 重复次数
*/
public static void shake(View view, float cycles) {
translate(view, 0.0f, 10.0f, 0.0f, 0.0f, cycles, 700, false);
}
/**
* 视图摇晃,默认摇晃幅度为10,重复7次
*
* @param view view
* @param durationMillis 持续时间
*/
public static void shake(View view, long durationMillis) {
translate(view, 0.0f, 10.0f, 0.0f, 0.0f, 7, durationMillis, false);
}
/**
* 视图摇晃,默认摇晃幅度为10,重复7次,持续700毫秒
*
* @param view view
* @param isBanClick 在执行动画的过程中是否禁止点击
*/
public static void shake(View view, final boolean isBanClick) {
translate(view, 0.0f, 10.0f, 0.0f, 0.0f, 7, 700, isBanClick);
}
/**
* 视图摇晃,默认摇晃幅度为10,重复7次,持续700毫秒
*
* @param view view
*/
public static void shake(View view) {
translate(view, 0.0f, 10.0f, 0.0f, 0.0f, 7, 700, false);
}
}
package com.github.lazylibrary.util;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import android.text.TextUtils;
public class MD5 {
private static final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
public static void main(String[] args) {
System.out.println(md5sum("/init.rc"));
}
public static String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
sb.append(HEX_DIGITS[b[i] & 0x0f]);
}
return sb.toString();
}
public static String md5sum(String filename) {
InputStream fis;
byte[] buffer = new byte[1024];
int numRead = 0;
MessageDigest md5;
try {
fis = new FileInputStream(filename);
md5 = MessageDigest.getInstance("MD5");
while ((numRead = fis.read(buffer)) > 0) {
md5.update(buffer, 0, numRead);
}
fis.close();
String md5Str = toHexString(md5.digest());
return TextUtils.isEmpty(md5Str) ? "" : md5Str;
} catch (Exception e) {
System.out.println("error");
return "";
}
}
}
package com.github.lazylibrary.util;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
/**
* 视图工具箱
*/
public class ViewUtils {
private static final String CLASS_NAME_GRID_VIEW
= "android.widget.GridView";
private static final String FIELD_NAME_VERTICAL_SPACING
= "mVerticalSpacing";
private ViewUtils() {
throw new AssertionError();
}
/**
* get ListView height according to every children
*
* @param view view
* @return int
*/
public static int getListViewHeightBasedOnChildren(ListView view) {
int height = getAbsListViewHeightBasedOnChildren(view);
ListAdapter adapter;
int adapterCount;
if (view != null && (adapter = view.getAdapter()) != null &&
(adapterCount = adapter.getCount()) > 0) {
height += view.getDividerHeight() * (adapterCount - 1);
}
return height;
}
/**
* get GridView vertical spacing
*
* @param view view
* @return int
*/
public static int getGridViewVerticalSpacing(GridView view) {
// get mVerticalSpacing by android.widget.GridView
Class> demo = null;
int verticalSpacing = 0;
try {
demo = Class.forName(CLASS_NAME_GRID_VIEW);
Field field = demo.getDeclaredField(FIELD_NAME_VERTICAL_SPACING);
field.setAccessible(true);
verticalSpacing = (Integer) field.get(view);
return verticalSpacing;
} catch (Exception e) {
/**
* accept all exception, include ClassNotFoundException, NoSuchFieldException, InstantiationException,
* IllegalArgumentException, IllegalAccessException, NullPointException
*/
e.printStackTrace();
}
return verticalSpacing;
}
/**
* get AbsListView height according to every children
*
* @param view view
* @return int
*/
public static int getAbsListViewHeightBasedOnChildren(AbsListView view) {
ListAdapter adapter;
if (view == null || (adapter = view.getAdapter()) == null) {
return 0;
}
int height = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View item = adapter.getView(i, null, view);
if (item instanceof ViewGroup) {
item.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
item.measure(0, 0);
height += item.getMeasuredHeight();
}
height += view.getPaddingTop() + view.getPaddingBottom();
return height;
}
/**
* set view height
*
* @param view view
* @param height height
*/
public static void setViewHeight(View view, int height) {
if (view == null) {
return;
}
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = height;
}
/**
* @param view listview
*/
public static void setListViewHeightBasedOnChildren(ListView view) {
setViewHeight(view, getListViewHeightBasedOnChildren(view));
}
/**
* @param view AbsListView
*/
public static void setAbsListViewHeightBasedOnChildren(AbsListView view) {
setViewHeight(view, getAbsListViewHeightBasedOnChildren(view));
}
/**
* set SearchView OnClickListener
*
* @param v set SearchView OnClickListener
* @param listener set SearchView OnClickListener
*/
public static void setSearchViewOnClickListener(View v, OnClickListener listener) {
if (v instanceof ViewGroup) {
ViewGroup group = (ViewGroup) v;
int count = group.getChildCount();
for (int i = 0; i < count; i++) {
View child = group.getChildAt(i);
if (child instanceof LinearLayout ||
child instanceof RelativeLayout) {
setSearchViewOnClickListener(child, listener);
}
if (child instanceof TextView) {
TextView text = (TextView) child;
text.setFocusable(false);
}
child.setOnClickListener(listener);
}
}
}
/**
* get descended views from parent.
*
* @param 泛型
* @param parent ViewGroup
* @param filter Type of views which will be returned.
* @param includeSubClass Whether returned list will include views which
* are
* subclass of filter or not.
* @return View
*/
public static List getDescendants(ViewGroup parent, Class filter, boolean includeSubClass) {
List descendedViewList = new ArrayList();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
Class extends View> childsClass = child.getClass();
if ((includeSubClass && filter.isAssignableFrom(childsClass)) ||
(!includeSubClass && childsClass == filter)) {
descendedViewList.add(filter.cast(child));
}
if (child instanceof ViewGroup) {
descendedViewList.addAll(
getDescendants((ViewGroup) child, filter,
includeSubClass));
}
}
return descendedViewList;
}
/**
* 手动测量布局大小
*
* @param view 被测量的布局
* @param width 布局默认宽度
* @param height 布局默认高度
* 示例: measureView(view, ViewGroup.LayoutParams.MATCH_PARENT,
* ViewGroup.LayoutParams.WRAP_CONTENT);
*/
public static void measureView(View view, int width, int height) {
ViewGroup.LayoutParams params = view.getLayoutParams();
if (params == null) {
params = new ViewGroup.LayoutParams(width, height);
}
int mWidth = ViewGroup.getChildMeasureSpec(0, 0, params.width);
int mHeight;
int tempHeight = params.height;
if (tempHeight > 0) {
mHeight = View.MeasureSpec.makeMeasureSpec(tempHeight,
View.MeasureSpec.EXACTLY);
}
else {
mHeight = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
}
view.measure(mWidth, mHeight);
}
//*****设置外边距相关函数*******************************************************************************
/**
* 设置View的左侧外边距
*
* @param view 要设置外边距的View
* @param left 左侧外边距
*/
public static void setMarginLeft(View view, int left) {
setMargins(view, left, 0, 0, 0);
}
/**
* 设置View的顶部外边距
*
* @param view 要设置外边距的View
* @param top 顶部外边距
*/
public static void setMarginTop(View view, int top) {
setMargins(view, 0, top, 0, 0);
}
/**
* 设置View的右侧外边距
*
* @param view 要设置外边距的View
* @param right 右侧外边距
*/
public static void setMarginRight(View view, int right) {
setMargins(view, 0, 0, right, 0);
}
/**
* 设置View的底部外边距
*
* @param view 要设置外边距的View
* @param bottom 底部外边距
*/
public static void setMarginBottom(View view, int bottom) {
setMargins(view, 0, 0, 0, bottom);
}
/**
* 设置View的外边距(Margins)
*
* @param view 要设置外边距的View
* @param left 左侧外边距
* @param top 顶部外边距
* @param right 右侧外边距
* @param bottom 底部外边距
*/
public static void setMargins(View view, int left, int top, int right, int bottom) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view
.getLayoutParams();
p.setMargins(left, top, right, bottom);
view.requestLayout(); //请求重绘
}
}
/**
* 获取一个LinearLayout
*
* @param context 上下文
* @param orientation 流向
* @param width 宽
* @param height 高
* @return LinearLayout
*/
public static LinearLayout createLinearLayout(Context context, int orientation, int width, int height) {
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(orientation);
linearLayout.setLayoutParams(
new LinearLayout.LayoutParams(width, height));
return linearLayout;
}
/**
* 获取一个LinearLayout
*
* @param context 上下文
* @param orientation 流向
* @param width 宽
* @param height 高
* @param weight 权重
* @return LinearLayout
*/
public static LinearLayout createLinearLayout(Context context, int orientation, int width, int height, int weight) {
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(orientation);
linearLayout.setLayoutParams(
new LinearLayout.LayoutParams(width, height, weight));
return linearLayout;
}
/**
* 根据ListView的所有子项的高度设置其高度
*/
public static void setListViewHeightByAllChildrenViewHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() *
(listAdapter.getCount() - 1));
((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10);
listView.setLayoutParams(params);
}
}
/**
* 将给定视图的高度增加一点
*
* @param view 给定的视图
* @param increasedAmount 增加多少
*/
public static void addViewHeight(View view, int increasedAmount) {
ViewGroup.LayoutParams headerLayoutParams
= (ViewGroup.LayoutParams) view.getLayoutParams();
headerLayoutParams.height += increasedAmount;
view.setLayoutParams(headerLayoutParams);
}
/**
* 设置给定视图的宽度
*
* @param view 给定的视图
* @param newWidth 新的宽度
*/
public static void setViewWidth(View view, int newWidth) {
ViewGroup.LayoutParams headerLayoutParams
= (ViewGroup.LayoutParams) view.getLayoutParams();
headerLayoutParams.width = newWidth;
view.setLayoutParams(headerLayoutParams);
}
/**
* 将给定视图的宽度增加一点
*
* @param view 给定的视图
* @param increasedAmount 增加多少
*/
public static void addViewWidth(View view, int increasedAmount) {
ViewGroup.LayoutParams headerLayoutParams
= (ViewGroup.LayoutParams) view.getLayoutParams();
headerLayoutParams.width += increasedAmount;
view.setLayoutParams(headerLayoutParams);
}
/**
* 获取流布局的底部外边距
*/
public static int getLinearLayoutBottomMargin(LinearLayout linearLayout) {
return ((LinearLayout.LayoutParams) linearLayout.getLayoutParams()).bottomMargin;
}
/**
* 设置流布局的底部外边距
*/
public static void setLinearLayoutBottomMargin(LinearLayout linearLayout, int newBottomMargin) {
LinearLayout.LayoutParams lp
= (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
lp.bottomMargin = newBottomMargin;
linearLayout.setLayoutParams(lp);
}
/**
* 获取流布局的高度
*/
public static int getLinearLayoutHiehgt(LinearLayout linearLayout) {
return ((LinearLayout.LayoutParams) linearLayout.getLayoutParams()).height;
}
/**
* 设置输入框的光标到末尾
*/
public static final void setEditTextSelectionToEnd(EditText editText) {
Editable editable = editText.getEditableText();
Selection.setSelection((Spannable) editable,
editable.toString().length());
}
/**
* 执行测量,执行完成之后只需调用View的getMeasuredXXX()方法即可获取测量结果
*/
public static final View measure(View view) {
ViewGroup.LayoutParams p = view.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = View.MeasureSpec.makeMeasureSpec(lpHeight,
View.MeasureSpec.EXACTLY);
}
else {
childHeightSpec = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
}
view.measure(childWidthSpec, childHeightSpec);
return view;
}
/**
* 获取给定视图的测量高度
*/
public static final int getMeasuredHeight(View view) {
return measure(view).getMeasuredHeight();
}
/**
* 获取给定视图的测量宽度
*/
public static final int getMeasuredWidth(View view) {
return measure(view).getMeasuredWidth();
}
/**
* 获取视图1相对于视图2的位置,注意在屏幕上看起来视图1应该被视图2包含,但是视图1和视图并不一定是绝对的父子关系也可以是兄弟关系,只是一个大一个小而已
*/
public static final Rect getRelativeRect(View view1, View view2) {
Rect childViewGlobalRect = new Rect();
Rect parentViewGlobalRect = new Rect();
view1.getGlobalVisibleRect(childViewGlobalRect);
view2.getGlobalVisibleRect(parentViewGlobalRect);
return new Rect(childViewGlobalRect.left - parentViewGlobalRect.left,
childViewGlobalRect.top - parentViewGlobalRect.top,
childViewGlobalRect.right - parentViewGlobalRect.left,
childViewGlobalRect.bottom - parentViewGlobalRect.top);
}
/**
* 删除监听器
*/
@SuppressWarnings("deprecation") @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static final void removeOnGlobalLayoutListener(ViewTreeObserver viewTreeObserver, ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
viewTreeObserver.removeGlobalOnLayoutListener(
onGlobalLayoutListener);
}
else {
viewTreeObserver.removeOnGlobalLayoutListener(
onGlobalLayoutListener);
}
}
/**
* 缩放视图
*/
public static void zoomView(View view, float scaleX, float scaleY, Point originalSize) {
int width = (int) (originalSize.x * scaleX);
int height = (int) (originalSize.y * scaleY);
ViewGroup.LayoutParams viewGroupParams = view.getLayoutParams();
if (viewGroupParams != null) {
viewGroupParams.width = width;
viewGroupParams.height = height;
}
else {
viewGroupParams = new ViewGroup.LayoutParams(width, height);
}
view.setLayoutParams(viewGroupParams);
}
/**
* 缩放视图
*/
public static void zoomView(View view, float scaleX, float scaleY) {
zoomView(view, scaleX, scaleY,
new Point(view.getWidth(), view.getHeight()));
}
/**
* 缩放视图
*
* @param scale 比例
*/
public static void zoomView(View view, float scale, Point originalSize) {
zoomView(view, scale, scale, originalSize);
}
/**
* 缩放视图
*/
public static void zoomView(View view, float scale) {
zoomView(view, scale, scale,
new Point(view.getWidth(), view.getHeight()));
}
}