Example usage for android.graphics Color alpha

List of usage examples for android.graphics Color alpha

Introduction

In this page you can find the example usage for android.graphics Color alpha.

Prototype

@IntRange(from = 0, to = 255)
public static int alpha(int color) 

Source Link

Document

Return the alpha component of a color int.

Usage

From source file:com.google.android.apps.muzei.settings.ChooseSourceFragment.java

public void updateSources() {
    mSelectedSource = null;/*  w ww  .  ja va2 s  .  c  o  m*/
    Intent queryIntent = new Intent(ACTION_MUZEI_ART_SOURCE);
    PackageManager pm = getContext().getPackageManager();
    mSources.clear();
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(queryIntent, PackageManager.GET_META_DATA);

    for (ResolveInfo ri : resolveInfos) {
        Source source = new Source();
        source.label = ri.loadLabel(pm).toString();
        source.icon = new BitmapDrawable(getResources(), generateSourceImage(ri.loadIcon(pm)));
        source.targetSdkVersion = ri.serviceInfo.applicationInfo.targetSdkVersion;
        source.componentName = new ComponentName(ri.serviceInfo.packageName, ri.serviceInfo.name);
        if (ri.serviceInfo.descriptionRes != 0) {
            try {
                Context packageContext = getContext()
                        .createPackageContext(source.componentName.getPackageName(), 0);
                Resources packageRes = packageContext.getResources();
                source.description = packageRes.getString(ri.serviceInfo.descriptionRes);
            } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
                Log.e(TAG, "Can't read package resources for source " + source.componentName);
            }
        }
        Bundle metaData = ri.serviceInfo.metaData;
        source.color = Color.WHITE;
        if (metaData != null) {
            String settingsActivity = metaData.getString("settingsActivity");
            if (!TextUtils.isEmpty(settingsActivity)) {
                source.settingsActivity = ComponentName
                        .unflattenFromString(ri.serviceInfo.packageName + "/" + settingsActivity);
            }

            String setupActivity = metaData.getString("setupActivity");
            if (!TextUtils.isEmpty(setupActivity)) {
                source.setupActivity = ComponentName
                        .unflattenFromString(ri.serviceInfo.packageName + "/" + setupActivity);
            }

            source.color = metaData.getInt("color", source.color);

            try {
                float[] hsv = new float[3];
                Color.colorToHSV(source.color, hsv);
                boolean adjust = false;
                if (hsv[2] < 0.8f) {
                    hsv[2] = 0.8f;
                    adjust = true;
                }
                if (hsv[1] > 0.4f) {
                    hsv[1] = 0.4f;
                    adjust = true;
                }
                if (adjust) {
                    source.color = Color.HSVToColor(hsv);
                }
                if (Color.alpha(source.color) != 255) {
                    source.color = Color.argb(255, Color.red(source.color), Color.green(source.color),
                            Color.blue(source.color));
                }
            } catch (IllegalArgumentException ignored) {
            }
        }

        mSources.add(source);
    }

    final String appPackage = getContext().getPackageName();
    Collections.sort(mSources, new Comparator<Source>() {
        @Override
        public int compare(Source s1, Source s2) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                boolean target1IsO = s1.targetSdkVersion >= Build.VERSION_CODES.O;
                boolean target2IsO = s2.targetSdkVersion >= Build.VERSION_CODES.O;
                if (target1IsO && !target2IsO) {
                    return 1;
                } else if (!target1IsO && target2IsO) {
                    return -1;
                }
            }
            String pn1 = s1.componentName.getPackageName();
            String pn2 = s2.componentName.getPackageName();
            if (!pn1.equals(pn2)) {
                if (appPackage.equals(pn1)) {
                    return -1;
                } else if (appPackage.equals(pn2)) {
                    return 1;
                }
            }
            return s1.label.compareTo(s2.label);
        }
    });

    redrawSources();
}

From source file:com.mishiranu.dashchan.ui.gallery.GalleryActivity.java

