Java Code Examples for android.graphics.drawable.Drawable#setAlpha()

The following examples show how to use android.graphics.drawable.Drawable#setAlpha() . 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: FlexibleToolbarLayout.java    From AppCompat-Extension-Library with Apache License 2.0 6 votes vote down vote up
/**
 * Set the drawable to use for the content scrim from resources. Providing null will disable
 * the scrim functionality.
 *
 * @param drawable the drawable to display
 * @attr ref R.styleable#FlexibleToolbarLayout_contentScrimColor
 * @see #getContentScrim()
 */
public void setContentScrim(@Nullable Drawable drawable) {
    if (mContentScrim != drawable) {
        if (mContentScrim != null) {
            mContentScrim.setCallback(null);
        }
        if (drawable != null) {
            mContentScrim = drawable.mutate();
            drawable.setBounds(0, 0, getWidth(), getHeight());
            drawable.setCallback(this);
            drawable.setAlpha(mScrimAlpha);
        } else {
            mContentScrim = null;
        }
        ViewCompat.postInvalidateOnAnimation(this);
    }
}
 
Example 2
Source File: OneDrawable.java    From OneDrawable with Apache License 2.0 6 votes vote down vote up
private static Drawable getPressedStateDrawable(Context context, @PressedMode.Mode int mode, @FloatRange(from = 0.0f, to = 1.0f) float alpha, @NonNull Drawable pressed) {
    //ColorDrawable is not supported on 4.4 because the size of the ColorDrawable can not be determined unless the View size is passed in
    if (isKitkat() && !(pressed instanceof ColorDrawable)) {
        return kitkatDrawable(context, pressed, mode, alpha);
    }
    switch (mode) {
        case PressedMode.ALPHA:
            pressed.setAlpha(convertAlphaToInt(alpha));
            break;
        case PressedMode.DARK:
            pressed.setColorFilter(alphaColor(Color.BLACK, convertAlphaToInt(alpha)), PorterDuff.Mode.SRC_ATOP);
            break;
        default:
            pressed.setAlpha(convertAlphaToInt(alpha));
    }
    return pressed;
}
 
Example 3
Source File: CoalitionsActivity.java    From intra42 with Apache License 2.0 6 votes vote down vote up
@Override
protected void setViewContent() {
    if (blocs == null) {
        setViewState(StatusCode.EMPTY);
        return;
    }
    List<Coalitions> coalitions = blocs.coalitions;
    Collections.sort(coalitions, (o1, o2) -> {
        if (o1.score == o2.score)
            return 0;
        return o1.score < o2.score ? 1 : -1;
    });

    RecyclerAdapterCoalitionsBlocs adapter = new RecyclerAdapterCoalitionsBlocs(this, coalitions);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setNestedScrollingEnabled(false);
    DividerItemDecoration decoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
    Drawable decoDrawable = decoration.getDrawable();
    decoDrawable.setAlpha(1);
    decoration.setDrawable(decoDrawable);
    recyclerView.addItemDecoration(decoration);
}
 
Example 4
Source File: AboutActivity.java    From linphone-android with GNU General Public License v3.0 6 votes vote down vote up
private void displayUploadLogsInProgress() {
    if (mUploadInProgress) {
        return;
    }
    mUploadInProgress = true;

    mProgress = ProgressDialog.show(this, null, null);
    Drawable d = new ColorDrawable(ContextCompat.getColor(this, R.color.light_grey_color));
    d.setAlpha(200);
    mProgress
            .getWindow()
            .setLayout(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT);
    mProgress.getWindow().setBackgroundDrawable(d);
    mProgress.setContentView(R.layout.wait_layout);
    mProgress.show();
}
 
Example 5
Source File: EditFragment.java    From Passbook with Apache License 2.0 5 votes vote down vote up
@Override
public void onScroll(int l, int t, int oldl, int oldt) {
    int height = mHeader.getMeasuredHeight() - mToolbar.getMeasuredHeight();
    Drawable drawable = mToolbarContainer.getBackground();
    int alpha = 255 *  (Math.min(Math.max(t, 0), height))/ height;
    drawable.setAlpha(alpha);
    if(alpha >= 255) {
        ViewCompat.setElevation(mToolbarContainer, mElevation);
        ViewCompat.setElevation(mHeader, 0);
    }
    else {
        ViewCompat.setElevation(mToolbarContainer, 0);
        ViewCompat.setElevation(mHeader, mElevation);
    }
}
 
