Android(Java) AppUtils

2022-05-10  本文已影响0人  想看烟花么
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;
    }
    
    
    
    
    
    
}
上一篇 下一篇

猜你喜欢

热点阅读