private void switchMode(boolean galleryMode, boolean animated) {
    int duration = animated ? GALLERY_TRANSITION_DURATION : 0;
    pagerUnit.switchMode(galleryMode, duration);
    listUnit.switchMode(galleryMode, duration);
    if (galleryMode) {
        int count = instance.galleryItems.size();
        setTitle(R.string.action_gallery);
        getActionBar().setSubtitle(/*ww w  .j a  v a 2  s  . com*/
                getResources().getQuantityString(R.plurals.text_several_files_count_format, count, count));
    }
    modifySystemUiVisibility(GalleryInstance.FLAG_LOCKED_GRID, galleryMode);
    this.galleryMode = galleryMode;
    if (galleryMode) {
        new CornerAnimator(0xa0, 0xc0);
    } else {
        int alpha = Color.alpha(ACTION_BAR_COLOR);
        new CornerAnimator(alpha, alpha);
    }
    invalidateOptionsMenu();
}

From source file:com.manuelpeinado.numericpageindicator.NumericPageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;//w  w  w  .  j a  v a 2s.  c o m
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    // mCurrentPage is -1 on first start and after orientation changed. If
    // so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null) {
        mCurrentPage = mViewPager.getCurrentItem();
    }

    // Handle the first time we draw the view, when onMeasure has not been
    // called yet
    if (mTextFirstPart == null) {
        updateText();
    }

    // Draw the main text (e.g. "Page 1 of 20"). The hardest part is drawing
    // the page
    // number itself, because of the animated effect in which the current
    // page fades
    // out and the next one fades in. In order to implement this effect we
    // are forced to
    // draw the text in four "chunks": the first part ("Page "), the current
    // page
    // number ("1"), the next page number ("2"), and the last part
    // (" of 20").
    // To implement the fade in and fade out animations we simply change the
    // alpha
    // of the page number text, relative to the view pager scroll

    final float currentPageWeight = 1 - mPageOffset;
    final float nextPageWeight = mPageOffset;
    final float firstPartWidth = mPaintText.measureText(mTextFirstPart);
    final String currentPageNumber = Integer.toString(mCurrentPage + 1);
    final String nextPageNumber = Integer.toString(mCurrentPage + 2);
    final float currentPageNumberWidth = mPaintText.measureText(currentPageNumber);
    final float nextPageNumberWidth = mPaintText.measureText(nextPageNumber);
    final float pageNumberWidth = currentPageWeight * currentPageNumberWidth
            + nextPageWeight * nextPageNumberWidth;
    final float lastPartWidth = mPaintText.measureText(mTextLastPart);
    final float totalWidth = firstPartWidth + pageNumberWidth + lastPartWidth;
    float currentX = (getWidth() - totalWidth) / 2;
    canvas.drawText(mTextFirstPart, currentX, mTextBottom, mPaintText);
    currentX += firstPartWidth;
    final float pageNumberCenterX = currentX + pageNumberWidth / 2;

    final int startAlpha = Color.alpha(mColorPageNumberText);
    final int endAlpha = 0;
    final float currentPageNumberAlpha = currentPageWeight * startAlpha + nextPageWeight * endAlpha;

    mPaintPageNumberText.setAlpha((int) currentPageNumberAlpha);
    canvas.drawText(currentPageNumber, pageNumberCenterX - currentPageNumberWidth / 2, mTextBottom,
            mPaintPageNumberText);

    final float nextPageNumberAlpha = nextPageWeight * startAlpha + currentPageWeight * endAlpha;
    mPaintPageNumberText.setAlpha((int) nextPageNumberAlpha);
    canvas.drawText(nextPageNumber, pageNumberCenterX - nextPageNumberWidth / 2, mTextBottom,
            mPaintPageNumberText);

    currentX += pageNumberWidth;
    canvas.drawText(mTextLastPart, currentX, mTextBottom, mPaintText);

    // Draw the "start" and "end" buttons
    if (mShowStartEndButtons) {
        final int textStartAlpha = Color.alpha(mColorText);
        final int textEndAlpha = 0;
        if (mCurrentPage != 0 && mStartDown) {
            canvas.drawRect(mRectStart, mPaintButtonBackground);
        }
        if (mCurrentPage != count - 1 && mEndDown) {
            canvas.drawRect(mRectEnd, mPaintButtonBackground);
        }
        if (mCurrentPage == 0) {
            mPaintText.setAlpha((int) (nextPageWeight * textStartAlpha + currentPageWeight * textEndAlpha));
        }
        if (!mShowImagesForPageControls) {
            canvas.drawText(mTextStartButton, mRectStart.centerX() - mWidthStartText / 2, mRectStartText.bottom,
                    mPaintText);
        } else if (mStartButtonImage != null) {
            canvas.drawBitmap(mStartButtonImage, mRectStart.centerX() - mStartButtonImage.getWidth() / 2,
                    mRectStart.centerY() - mStartButtonImage.getHeight() / 2, mPaintText);
        }
        mPaintText.setAlpha(Color.alpha(mColorText));
        if (mCurrentPage < count - 1) {
            if (mCurrentPage == count - 2) {
                mPaintText.setAlpha((int) (currentPageWeight * textStartAlpha + nextPageWeight * textEndAlpha));
            }
            if (!mShowImagesForPageControls) {
                canvas.drawText(mTextEndButton, mRectEnd.centerX() - mWidthEndText / 2, mRectEndText.bottom,
                        mPaintText);
            } else if (mEndButtonImage != null) {
                canvas.drawBitmap(mEndButtonImage, mRectEnd.centerX() - mEndButtonImage.getWidth() / 2,
                        mRectEnd.centerY() - mEndButtonImage.getHeight() / 2, mPaintText);
            }
            mPaintText.setAlpha(Color.alpha(mColorText));
        }
    }

    // Draw the "next" and "previous" buttons
    if (mShowChangePageButtons) {
        final int textStartAlpha = Color.alpha(mColorText);
        final int textEndAlpha = 0;
        if (mCurrentPage != 0 && mPreviousDown) {
            canvas.drawRect(mRectPrevious, mPaintButtonBackground);
        }
        if (mCurrentPage != count - 1 && mNextDown) {
            canvas.drawRect(mRectNext, mPaintButtonBackground);
        }
        if (mCurrentPage == 0) {
            mPaintText.setAlpha((int) (nextPageWeight * textStartAlpha + currentPageWeight * textEndAlpha));
        }
        if (!mShowImagesForPageControls) {
            canvas.drawText(mTextPreviousButton, mRectPrevious.centerX() - mWidthPreviousText / 2,
                    mRectPreviousText.bottom, mPaintText);
        } else if (mPreviousButtonImage != null) {
            canvas.drawBitmap(mPreviousButtonImage,
                    mRectPrevious.centerX() - mPreviousButtonImage.getWidth() / 2,
                    mRectPrevious.centerY() - mPreviousButtonImage.getHeight() / 2, mPaintText);
        }
        mPaintText.setAlpha(Color.alpha(mColorText));
        if (mCurrentPage < count - 1) {
            if (mCurrentPage == count - 2) {
                mPaintText.setAlpha((int) (currentPageWeight * textStartAlpha + nextPageWeight * textEndAlpha));
            }
            if (!mShowImagesForPageControls) {
                canvas.drawText(mTextNextButton, mRectNext.centerX() - mWidthNextText / 2, mRectNextText.bottom,
                        mPaintText);
            } else if (mNextButtonImage != null) {
                canvas.drawBitmap(mNextButtonImage, mRectNext.centerX() - mNextButtonImage.getWidth() / 2,
                        mRectNext.centerY() - mNextButtonImage.getHeight() / 2, mPaintText);
            }
            mPaintText.setAlpha(Color.alpha(mColorText));
        }
    }
}

