一.重写Application并在清单文件中注册,一个快速得到
上下文---------Context------
主线程---------MainThread
主线程Id-------MainThreadId
轮巡器---------MainLooper
主Handler------MainHandler
public class BaseApplication extends Application
{
private static Context mContext;
private static Thread mMainThread;
private static long mMainThreadId;
private static Looper mMainLooper;
private static Handler mMainHandler;
public static Context getContext()
{
return mContext;
}
public static Thread getMainThread()
{
return mMainThread;
}
public static long getMainThreadId()
{
return mMainThreadId;
}
public static Looper getMainThreadLooper()
{
return mMainLooper;
}
public static Handler getMainHandler()
{
return mMainHandler;
}
// 应用程序的入口
@Override
public void onCreate()
{
super.onCreate();
// 上下文
mContext = getApplicationContext();
// 主线程
mMainThread = Thread.currentThread();
// mMainThreadId = mMainThread.getId();
mMainThreadId = android.os.Process.myTid();
mMainLooper = getMainLooper();
// 创建主线程的handler
mMainHandler = new Handler();
}
}
二.用于UI改变的工具类
1.获取Context
2.获取资源文件的各资源----String,StringArray,packageName,Color,
3.将task在主线程执行,用于UI的更新
4.dp与px的转换
5.线程执行延时任务
6.移除任务
7.属性动画的开关1.安装具体具体俩--详细间AppdetailActivity
8.监听动画使得动画展开的时候父ScollerView随之滚动
public class UIUtils {
/**
* 上下文
*
* @return
*/
public static Context getContext() {
return BaseApplication.getContext();
}
public static Resources getResources() {
return getContext().getResources();
}
public static String getString(int resId) {
return getResources().getString(resId);
}
public static String[] getStringArray(int resId) {
return getResources().getStringArray(resId);
}
public static String getPackageName() {
return getContext().getPackageName();
}
public static int getColor(int resId) {
return getResources().getColor(resId);
}
public static Handler getMainHandler() {
return BaseApplication.getMainHandler();
}
public static long getMainThreadId() {
return BaseApplication.getMainThreadId();
}
/**
* 让task在主线程中执行
*/
public static void post(Runnable task) {
int myTid = android.os.Process.myTid();
if (myTid == getMainThreadId()) {
// 在主线程中执行的
task.run();
} else {
// 在子线程中执行的
getMainHandler().post(task);
}
}
/**
* dip 转 px
*
* @param dip
* @return
*/
public static int dip2px(int dip) {
//
// 公式: dp = px / (dpi / 160) px = dp * (dpi / 160)
// dp = px / denisity
// px = dp * denisity;
DisplayMetrics metrics = getResources().getDisplayMetrics();
float density = metrics.density;
return (int) (dip * density + 0.5f);
}
public static int px2dip(int px) {
// dp = px / denisity
DisplayMetrics metrics = getResources().getDisplayMetrics();
float density = metrics.density;
return (int) (px / density + 0.5f);
}
/**
* 执行延时任务
*
*/
public static void postDelayed(Runnable task, int delayed) {
getMainHandler().postDelayed(task, delayed);
}
/**
* 移除任务
*
* @param task
*/
public static void removeCallbacks(Runnable task) {
getMainHandler().removeCallbacks(task);
}
public static String getString(int id, Object... formatArgs) {
return getResources().getString(id, formatArgs);
}
//属性动画的开关
public static void doAnimationToggle(ViewGroup container, boolean animated,
boolean defailedToggleStatus) {
// 测量以下,然后得到高度
container.measure(0, 0);
int height = container.getMeasuredHeight();
if (defailedToggleStatus) {
if (animated) {
// 动画
// 如果是打开的,那么就关闭
// height ---> 0
int start = height;
int end = 0;
doAnimation(container, start, end);
} else {
// 这个
LayoutParams params = container.getLayoutParams();
params.height = 0;
container.setLayoutParams(params);
}
} else {
if (animated) {
// 如果是关闭的,那么就打开
int start = 0;
int end = height;
doAnimation(container, start, end);
} else {
LayoutParams params = container.getLayoutParams();
params.height = height;
container.setLayoutParams(params);
}
}
// // 给 箭头设置动画
// if (defailedToggleStatus) {
// // 如果是打开的,需要关闭,箭头由 上 到下
// ObjectAnimator.ofFloat(mIvArrow, "rotation", -180, 0).start();
// } else {
// // 箭头由 下到上
// ObjectAnimator.ofFloat(mIvArrow, "rotation", 0, 180).start();
// }
// 状态改变
defailedToggleStatus = !defailedToggleStatus;
}
private static void doAnimation(final ViewGroup container, int start,
int end) {
// 属性动画,ValueAnimator
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.setDuration(300);
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
int value = (Integer) animator.getAnimatedValue();
LayoutParams params = container.getLayoutParams();
params.height = value;
container.setLayoutParams(params);
}
});
animator.start();
}
//做动画的监听,使得父容器中存在ScollerView的时候能滚动到下面
public static void let_ParentsScollerViewFucosDowmWithAnimationListener(
Animator animator, final View rootView) {
animator.addListener(new AnimatorListener() {
@Override
public void onAnimationEnd(Animator arg0) {
//View rootView = getRootView();
ScrollView scrollView = null;
ViewParent parent = rootView.getParent();
if (parent != null && parent instanceof ViewGroup) {
while (true) {
parent = parent.getParent();
if (parent != null && parent instanceof ScrollView) {
scrollView = (ScrollView) parent;
break;
}
if (parent == null) {
break;
}
}
// 让ScrollView滚动到底部
scrollView.fullScroll(View.FOCUS_DOWN);
}
}
@Override
public void onAnimationStart(Animator arg0) {
}
@Override
public void onAnimationRepeat(Animator arg0) {
}
@Override
public void onAnimationCancel(Animator arg0) {
}
});
}
}