66 lines
2.5 KiB
Java
66 lines
2.5 KiB
Java
package com.xscm.moduleutil.widget;
|
|
|
|
import android.content.Context;
|
|
import android.content.res.Resources;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.os.Build;
|
|
import android.util.TypedValue;
|
|
|
|
public class Utils {
|
|
|
|
public static <T> T checkNotNull(T object, String message) {
|
|
if (object == null) {
|
|
throw new NullPointerException(message);
|
|
}
|
|
return object;
|
|
}
|
|
|
|
public static int pixelOfScaled(Context c, int sp) {
|
|
Resources r = c.getResources();
|
|
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, r.getDisplayMetrics());
|
|
}
|
|
|
|
public static int pixelOfDp(Context c, int dp) {
|
|
Resources r = c.getResources();
|
|
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
|
|
}
|
|
|
|
|
|
private static final Object sLock = new Object();
|
|
private static TypedValue sTempValue;
|
|
/**
|
|
* Returns a drawable object associated with a particular resource ID.
|
|
* <p>
|
|
* Starting in {@link Build.VERSION_CODES#LOLLIPOP}, the
|
|
* returned drawable will be styled for the specified Context's theme.
|
|
*
|
|
* @param id The desired resource identifier, as generated by the aapt tool.
|
|
* This integer encodes the package, type, and resource entry.
|
|
* The value 0 is an invalid identifier.
|
|
* @return Drawable An object that can be used to draw this resource.
|
|
*/
|
|
public static Drawable getDrawable(Context context, int id) {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
return context.getDrawable(id);
|
|
} else if (Build.VERSION.SDK_INT >= 16) {
|
|
//noinspection deprecation
|
|
return context.getResources().getDrawable(id);
|
|
} else {
|
|
// Prior to JELLY_BEAN, Resources.getDrawable() would not correctly
|
|
// retrieve the final configuration density when the resource ID
|
|
// is a reference another Drawable resource. As a workaround, try
|
|
// to resolve the drawable reference manually.
|
|
final int resolvedId;
|
|
synchronized (sLock) {
|
|
if (sTempValue == null) {
|
|
sTempValue = new TypedValue();
|
|
}
|
|
context.getResources().getValue(id, sTempValue, true);
|
|
resolvedId = sTempValue.resourceId;
|
|
}
|
|
//noinspection deprecation
|
|
return context.getResources().getDrawable(resolvedId);
|
|
}
|
|
}
|
|
}
|