From source file:com.granita.tasks.EditTaskFragment.java

public int mixColors(int col1, int col2) {
    int r1, g1, b1, r2, g2, b2;

    int a1 = Color.alpha(col1);

    r1 = Color.red(col1);/*from ww  w  .  ja v a 2 s . c o m*/
    g1 = Color.green(col1);
    b1 = Color.blue(col1);

    r2 = Color.red(col2);
    g2 = Color.green(col2);
    b2 = Color.blue(col2);

    int r3 = (r1 * a1 + r2 * (255 - a1)) / 255;
    int g3 = (g1 * a1 + g2 * (255 - a1)) / 255;
    int b3 = (b1 * a1 + b2 * (255 - a1)) / 255;

    return Color.rgb(r3, g3, b3);
}

From source file:devlight.io.library.ArcProgressStackView.java

private int adjustColorAlpha(final int color, final float factor) {
    return Color.argb(Math.round(Color.alpha(color) * factor), Color.red(color), Color.green(color),
            Color.blue(color));//from  w  ww.  ja v  a  2  s .  co  m
}

From source file:io.jawg.osmcontributor.ui.utils.BitmapHandler.java

public static Bitmap hue(Bitmap bitmap, float hue) {
    Bitmap newBitmap = bitmap.copy(bitmap.getConfig(), true);
    final int width = newBitmap.getWidth();
    final int height = newBitmap.getHeight();
    float[] hsv = new float[3];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int pixel = newBitmap.getPixel(x, y);
            Color.colorToHSV(pixel, hsv);
            hsv[0] = hue;//from   ww w  . j  a v  a2s  . co m
            newBitmap.setPixel(x, y, Color.HSVToColor(Color.alpha(pixel), hsv));
        }
    }

    return newBitmap;
}

