Java Code Examples for android.graphics.drawable.ColorDrawable#getColor()

The following examples show how to use android.graphics.drawable.ColorDrawable#getColor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppUtils.java    From AndroidNavigation with MIT License 6 votes vote down vote up
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;
}
 
Example 2
Source File: MergedAppBarLayoutBehavior.java    From react-native-bottom-sheet-behavior with MIT License 5 votes vote down vote up
public int getFullbackGroundColor() {
    if (mToolbar != null) {
        ColorDrawable drawable = (ColorDrawable) mToolbar.getBackground();
        if (drawable != null) {
            return drawable.getColor();
        }
    }
    return 0;
}
 
Example 3
Source File: ViewUtil.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
@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();
    }
}
 
Example 4
Source File: GalleryListTest.java    From moviedb-android with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 5
Source File: TrailerListTest.java    From moviedb-android with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 6
Source File: PXColorUtil.java    From pixate-freestyle-android with Apache License 2.0 5 votes vote down vote up
/**
 * 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]));
    }
}
 
Example 7
Source File: Coloring.java    From DrawMe with Apache License 2.0 5 votes vote down vote up
@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;
    }
}
 
Example 8
Source File: MainActivity.java    From EasyTableView with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 9
Source File: PXColorUtil.java    From pixate-freestyle-android with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source File: NotificationMainView.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example 11
Source File: PXColorUtil.java    From pixate-freestyle-android with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
    }
}
 
Example 12
Source File: PXColorUtil.java    From pixate-freestyle-android with Apache License 2.0 5 votes vote down vote up
/**
 * 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]));
    }
}
 
Example 13
Source File: SaveBitmapUtil.java    From OneText_For_Android with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 对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;
}
 
Example 14
Source File: TestUtils.java    From twitter-kit-android with Apache License 2.0 4 votes vote down vote up
public static int getBackgroundColor(ImageView imageView) {
    final ColorDrawable drawable = (ColorDrawable) imageView.getBackground();
    return drawable.getColor();
}
 
Example 15
Source File: ScreenShotHelper.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
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;
}
 
Example 16
Source File: ChangeColor.java    From animation-samples with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example 17
Source File: PXColorUtil.java    From pixate-freestyle-android with Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 18
Source File: RoundedColorDrawable.java    From FanXin-based-HuanXin with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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());
}
 
Example 19
Source File: TestUtils.java    From twitter-kit-android with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the color of the ImageView's ColorDrawable or 0 for API &lt; 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();
}
 
Example 20
Source File: RoundedColorDrawable.java    From fresco with MIT License 2 votes vote down vote up
/**
 * 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());
}