2017-02-22 21:39:55 +08:00

144 lines
5.6 KiB
Java

package com.fastaccess.helper;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.support.annotation.ColorInt;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.util.TypedValue;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import com.fastaccess.R;
import java.util.Arrays;
/**
* Created by kosh20111 on 10/7/2015 10:42 PM
*/
public class ViewHelper {
public static int getPrimaryDarkColor(Context context) {
TypedValue typedValue = new TypedValue();
TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[]{R.attr.colorPrimaryDark});
int color = a.getColor(0, 0);
a.recycle();
return color;
}
public static int toPx(Context context, int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, dp, context.getResources().getDisplayMetrics());
}
public static Drawable tintDrawable(@NonNull Drawable drawable, @ColorInt int color) {
drawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
return drawable;
}
public static Drawable getDrawableSelector(int normalColor, int pressedColor) {
return new RippleDrawable(ColorStateList.valueOf(pressedColor), getRippleMask(normalColor), getRippleMask(normalColor));
}
private static Drawable getRippleMask(int color) {
float[] outerRadii = new float[8];
Arrays.fill(outerRadii, 3);
RoundRectShape r = new RoundRectShape(outerRadii, null, null);
ShapeDrawable shapeDrawable = new ShapeDrawable(r);
shapeDrawable.getPaint().setColor(color);
return shapeDrawable;
}
private static StateListDrawable getStateListDrawable(int normalColor, int pressedColor) {
StateListDrawable states = new StateListDrawable();
states.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(pressedColor));
states.addState(new int[]{android.R.attr.state_focused}, new ColorDrawable(pressedColor));
states.addState(new int[]{android.R.attr.state_activated}, new ColorDrawable(pressedColor));
states.addState(new int[]{android.R.attr.state_selected}, new ColorDrawable(pressedColor));
states.addState(new int[]{}, new ColorDrawable(normalColor));
return states;
}
public static ColorStateList textSelector(int normalColor, int pressedColor) {
return new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_pressed},
new int[]{android.R.attr.state_focused},
new int[]{android.R.attr.state_activated},
new int[]{android.R.attr.state_selected},
new int[]{}
},
new int[]{
pressedColor,
pressedColor,
pressedColor,
pressedColor,
normalColor
}
);
}
private static boolean isTablet(Resources resources) {
return (resources.getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
public static boolean isTablet(Context context) {
return isTablet(context.getResources());
}
private static void setTextViewMenuCounter(@NonNull NavigationView navigationView, @IdRes int itemId, int count) {
TextView view = (TextView) navigationView.getMenu().findItem(itemId).getActionView();
view.setText(String.format("%s", count));
}
public static boolean isLandscape(Resources resources) {
return resources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
}
@SuppressWarnings("WeakerAccess") public static Rect getLayoutPosition(@NonNull View view) {
Rect myViewRect = new Rect();
view.getGlobalVisibleRect(myViewRect);
return myViewRect;
}
@SuppressWarnings("WeakerAccess") @Nullable public static String getTransitionName(@NonNull View view) {
return !InputHelper.isEmpty(view.getTransitionName()) ? view.getTransitionName() : null;
}
@SuppressWarnings("WeakerAccess") public static void showKeyboard(@NonNull View v, @NonNull Context activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, 0);
}
public static void showKeyboard(@NonNull View v) {
showKeyboard(v, v.getContext());
}
public static void hideKeyboard(@NonNull View view) {
InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
@ColorInt public static int generateTextColor(int color) {
return Color.rgb(255 - Color.red(color),
255 - Color.green(color),
255 - Color.blue(color));
}
}