Example 6
Source File: FadingSelectionTransformer.java    From WheelView with Apache License 2.0 5 votes vote down vote up
@Override
public void transform(Drawable drawable, WheelView.ItemState itemState) {
    float relativePosition = Math.abs(itemState.getRelativePosition());
    int alpha = (int) ((1f - Math.pow(relativePosition, 2.5f)) * 255f);

    //clamp to between 0 and 255
    if (alpha > 255) alpha = 255;
    else if (alpha < 0) alpha = 0;

    drawable.setAlpha(alpha);
}
 
Example 7
Source File: OneDrawable.java    From OneDrawable with Apache License 2.0 5 votes vote down vote up
private static Drawable kitkatDrawable(Context context, @NonNull Drawable pressed, @PressedMode.Mode int mode, @FloatRange(from = 0.0f, to = 1.0f) float alpha) {
    Bitmap bitmap = Bitmap.createBitmap(pressed.getIntrinsicWidth(), pressed.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas myCanvas = new Canvas(bitmap);
    switch (mode) {
        case PressedMode.ALPHA:
            pressed.setAlpha(convertAlphaToInt(alpha));
            break;
        case PressedMode.DARK:
            pressed.setColorFilter(alphaColor(Color.BLACK, convertAlphaToInt(alpha)), PorterDuff.Mode.SRC_ATOP);
            break;
    }
    pressed.setBounds(0, 0, pressed.getIntrinsicWidth(), pressed.getIntrinsicHeight());
    pressed.draw(myCanvas);
    return new BitmapDrawable(context.getResources(), bitmap);
}
 
Example 8
Source File: FloatingActionButton.java    From school_shop with MIT License 5 votes vote down vote up
private void updateBackground(Drawable background) {
    if (background instanceof LayerDrawable) {
        LayerDrawable layers = (LayerDrawable) background;
        if (layers.getNumberOfLayers() == 2) {
            Drawable shadow = layers.getDrawable(0);
            Drawable circle = layers.getDrawable(1);

            if (shadow instanceof GradientDrawable) {
                if (mShadow) {
                    ((GradientDrawable) shadow.mutate()).setGradientRadius(getShadowRadius(shadow, circle));
                } else {
                    shadow.setAlpha(0);
                }
            }

            if (circle instanceof GradientDrawable) {
                initCircleDrawable(circle);
            }
        }
    } else if (background instanceof GradientDrawable) {
        initCircleDrawable(background);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (mImplicitElevation) {
            setElevation(mShadow ? getResources().getDimension(R.dimen.floating_action_button_elevation) : 0f);
        }
    }

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        //noinspection deprecation
        setBackgroundDrawable(background);
    } else {
        setBackground(background);
    }
}
 
Example 9
Source File: LayerDrawable.java    From Carbon with Apache License 2.0 5 votes vote down vote up
@Override
public void setAlpha(int alpha) {
    final ChildDrawable[] array = mLayerState.mChildren;
    final int N = mLayerState.mNum;
    for (int i = 0; i < N; i++) {
        final Drawable dr = array[i].mDrawable;
        if (dr != null) {
            dr.setAlpha(alpha);
        }
    }
}
 
Example 10
Source File: RadialProgressView.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Keep
@Override
public void setAlpha(float alpha) {
    super.setAlpha(alpha);
    if (useSelfAlpha) {
        Drawable background = getBackground();
        int a = (int) (alpha * 255);
        if (background != null) {
            background.setAlpha(a);
        }
        progressPaint.setAlpha(a);
    }
}
 
Example 11
Source File: DrawableHelper.java    From GetApk with MIT License 5 votes vote down vote up
public static Drawable tintDrawable(@NonNull Context context, @NonNull Drawable drawable, int colorAttr, int alpha, PorterDuff.Mode tintMode) {
    if (DrawableUtils.canSafelyMutateDrawable(drawable)) {
        drawable = drawable.mutate();
    }
    final int color = getThemeAttrColor(context, colorAttr);
    drawable.setColorFilter(AppCompatDrawableManager.getPorterDuffColorFilter(color, tintMode));

    if (alpha != -1) {
        drawable.setAlpha(alpha);
    }

    return drawable;
}
 
Example 12
Source File: CrossFadeTransitionDrawable.java    From AndroidMaterialDialog with Apache License 2.0 5 votes vote down vote up
@Override
protected final void onDraw(final float interpolatedTime, @NonNull final Canvas canvas) {
    int currentAlpha = Math.round(255 * interpolatedTime);
    Drawable first = getDrawable(0);

    if (useCrossFade) {
        first.setAlpha(255 - currentAlpha);
    }

    first.draw(canvas);
    Drawable second = getDrawable(1);
    second.setAlpha(currentAlpha);
    second.draw(canvas);
}
 