From source file:com.jaredrummler.android.colorpicker.ColorPickerDialog.java

void createColorShades(@ColorInt final int color) {
    final int[] colorShades = getColorShades(color);

    if (shadesLayout.getChildCount() != 0) {
        for (int i = 0; i < shadesLayout.getChildCount(); i++) {
            FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
            final ColorPanelView cpv = (ColorPanelView) layout.findViewById(R.id.cpv_color_panel_view);
            ImageView iv = (ImageView) layout.findViewById(R.id.cpv_color_image_view);
            cpv.setColor(colorShades[i]);
            cpv.setTag(false);/*from   w w  w . j a  v a 2s.c om*/
            iv.setImageDrawable(null);
        }
        return;
    }

    final int horizontalPadding = getResources().getDimensionPixelSize(R.dimen.cpv_item_horizontal_padding);

    for (final int colorShade : colorShades) {
        int layoutResId;
        if (colorShape == ColorShape.SQUARE) {
            layoutResId = R.layout.cpv_color_item_square;
        } else {
            layoutResId = R.layout.cpv_color_item_circle;
        }

        final View view = View.inflate(getActivity(), layoutResId, null);
        final ColorPanelView colorPanelView = (ColorPanelView) view.findViewById(R.id.cpv_color_panel_view);

        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) colorPanelView.getLayoutParams();
        params.leftMargin = params.rightMargin = horizontalPadding;
        colorPanelView.setLayoutParams(params);
        colorPanelView.setColor(colorShade);
        shadesLayout.addView(view);

        colorPanelView.post(new Runnable() {
            @Override
            public void run() {
                // The color is black when rotating the dialog. This is a dirty fix. WTF!?
                colorPanelView.setColor(colorShade);
            }
        });

        colorPanelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag() instanceof Boolean && (Boolean) v.getTag()) {
                    colorPickerDialogListener.onColorSelected(dialogId, ColorPickerDialog.this.color);
                    dismiss();
                    return; // already selected
                }
                ColorPickerDialog.this.color = colorPanelView.getColor();
                adapter.selectNone();
                for (int i = 0; i < shadesLayout.getChildCount(); i++) {
                    FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
                    ColorPanelView cpv = (ColorPanelView) layout.findViewById(R.id.cpv_color_panel_view);
                    ImageView iv = (ImageView) layout.findViewById(R.id.cpv_color_image_view);
                    iv.setImageResource(cpv == v ? R.drawable.cpv_preset_checked : 0);
                    if (cpv == v && ColorUtils.calculateLuminance(cpv.getColor()) >= 0.65
                            || Color.alpha(cpv.getColor()) <= ALPHA_THRESHOLD) {
                        iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                    } else {
                        iv.setColorFilter(null);
                    }
                    cpv.setTag(cpv == v);
                }
            }
        });
        colorPanelView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                colorPanelView.showHint();
                return true;
            }
        });
    }
}

From source file:com.jrummyapps.android.colorpicker.ColorPickerDialog.java

