下面列出了android.graphics.drawable.ColorDrawable#getColor() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static int getStatusBarColor(final Window window) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return window.getStatusBarColor();
} else {
ViewGroup decorViewGroup = (ViewGroup) window.getDecorView();
View statusBarView = decorViewGroup.findViewWithTag("custom_status_bar_tag");
if (statusBarView != null) {
Drawable drawable = statusBarView.getBackground();
if (drawable instanceof ColorDrawable) {
ColorDrawable colorDrawable = (ColorDrawable) drawable;
return colorDrawable.getColor();
}
}
}
return Color.BLACK;
}
/**
* Sets the Saturation value on a view that has a colored background. In
* case the view's background is not a {@link ColorDrawable}, or does not
* contain one in a {@link LayerDrawable}, nothing will be applied.
*
* @param view
* @param saturation
*/
public static void setSaturation(View view, float saturation) {
ColorDrawable colorDrawable = getColorDrawableBackground(view);
if (colorDrawable != null) {
int color = colorDrawable.getColor();
float[] hsl = new float[3];
PXColorUtil.colorToHsl(color, hsl);
colorDrawable.setColor(PXColorUtil.hslToColor(Color.alpha(color), hsl[0], saturation,
hsl[2]));
}
}
/**
* Sets the Brightness value on a view that has a colored background. In
* case the view's background is not a {@link ColorDrawable}, or does not
* contain one in a {@link LayerDrawable}, nothing will be applied.
*
* @param view
* @param brightness
*/
public static void setBrightness(View view, float brightness) {
ColorDrawable colorDrawable = getColorDrawableBackground(view);
if (colorDrawable != null) {
int color = colorDrawable.getColor();
float[] hsl = new float[3];
PXColorUtil.colorToHsl(color, hsl);
colorDrawable.setColor(PXColorUtil.hslToColor(Color.alpha(color), hsl[0], hsl[1],
brightness));
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mTextAndBackground = (ViewGroup) findViewById(R.id.text_and_background);
ColorDrawable colorBackground = (ColorDrawable) mTextAndBackground.getBackground();
mBackgroundColor = colorBackground.getColor();
RippleDrawable rippleBackground = new RippleDrawable(ColorStateList.valueOf(
ThemeUtils.getAttrColor(getContext(), android.R.attr.colorControlHighlight)),
colorBackground, null);
mTextAndBackground.setBackground(rippleBackground);
mTitleView = (TextView) mTextAndBackground.findViewById(R.id.title);
mTextView = (TextView) mTextAndBackground.findViewById(R.id.text);
}
/**
* Returns the HSL value of a view that has a colored background. In case
* the view's background is not a {@link ColorDrawable}, or does not contain
* a color-drawable in one of its layers, the return value is
* <code>null</code>
*
* @param view
* @return The hue value (<code>null</code> in case the background is not a
* {@link ColorDrawable})
*/
public static float[] getHSL(View view) {
ColorDrawable colorDrawable = getColorDrawableBackground(view);
if (colorDrawable != null) {
int color = colorDrawable.getColor();
float[] hsl = new float[3];
PXColorUtil.colorToHsl(color, hsl);
return hsl;
}
return null;
}
public void onChangedTextColor(View view) {
Drawable background = view.getBackground();
ColorDrawable colorDrawable = (ColorDrawable) background;
switch (curSelect) {
case SELECT_TEXTS:
if (null != curCellInfo) {
curCellInfo.textColor = colorDrawable.getColor();
table.updateData(curCellInfo);
} else if (null != curMergeInfo) {
curMergeInfo.textColor = colorDrawable.getColor();
table.updateMergeData(curMergeInfo);
}
break;
case SELECT_BG:
if (null != curCellInfo) {
curCellInfo.bgColor = colorDrawable.getColor();
table.updateData(curCellInfo);
} else if (null != curMergeInfo) {
curMergeInfo.bgColor = colorDrawable.getColor();
table.updateMergeData(curMergeInfo);
}
break;
case SELECT_STROKE:
table.setStrokeColor(colorDrawable.getColor());
table.reset();
break;
case SELECT_OUT_STROKE:
table.setOutStrokeColor(colorDrawable.getColor());
table.reset();
break;
}
}
@ColorInt
public static int getDrawableColor(ColorDrawable colorDrawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return colorDrawable.getColor();
} else {
// http://stackoverflow.com/questions/15982044/get-activity-background-color-in-android-api-level-8
Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
colorDrawable.draw(canvas);
final int color = bitmap.getPixel(0, 0);
bitmap.recycle();
return color;
}
}
public int getFullbackGroundColor() {
if (mToolbar != null) {
ColorDrawable drawable = (ColorDrawable) mToolbar.getBackground();
if (drawable != null) {
return drawable.getColor();
}
}
return 0;
}
/**
* Sets the Hue value on a view that has a colored background. In case the
* view's background is not a {@link ColorDrawable}, or does not contain one
* in a {@link LayerDrawable}, nothing will be applied.
*
* @param view
* @param hue
*/
public static void setHue(View view, float hue) {
ColorDrawable colorDrawable = getColorDrawableBackground(view);
if (colorDrawable != null) {
int color = colorDrawable.getColor();
float[] hsl = new float[3];
PXColorUtil.colorToHsl(color, hsl);
colorDrawable.setColor(PXColorUtil.hslToColor(Color.alpha(color), hue, hsl[1], hsl[2]));
}
}
@Test
public void testBackgroundColor() throws Exception {
int expected = ContextCompat.getColor(activity, R.color.background_material_light);
ColorDrawable actualDrawable = (ColorDrawable) activity.getWindow().getDecorView().getBackground();
int actual = actualDrawable.getColor();
assertEquals("Background color is different!", expected, actual);
}
@Test
public void testBackgroundColor() throws Exception {
int expected = ContextCompat.getColor(activity, R.color.background_material_light);
ColorDrawable actualDrawable = (ColorDrawable) activity.getWindow().getDecorView().getBackground();
int actual = actualDrawable.getColor();
assertEquals("Background color is different!", expected, actual);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static int getColorDrawableColor(ColorDrawable drawable) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
drawable.draw(canvas);
int pix = bitmap.getPixel(0, 0);
bitmap.recycle();
return pix;
} else {
return drawable.getColor();
}
}
/**
* 对RecyclerView进行截图
* https://gist.github.com/PrashamTrivedi/809d2541776c8c141d9a
*/
public static Bitmap shotRecyclerView(RecyclerView view) {
RecyclerView.Adapter adapter = view.getAdapter();
Bitmap bigBitmap = null;
if (adapter != null) {
int size = adapter.getItemCount();
int height = 0;
Paint paint = new Paint();
int iHeight = 0;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);
for (int i = 0; i < size; i++) {
RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
adapter.onBindViewHolder(holder, i);
holder.itemView.measure(
View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(),
holder.itemView.getMeasuredHeight());
holder.itemView.setDrawingCacheEnabled(true);
holder.itemView.buildDrawingCache();
Bitmap drawingCache = holder.itemView.getDrawingCache();
if (drawingCache != null) {
bitmaCache.put(String.valueOf(i), drawingCache);
}
height += holder.itemView.getMeasuredHeight();
}
bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
Canvas bigCanvas = new Canvas(bigBitmap);
Drawable lBackground = view.getBackground();
if (lBackground instanceof ColorDrawable) {
ColorDrawable lColorDrawable = (ColorDrawable) lBackground;
int lColor = lColorDrawable.getColor();
bigCanvas.drawColor(lColor);
}
for (int i = 0; i < size; i++) {
Bitmap bitmap = bitmaCache.get(String.valueOf(i));
bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);
iHeight += bitmap.getHeight();
bitmap.recycle();
}
}
return bigBitmap;
}
@Override
public Animator createAnimator(ViewGroup sceneRoot,
TransitionValues startValues, TransitionValues endValues) {
// This transition can only be applied to views that are on both starting and ending scenes.
if (null == startValues || null == endValues) {
return null;
}
// Store a convenient reference to the target. Both the starting and ending layout have the
// same target.
final View view = endValues.view;
// Store the object containing the background property for both the starting and ending
// layouts.
Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND);
Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND);
// This transition changes background colors for a target. It doesn't animate any other
// background changes. If the property isn't a ColorDrawable, ignore the target.
if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) {
ColorDrawable startColor = (ColorDrawable) startBackground;
ColorDrawable endColor = (ColorDrawable) endBackground;
// If the background color for the target in the starting and ending layouts is
// different, create an animation.
if (startColor.getColor() != endColor.getColor()) {
// Create a new Animator object to apply to the targets as the transitions framework
// changes from the starting to the ending layout. Use the class ValueAnimator,
// which provides a timing pulse to change property values provided to it. The
// animation runs on the UI thread. The Evaluator controls what type of
// interpolation is done. In this case, an ArgbEvaluator interpolates between two
// #argb values, which are specified as the 2nd and 3rd input arguments.
ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(),
startColor.getColor(), endColor.getColor());
// Add an update listener to the Animator object.
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Object value = animation.getAnimatedValue();
// Each time the ValueAnimator produces a new frame in the animation, change
// the background color of the target. Ensure that the value isn't null.
if (null != value) {
view.setBackgroundColor((Integer) value);
}
}
});
// Return the Animator object to the transitions framework. As the framework changes
// between the starting and ending layouts, it applies the animation you've created.
return animator;
}
}
// For non-ColorDrawable backgrounds, we just return null, and no animation will take place.
return null;
}
public static Bitmap shotRecyclerView(RecyclerView view) {
RecyclerView.Adapter adapter = view.getAdapter();
Bitmap bigBitmap = null;
if (adapter != null) {
int size = adapter.getItemCount();
int height = 0;
Paint paint = new Paint();
int iHeight = 0;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);
for (int i = 0; i < size; i++) {
RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
adapter.onBindViewHolder(holder, i);
holder.itemView.measure(
View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(),
holder.itemView.getMeasuredHeight());
holder.itemView.setDrawingCacheEnabled(true);
holder.itemView.buildDrawingCache();
Bitmap drawingCache = holder.itemView.getDrawingCache();
if (drawingCache != null) {
bitmaCache.put(String.valueOf(i), drawingCache);
}
height += holder.itemView.getMeasuredHeight();
}
bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Config.ARGB_8888);
Canvas bigCanvas = new Canvas(bigBitmap);
Drawable lBackground = view.getBackground();
if (lBackground instanceof ColorDrawable) {
ColorDrawable lColorDrawable = (ColorDrawable) lBackground;
int lColor = lColorDrawable.getColor();
bigCanvas.drawColor(lColor);
}
for (int i = 0; i < size; i++) {
Bitmap bitmap = bitmaCache.get(String.valueOf(i));
bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);
iHeight += bitmap.getHeight();
bitmap.recycle();
}
}
return bigBitmap;
}
public static int getBackgroundColor(ImageView imageView) {
final ColorDrawable drawable = (ColorDrawable) imageView.getBackground();
return drawable.getColor();
}
/**
* Returns the background color value for a View that have a
* {@link ColorDrawable} background, or a {@link LayerDrawable} background
* that contains one.
*
* @param view
* @return The view's background color. -1 in case the view does not have a
* ColorDrawable background.
*/
public static int getColor(View view) {
ColorDrawable colorDrawable = getColorDrawableBackground(view);
if (colorDrawable != null) {
return colorDrawable.getColor();
}
return -1;
}
/**
* Creates a new instance of RoundedColorDrawable from the given ColorDrawable.
* @param colorDrawable color drawable to extract the color from
* @return a new RoundedColorDrawable
*/
public static RoundedColorDrawable fromColorDrawable(ColorDrawable colorDrawable) {
return new RoundedColorDrawable(colorDrawable.getColor());
}
/**
* Gets the color of the ImageView's ColorDrawable or 0 for API < 11.
* @param imageView an ImageView with a ColorDrawable
* @return int color of the ImageView
*/
public static int getDrawableColor(ImageView imageView) {
final ColorDrawable drawable = (ColorDrawable) imageView.getDrawable();
return drawable.getColor();
}
/**
* Creates a new instance of RoundedColorDrawable from the given ColorDrawable.
*
* @param colorDrawable color drawable to extract the color from
* @return a new RoundedColorDrawable
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static RoundedColorDrawable fromColorDrawable(ColorDrawable colorDrawable) {
return new RoundedColorDrawable(colorDrawable.getColor());
}