Example 13
Source File: WrappedDrawable.java    From android-file-chooser with Apache License 2.0 5 votes vote down vote up
@Override
public void setAlpha(int alpha) {
    Drawable drawable = getDrawable();
    if (drawable != null) {
        drawable.setAlpha(alpha);
    }
}
 
Example 14
Source File: RadialProgressView.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Keep
@Override
public void setAlpha(float alpha) {
    super.setAlpha(alpha);
    if (useSelfAlpha) {
        Drawable background = getBackground();
        int a = (int) (alpha * 255);
        if (background != null) {
            background.setAlpha(a);
        }
        progressPaint.setAlpha(a);
    }
}
 
Example 15
Source File: GuiInfoPopupWindow.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
static void applyDim(@NonNull ViewGroup parent){
    Drawable dim = new ColorDrawable(Color.BLACK);
    dim.setBounds(0, 0, parent.getWidth(), parent.getHeight());
    dim.setAlpha((int) (255 * 0.5));

    ViewGroupOverlay overlay = parent.getOverlay();
    overlay.add(dim);
}
 
Example 16
Source File: RemoteImageView.java    From cathode with Apache License 2.0 5 votes vote down vote up
protected void drawPlaceholder(Canvas canvas, Drawable placeholder, int alpha) {
  final int width = getWidth();
  final int height = getHeight();
  placeHolder.setBounds(getPaddingStart(), getPaddingTop(), width - getPaddingEnd(),
      height - getPaddingBottom());
  placeholder.setAlpha(alpha);
  placeholder.setAlpha(alpha);
  placeholder.draw(canvas);
}
 
Example 17
Source File: InnerPainter.java    From NCalendar with Apache License 2.0 4 votes vote down vote up
private void drawCheckedBackground(Canvas canvas, Drawable drawable, RectF rectF, int alphaColor) {
    Rect drawableBounds = DrawableUtil.getDrawableBounds((int) rectF.centerX(), (int) rectF.centerY(), drawable);
    drawable.setBounds(drawableBounds);
    drawable.setAlpha(alphaColor);
    drawable.draw(canvas);
}
 
Example 18
Source File: ToolbarPhone.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * When entering and exiting the TabSwitcher mode, we fade out or fade in the browsing
 * mode of the toolbar on top of the TabSwitcher mode version of it.  We do this by
 * drawing all of the browsing mode views on top of the android view.
 */
