public class AppUtils {
/**
* convert DP to PX
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* check double click
*/
private static long lastClickTime = 0L;
public static boolean isDoubleClick() {
long currentTime = System.currentTimeMillis();
long timeDistance = Math.abs(currentTime - lastClickTime);
if (timeDistance < 500) {
return true;
}
lastClickTime = currentTime;
return false;
}
private static int[] channelLayoutLocation = null;
/**
* check pointer is in the view scope.
*/
public static boolean checkPointerInView(View view, float x, float y, boolean alwaysNew) {
if (channelLayoutLocation == null) {
channelLayoutLocation = new int[2];
view.getLocationOnScreen(channelLayoutLocation);
} else {
if (alwaysNew) {
channelLayoutLocation = new int[2];
view.getLocationOnScreen(channelLayoutLocation);
}
}
if (x >= channelLayoutLocation[0] && x <= channelLayoutLocation[0] + view.getWidth() && y >= channelLayoutLocation[1] && y <= channelLayoutLocation[1] + view.getHeight()) {
return true;
}
return false;
}
/**
* change imageView tint color by coding.
*/
public static void setImageViewTint(ImageView imageView, @DrawableRes int drawableId, int color) {
if (imageView == null) {
return;
}
Context context = imageView.getContext();
if (context == null) {
return;
}
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (drawable != null) {
Drawable drawableWrapperEmpty = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawableWrapperEmpty, color);
imageView.setImageDrawable(drawableWrapperEmpty);
}
}
/**
* java reflection for you to reference.
* @param themeInfoTriplet customize theme class info.example:
* <code>
* themeInfoTriplet = new Triplet<>("setTheme", Theme.class, ThemeProvider.INSTANCE.getCurrent().getValue()).
* </code>
* @usage if themeInfoTriplet is not null , the Item View Holder will be create based on theme,otherwise, will be create based on customize view properties.
*/
private <HOLDER extends ViewHolder, BINDING extends ViewDataBinding>
ViewHolder getHolderByDataBinding(ViewGroup parent, @LayoutRes int layout, Class<HOLDER> holderClass,
MessagesListStyle style, Object payload, Triplet<String, Class<?>, Object> themeInfoTriplet) {
try {
//layout
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
BINDING binding = DataBindingUtil.inflate(layoutInflater, layout, parent, false);
if (BuildConfig.DEBUG && binding.getClass() != null) {
Log.w("javaReflection", "bindingClass=" + binding.getClass().getSimpleName() + "|paramsClass=" + themeInfoTriplet.second);
}
Method setThemeMethod = binding.getClass().getMethod(themeInfoTriplet.first, themeInfoTriplet.second);
setThemeMethod.invoke(binding, themeInfoTriplet.third);
View v = binding.getRoot();
//create viewHolder.
Constructor<HOLDER> constructor = null;
HOLDER holder;
try {
constructor = holderClass.getDeclaredConstructor(View.class, Object.class);
constructor.setAccessible(true);
holder = constructor.newInstance(v, payload);
} catch (NoSuchMethodException e) {
constructor = holderClass.getDeclaredConstructor(View.class);
constructor.setAccessible(true);
holder = constructor.newInstance(v);
}
if (BuildConfig.DEBUG) {
Log.w("javaReflection", "binding=success==>" + holderClass);
}
return holder;
} catch (Exception e) {
if (BuildConfig.DEBUG) {
Log.w("javaReflection", "binding=failure==>" + holderClass);
}
throw new UnsupportedOperationException("Somehow we couldn't create the ViewHolder for message.3 ways should check: \n\n1.Are you sure you are using 'databinding' mode in yourItemLayout.xml? [" + holderClass + "]\n2.Are you sure you had configured '<variable/>' in yourItemLayout.xml? example: \n\n<variable\nname=\"theme\"\ntype=\"com.xxxxxx.themelib.Theme\"/>\n\n3.Are you sure you had passed the param of 'themeInfoTriplet' for MessagesListAdapter?\n", e);
}
}
/**
* get Status Bar Height
*/
public static int getStatusBarHeight(@NotNull Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
Android(Java) AppUtils
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 这个是写kotlin单模块测试时出现的问题,解决方法是: 1.在gradle.properties中添加andro...
- 1.在AS中 双击 shift 输入 default project structure 打开第一个,然后 JD...
- 我的是引入了重复的lib导致的!!!!当你遇到这样的错误时,一定要先检查你的build.gradle是不是有重复的...
- execution failed for task ':transformClassesWithDexForQsD...
- 我的当时是引入了重复的lib导致的!!!!当你遇到这样的错误时一定要先检查你的build.gradle是不是有重复...