Android 對UI操作的工具類UIUtils
Android中對UI操作的工具類,大概還有不少常用方法沒有記錄進去,以后用到一些再更新就好噠~網上那么多資料來著。
我絕對是仙劍是真愛粉吶,今朝眼神好溫柔喲~今朝和祈好甜蜜啊~羨慕!
public class UIUtils {
public static Context getContext() {
return XXApplication.getInstance();
}
/**
* 獲取資源對象
*/
public static Resources getResources() {
return getContext().getResources();
}
/**
* 獲取資源文件字符串
*
* @param id
* @return
*/
public static String getString(int id) {
return getResources().getString(id);
}
/**
* 獲取資源文件字符串數組
*
* @param id
* @return
*/
public static String[] getStringArray(int id) {
return getResources().getStringArray(id);
}
/**
* 獲取資源文件圖片
*
* @param id
* @return
*/
public static Drawable getDrawable(int id) {
return ContextCompat.getDrawable(getContext(), id);
}
/**
* 獲取資源文件顏色
*
* @param id
* @return
*/
public static int getColor(int id) {
return ContextCompat.getColor(getContext(), id);
}
/**
* 根據id獲取顏色的狀態選擇器
*
* @param id
* @return
*/
public static ColorStateList getColorStateList(int id) {
return ContextCompat.getColorStateList(getContext(), id);
}
/**
* 獲取尺寸
*
* @param id
* @return
*/
public static int getDimen(int id) {
return getResources().getDimensionPixelSize(id); // 返回具體像素值
}
/**
* dp -> px
*
* @param dp
* @return
*/
public static int dp2px(float dp) {
float density = getResources().getDisplayMetrics().density;
return (int) (dp * density + 0.5f);
}
/**
* px -> dp
*
* @param px
* @return
*/
public static float px2dp(int px) {
float density = getResources().getDisplayMetrics().density;
return px / density;
}
/**
* 加載布局文件
*
* @param id
* @return
*/
public static View inflate(int id) {
return View.inflate(getContext(), id, null);
}
/**
* 把自身從父View中移除
* @param view
*/
public static void removeSelfFromParent(View view) {
if (view != null) {
ViewParent parent = view.getParent();
if (parent != null && parent instanceof ViewGroup) {
ViewGroup group = (ViewGroup) parent;
group.removeView(view);
}
}
}
/**
* 請求View樹重新布局,用于解決中層View有布局狀態而導致上層View狀態斷裂
* @param view
* @param isAll
*/
public static void requestLayoutParent(View view, boolean isAll) {
ViewParent parent = view.getParent();
while (parent != null && parent instanceof View) {
if (!parent.isLayoutRequested()) {
parent.requestLayout();
if (!isAll) {
break;
}
}
parent = parent.getParent();
}
}
/**
* 判斷觸點是否落在該View上
* @param ev
* @param v
* @return
*/
public static boolean isTouchInView(MotionEvent ev, View v) {
int[] vLoc = new int[2];
v.getLocationOnScreen(vLoc);
float motionX = ev.getRawX();
float motionY = ev.getRawY();
return motionX >= vLoc[0] && motionX <= (vLoc[0] + v.getWidth())
&& motionY >= vLoc[1] && motionY <= (vLoc[1] + v.getHeight());
}
/**
* findViewById的泛型封裝,減少強轉代碼
* @param layout
* @param id
* @param <T>
* @return
*/
public static <T extends View> T findViewById(View layout, int id) {
return (T) layout.findViewById(id);
}
/**
* 獲取屏幕的比例
* @param context
* @return
*/
public static float getScaledDensity(Context context) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
float value = dm.scaledDensity;
return value;
}
/**
* 獲取控件的高度,如果獲取的高度為0,則重新計算尺寸后再返回高度
* @param view
* @return
*/
public static int getViewMeasuredHeight(View view) {
calcViewMeasure(view);
return view.getMeasuredHeight();
}
/**
* 獲取控件的寬度,如果獲取的寬度為0,則重新計算尺寸后再返回寬度
* @param view
* @return
*/
public static int getViewMeasuredWidth(View view) {
calcViewMeasure(view);
return view.getMeasuredWidth();
}
/**
* 測量控件的尺寸
* @param view
*/
public static void calcViewMeasure(View view) {
int width = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
int expandSpec = View.MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
view.measure(width, expandSpec);
}
/**
* 設置textview指定文字為某一顏色
*
* @param content 顯示的文字
* @param color 需要轉換成的顏色值
* @param start 需要變色文字開始位置
* @param end 需要變色文字結束位置
*/
public static SpannableStringBuilder changeTextColor(String content, int color, int start, int end) {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);
spannableStringBuilder.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannableStringBuilder;
}
}
來自:http://www.jianshu.com/p/a5f34960eb11
本文由用戶 susan1318 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!