protected void drawTabSwitcherAnimationOverlay(Canvas canvas, float animationProgress) {
    if (!isNativeLibraryReady()) return;

    float floatAlpha = 1 - animationProgress;
    int rgbAlpha = (int) (255 * floatAlpha);
    canvas.save();
    canvas.translate(0, -animationProgress * mBackgroundOverlayBounds.height());
    canvas.clipRect(mBackgroundOverlayBounds);

    float previousAlpha = 0.f;
    if (mHomeButton.getVisibility() != View.GONE) {
        // Draw the New Tab button used in the URL view.
        previousAlpha = mHomeButton.getAlpha();
        mHomeButton.setAlpha(previousAlpha * floatAlpha);
        drawChild(canvas, mHomeButton, SystemClock.uptimeMillis());
        mHomeButton.setAlpha(previousAlpha);
    }

    // Draw the location/URL bar.
    previousAlpha = mLocationBar.getAlpha();
    mLocationBar.setAlpha(previousAlpha * floatAlpha);
    // If the location bar is now fully transparent, do not bother drawing it.
    if (mLocationBar.getAlpha() != 0) {
        drawChild(canvas, mLocationBar, SystemClock.uptimeMillis());
    }
    mLocationBar.setAlpha(previousAlpha);

    // Draw the tab stack button and associated text.
    translateCanvasToView(this, mToolbarButtonsContainer, canvas);

    if (mTabSwitcherAnimationTabStackDrawable != null && mToggleTabStackButton != null
            && mUrlExpansionPercent != 1f) {
        // Draw the tab stack button image.
        canvas.save();
        translateCanvasToView(mToolbarButtonsContainer, mToggleTabStackButton, canvas);

        int backgroundWidth = mToggleTabStackButton.getDrawable().getIntrinsicWidth();
        int backgroundHeight = mToggleTabStackButton.getDrawable().getIntrinsicHeight();
        int backgroundLeft = (mToggleTabStackButton.getWidth()
                - mToggleTabStackButton.getPaddingLeft()
                - mToggleTabStackButton.getPaddingRight() - backgroundWidth) / 2;
        backgroundLeft += mToggleTabStackButton.getPaddingLeft();
        int backgroundTop = (mToggleTabStackButton.getHeight()
                - mToggleTabStackButton.getPaddingTop()
                - mToggleTabStackButton.getPaddingBottom() - backgroundHeight) / 2;
        backgroundTop += mToggleTabStackButton.getPaddingTop();
        canvas.translate(backgroundLeft, backgroundTop);

        mTabSwitcherAnimationTabStackDrawable.setAlpha(rgbAlpha);
        mTabSwitcherAnimationTabStackDrawable.draw(canvas);
        canvas.restore();
    }

    // Draw the menu button if necessary.
    if (!mShowMenuBadge && mTabSwitcherAnimationMenuDrawable != null
            && mUrlExpansionPercent != 1f) {
        mTabSwitcherAnimationMenuDrawable.setBounds(
                mMenuButton.getPaddingLeft(), mMenuButton.getPaddingTop(),
                mMenuButton.getWidth() - mMenuButton.getPaddingRight(),
                mMenuButton.getHeight() - mMenuButton.getPaddingBottom());
        translateCanvasToView(mToolbarButtonsContainer, mMenuButton, canvas);
        mTabSwitcherAnimationMenuDrawable.setAlpha(rgbAlpha);
        int color = mUseLightDrawablesForTextureCapture
                ? mLightModeDefaultColor
                : mDarkModeDefaultColor;
        mTabSwitcherAnimationMenuDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        mTabSwitcherAnimationMenuDrawable.draw(canvas);
    }

    // Draw the menu badge if necessary.
    Drawable badgeDrawable = mUseLightDrawablesForTextureCapture
            ? mTabSwitcherAnimationMenuBadgeLightDrawable
                    : mTabSwitcherAnimationMenuBadgeDarkDrawable;
    if (mShowMenuBadge && badgeDrawable != null && mUrlExpansionPercent != 1f) {
        badgeDrawable.setBounds(
                mMenuBadge.getPaddingLeft(), mMenuBadge.getPaddingTop(),
                mMenuBadge.getWidth() - mMenuBadge.getPaddingRight(),
                mMenuBadge.getHeight() - mMenuBadge.getPaddingBottom());
        translateCanvasToView(mToolbarButtonsContainer, mMenuBadge, canvas);
        badgeDrawable.setAlpha(rgbAlpha);
        badgeDrawable.draw(canvas);
    }

    mLightDrawablesUsedForLastTextureCapture = mUseLightDrawablesForTextureCapture;

    canvas.restore();
}
 
Example 19
Source File: AnimatorProperties.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public void set(Drawable d, Integer alpha) {
    d.setAlpha(alpha);
}
 
Example 20
Source File: SkinCompatDrawableManager.java    From Android-skin-support with MIT License 4 votes vote down vote up
static boolean tintDrawableUsingColorFilter(@NonNull Context context,
                                            @DrawableRes final int resId, @NonNull Drawable drawable) {
    PorterDuff.Mode tintMode = DEFAULT_MODE;
    boolean colorAttrSet = false;
    int colorAttr = 0;
    int alpha = -1;

    if (arrayContains(COLORFILTER_TINT_COLOR_CONTROL_NORMAL, resId)) {
        colorAttr = R.attr.colorControlNormal;
        colorAttrSet = true;
    } else if (arrayContains(COLORFILTER_COLOR_CONTROL_ACTIVATED, resId)) {
        colorAttr = R.attr.colorControlActivated;
        colorAttrSet = true;
    } else if (arrayContains(COLORFILTER_COLOR_BACKGROUND_MULTIPLY, resId)) {
        colorAttr = android.R.attr.colorBackground;
        colorAttrSet = true;
        tintMode = PorterDuff.Mode.MULTIPLY;
    } else if (resId == R.drawable.abc_list_divider_mtrl_alpha) {
        colorAttr = android.R.attr.colorForeground;
        colorAttrSet = true;
        alpha = Math.round(0.16f * 255);
    } else if (resId == R.drawable.abc_dialog_material_background) {
        colorAttr = android.R.attr.colorBackground;
        colorAttrSet = true;
    }

    if (colorAttrSet) {
        if (SkinCompatDrawableUtils.canSafelyMutateDrawable(drawable)) {
            drawable = drawable.mutate();
        }

        final int color = getThemeAttrColor(context, colorAttr);
        drawable.setColorFilter(getPorterDuffColorFilter(color, tintMode));

        if (alpha != -1) {
            drawable.setAlpha(alpha);
        }

        if (DEBUG) {
            Log.d(TAG, "[tintDrawableUsingColorFilter] Tinted "
                    + context.getResources().getResourceName(resId) +
                    " with color: #" + Integer.toHexString(color));
        }
        return true;
    }
    return false;
}