void createColorShades(@ColorInt final int color) {
    final int[] colorShades = getColorShades(color);

    if (shadesLayout.getChildCount() != 0) {
        for (int i = 0; i < shadesLayout.getChildCount(); i++) {
            FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
            final ColorPanelView cpv = layout.findViewById(R.id.cpv_color_panel_view);
            ImageView iv = layout.findViewById(R.id.cpv_color_image_view);
            cpv.setColor(colorShades[i]);
            cpv.setTag(false);/*from  w w w.j  a  v a  2  s .  c  o  m*/
            iv.setImageDrawable(null);
        }
        return;
    }

    final int horizontalPadding = getResources().getDimensionPixelSize(R.dimen.cpv_item_horizontal_padding);

    for (final int colorShade : colorShades) {
        int layoutResId;
        if (colorShape == ColorShape.SQUARE) {
            layoutResId = R.layout.cpv_color_item_square;
        } else {
            layoutResId = R.layout.cpv_color_item_circle;
        }

        final View view = View.inflate(getActivity(), layoutResId, null);
        final ColorPanelView colorPanelView = view.findViewById(R.id.cpv_color_panel_view);

        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) colorPanelView.getLayoutParams();
        params.leftMargin = params.rightMargin = horizontalPadding;
        colorPanelView.setLayoutParams(params);
        colorPanelView.setColor(colorShade);
        shadesLayout.addView(view);

        colorPanelView.post(new Runnable() {
            @Override
            public void run() {
                // The color is black when rotating the dialog. This is a dirty fix. WTF!?
                colorPanelView.setColor(colorShade);
            }
        });

        colorPanelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag() instanceof Boolean && (Boolean) v.getTag()) {
                    colorPickerDialogListener.onColorSelected(dialogId, ColorPickerDialog.this.color);
                    dismiss();
                    return; // already selected
                }
                ColorPickerDialog.this.color = colorPanelView.getColor();
                adapter.selectNone();
                for (int i = 0; i < shadesLayout.getChildCount(); i++) {
                    FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
                    ColorPanelView cpv = layout.findViewById(R.id.cpv_color_panel_view);
                    ImageView iv = layout.findViewById(R.id.cpv_color_image_view);
                    iv.setImageResource(cpv == v ? R.drawable.cpv_preset_checked : 0);
                    if (cpv == v && ColorUtils.calculateLuminance(cpv.getColor()) >= 0.65
                            || Color.alpha(cpv.getColor()) <= ALPHA_THRESHOLD) {
                        iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                    } else {
                        iv.setColorFilter(null);
                    }
                    cpv.setTag(cpv == v);
                }
            }
        });
        colorPanelView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                colorPanelView.showHint();
                return true;
            }
        });
    }
}

From source file:com.jaredrummler.android.colorpicker.ColorPickerDialog.java

private int shadeColor(@ColorInt int color, double percent) {
    String hex = String.format("#%06X", (0xFFFFFF & color));
    long f = Long.parseLong(hex.substring(1), 16);
    double t = percent < 0 ? 0 : 255;
    double p = percent < 0 ? percent * -1 : percent;
    long R = f >> 16;
    long G = f >> 8 & 0x00FF;
    long B = f & 0x0000FF;
    int alpha = Color.alpha(color);
    int red = (int) (Math.round((t - R) * p) + R);
    int green = (int) (Math.round((t - G) * p) + G);
    int blue = (int) (Math.round((t - B) * p) + B);
    return Color.argb(alpha, red, green, blue);
}

From source file:com.klinker.android.sliding.MultiShrinkScroller.java

/**
 * Sets the tint color that should be applied to the header. If an image is present, this
 * will go behind the image and show over it as the activity is scrolled, otherwise it will
 * just be the color displayed at the top of the screen.
 * @param color the primary color for the activity to display.
 *//*from  www.  ja  v a2  s.c om*/
public void setHeaderTintColor(int color) {
    headerTintColor = color;
    updatePhotoTintAndDropShadow();
    // We want to use the same amount of alpha on the new tint color as the previous tint color.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        final int edgeEffectAlpha = Color.alpha(edgeGlowBottom.getColor());
        edgeGlowBottom.setColor((color & 0xffffff) | Color.argb(edgeEffectAlpha, 0, 0, 0));
        edgeGlowTop.setColor(edgeGlowBottom.getColor());
    }
}