Example usage for android.graphics PorterDuffXfermode PorterDuffXfermode

List of usage examples for android.graphics PorterDuffXfermode PorterDuffXfermode

Introduction

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

Prototype

public PorterDuffXfermode(PorterDuff.Mode mode) 

Source Link

Document

Create an xfermode that uses the specified porter-duff mode.

Usage

From source file:com.android.app.MediaPlaybackActivity.java

/**
 * //from ww  w. j a  v a2s  .  c o m
 *
 * @param src
 * @return
 */
private Bitmap createCircleBitmap(Bitmap src) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setARGB(255, 241, 239, 229);

    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap target = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(target);

    canvas.drawCircle(width / 2, width / 2, width / 2, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(src, 0, 0, paint);

    return target;
}

From source file:org.telegram.ui.ChannelAdminLogActivity.java

private TextureView createTextureView(boolean add) {
    if (parentLayout == null) {
        return null;
    }/*  w  w  w .  ja v  a 2s .com*/
    if (roundVideoContainer == null) {
        if (Build.VERSION.SDK_INT >= 21) {
            roundVideoContainer = new FrameLayout(getParentActivity()) {
                @Override
                public void setTranslationY(float translationY) {
                    super.setTranslationY(translationY);
                    contentView.invalidate();
                }
            };
            roundVideoContainer.setOutlineProvider(new ViewOutlineProvider() {
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void getOutline(View view, Outline outline) {
                    outline.setOval(0, 0, AndroidUtilities.roundMessageSize, AndroidUtilities.roundMessageSize);
                }
            });
            roundVideoContainer.setClipToOutline(true);
        } else {
            roundVideoContainer = new FrameLayout(getParentActivity()) {
                @Override
                protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                    super.onSizeChanged(w, h, oldw, oldh);
                    aspectPath.reset();
                    aspectPath.addCircle(w / 2, h / 2, w / 2, Path.Direction.CW);
                    aspectPath.toggleInverseFillType();
                }

                @Override
                public void setTranslationY(float translationY) {
                    super.setTranslationY(translationY);
                    contentView.invalidate();
                }

                @Override
                public void setVisibility(int visibility) {
                    super.setVisibility(visibility);
                    if (visibility == VISIBLE) {
                        setLayerType(View.LAYER_TYPE_HARDWARE, null);
                    }
                }

                @Override
                protected void dispatchDraw(Canvas canvas) {
                    super.dispatchDraw(canvas);
                    canvas.drawPath(aspectPath, aspectPaint);
                }
            };
            aspectPath = new Path();
            aspectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            aspectPaint.setColor(0xff000000);
            aspectPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        }
        roundVideoContainer.setWillNotDraw(false);
        roundVideoContainer.setVisibility(View.INVISIBLE);

        aspectRatioFrameLayout = new AspectRatioFrameLayout(getParentActivity());
        aspectRatioFrameLayout.setBackgroundColor(0);
        if (add) {
            roundVideoContainer.addView(aspectRatioFrameLayout,
                    LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
        }

        videoTextureView = new TextureView(getParentActivity());
        videoTextureView.setOpaque(false);
        aspectRatioFrameLayout.addView(videoTextureView,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
    }
    if (roundVideoContainer.getParent() == null) {
        contentView.addView(roundVideoContainer, 1, new FrameLayout.LayoutParams(
                AndroidUtilities.roundMessageSize, AndroidUtilities.roundMessageSize));
    }
    roundVideoContainer.setVisibility(View.INVISIBLE);
    aspectRatioFrameLayout.setDrawingReady(false);
    return videoTextureView;
}

From source file:com.tct.mail.utils.NotificationUtils.java

/**
 * Crop a square bitmap into a circular one. Used for both contact photos and letter tiles.
 * @param icon Square bitmap to crop/*from  w  w  w .ja va  2  s . c om*/
 * @return Circular bitmap
 */
private static Bitmap cropSquareIconToCircle(Bitmap icon) {
    final int iconWidth = icon.getWidth();
    final Bitmap newIcon = Bitmap.createBitmap(iconWidth, iconWidth, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(newIcon);
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, icon.getWidth(), icon.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(iconWidth / 2, iconWidth / 2, iconWidth / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(icon, rect, rect, paint);

    return newIcon;
}

From source file:g7.bluesky.launcher3.Launcher.java

public void loadIconPack() {
    //theming vars-----------------------------------------------
    PackageManager pm = getPackageManager();
    final int ICONSIZE = Tools.numtodp(64, Launcher.this);
    Resources themeRes = null;//from www.j  a  v a 2 s .  c o  m
    String resPacName = defaultSharedPref.getString(SettingConstants.ICON_THEME_PREF_KEY, "");
    String iconResource = null;
    int intres = 0;
    int intresiconback = 0;
    int intresiconfront = 0;
    int intresiconmask = 0;
    float scaleFactor = 1.0f;

    Paint p = new Paint(Paint.FILTER_BITMAP_FLAG);
    p.setAntiAlias(true);

    Paint origP = new Paint(Paint.FILTER_BITMAP_FLAG);
    origP.setAntiAlias(true);

    Paint maskp = new Paint(Paint.FILTER_BITMAP_FLAG);
    maskp.setAntiAlias(true);
    maskp.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

    if (resPacName.compareTo("") != 0) {
        try {
            themeRes = pm.getResourcesForApplication(resPacName);
        } catch (Exception e) {
        }
        ;
        if (themeRes != null) {
            String[] backAndMaskAndFront = ThemeTools.getIconBackAndMaskResourceName(themeRes, resPacName);
            if (backAndMaskAndFront[0] != null)
                intresiconback = themeRes.getIdentifier(backAndMaskAndFront[0], "drawable", resPacName);
            if (backAndMaskAndFront[1] != null)
                intresiconmask = themeRes.getIdentifier(backAndMaskAndFront[1], "drawable", resPacName);
            if (backAndMaskAndFront[2] != null)
                intresiconfront = themeRes.getIdentifier(backAndMaskAndFront[2], "drawable", resPacName);
        }
    }

    BitmapFactory.Options uniformOptions = new BitmapFactory.Options();
    uniformOptions.inScaled = false;
    uniformOptions.inDither = false;
    uniformOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;

    Canvas origCanv;
    Canvas canvas;
    scaleFactor = ThemeTools.getScaleFactor(themeRes, resPacName);
    Bitmap back = null;
    Bitmap mask = null;
    Bitmap front = null;
    Bitmap scaledBitmap = null;
    Bitmap scaledOrig = null;
    Bitmap orig = null;

    if (resPacName.compareTo("") != 0 && themeRes != null) {
        try {
            if (intresiconback != 0)
                back = BitmapFactory.decodeResource(themeRes, intresiconback, uniformOptions);
        } catch (Exception e) {
        }
        try {
            if (intresiconmask != 0)
                mask = BitmapFactory.decodeResource(themeRes, intresiconmask, uniformOptions);
        } catch (Exception e) {
        }
        try {
            if (intresiconfront != 0)
                front = BitmapFactory.decodeResource(themeRes, intresiconfront, uniformOptions);
        } catch (Exception e) {
        }
    }
    //theming vars-----------------------------------------------
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inDither = true;

    for (int i = 0; i < listApps.size(); i++) {
        if (themeRes != null) {
            iconResource = null;
            intres = 0;
            iconResource = ThemeTools.getResourceName(themeRes, resPacName,
                    listApps.get(i).getComponentName().toString());
            if (iconResource != null) {
                intres = themeRes.getIdentifier(iconResource, "drawable", resPacName);
            }

            if (intres != 0) {//has single drawable for app
                listApps.get(i).setIconBitmap(BitmapFactory.decodeResource(themeRes, intres, uniformOptions));
            } else {
                Drawable drawable = listApps.get(i).getIconDrawable();
                if (drawable == null) {
                    drawable = Utilities.createIconDrawable(listApps.get(i).getIconBitmap());
                }
                orig = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                        Bitmap.Config.ARGB_8888);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                drawable.draw(new Canvas(orig));

                scaledOrig = Bitmap.createBitmap(ICONSIZE, ICONSIZE, Bitmap.Config.ARGB_8888);
                scaledBitmap = Bitmap.createBitmap(ICONSIZE, ICONSIZE, Bitmap.Config.ARGB_8888);
                canvas = new Canvas(scaledBitmap);
                if (back != null) {
                    canvas.drawBitmap(back, Tools.getResizedMatrix(back, ICONSIZE, ICONSIZE), p);
                }

                origCanv = new Canvas(scaledOrig);
                orig = Tools.getResizedBitmap(orig, ((int) (ICONSIZE * scaleFactor)),
                        ((int) (ICONSIZE * scaleFactor)));
                origCanv.drawBitmap(orig,
                        scaledOrig.getWidth() - (orig.getWidth() / 2) - scaledOrig.getWidth() / 2,
                        scaledOrig.getWidth() - (orig.getWidth() / 2) - scaledOrig.getWidth() / 2, origP);

                if (mask != null) {
                    origCanv.drawBitmap(mask, Tools.getResizedMatrix(mask, ICONSIZE, ICONSIZE), maskp);
                }

                if (back != null) {
                    canvas.drawBitmap(Tools.getResizedBitmap(scaledOrig, ICONSIZE, ICONSIZE), 0, 0, p);
                } else
                    canvas.drawBitmap(Tools.getResizedBitmap(scaledOrig, ICONSIZE, ICONSIZE), 0, 0, p);

                if (front != null)
                    canvas.drawBitmap(front, Tools.getResizedMatrix(front, ICONSIZE, ICONSIZE), p);

                listApps.get(i).setIconBitmap(scaledBitmap);
            }
        }
    }
}

From source file:g7.bluesky.launcher3.Launcher.java

public void loadIconPack(List<ShortcutInfo> shortcuts) {
    //theming vars-----------------------------------------------
    PackageManager pm = getPackageManager();
    final int ICONSIZE = Tools.numtodp(128, Launcher.this);
    Resources themeRes = null;// w  ww  .ja  v  a 2 s .  c  o m
    String resPacName = defaultSharedPref.getString(SettingConstants.ICON_THEME_PREF_KEY, "");
    String iconResource = null;
    int intres = 0;
    int intresiconback = 0;
    int intresiconfront = 0;
    int intresiconmask = 0;
    float scaleFactor = 1.0f;

    Paint p = new Paint(Paint.FILTER_BITMAP_FLAG);
    p.setAntiAlias(true);

    Paint origP = new Paint(Paint.FILTER_BITMAP_FLAG);
    origP.setAntiAlias(true);

    Paint maskp = new Paint(Paint.FILTER_BITMAP_FLAG);
    maskp.setAntiAlias(true);
    maskp.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

    if (resPacName.compareTo("") != 0) {
        try {
            themeRes = pm.getResourcesForApplication(resPacName);
        } catch (Exception e) {
        }
        ;
        if (themeRes != null) {
            String[] backAndMaskAndFront = ThemeTools.getIconBackAndMaskResourceName(themeRes, resPacName);
            if (backAndMaskAndFront[0] != null)
                intresiconback = themeRes.getIdentifier(backAndMaskAndFront[0], "drawable", resPacName);
            if (backAndMaskAndFront[1] != null)
                intresiconmask = themeRes.getIdentifier(backAndMaskAndFront[1], "drawable", resPacName);
            if (backAndMaskAndFront[2] != null)
                intresiconfront = themeRes.getIdentifier(backAndMaskAndFront[2], "drawable", resPacName);
        }
    }

    BitmapFactory.Options uniformOptions = new BitmapFactory.Options();
    uniformOptions.inScaled = false;
    uniformOptions.inDither = false;
    uniformOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;

    Canvas origCanv;
    Canvas canvas;
    scaleFactor = ThemeTools.getScaleFactor(themeRes, resPacName);
    Bitmap back = null;
    Bitmap mask = null;
    Bitmap front = null;
    Bitmap scaledBitmap = null;
    Bitmap scaledOrig = null;
    Bitmap orig = null;

    if (resPacName.compareTo("") != 0 && themeRes != null) {
        try {
            if (intresiconback != 0)
                back = BitmapFactory.decodeResource(themeRes, intresiconback, uniformOptions);
        } catch (Exception e) {
        }
        try {
            if (intresiconmask != 0)
                mask = BitmapFactory.decodeResource(themeRes, intresiconmask, uniformOptions);
        } catch (Exception e) {
        }
        try {
            if (intresiconfront != 0)
                front = BitmapFactory.decodeResource(themeRes, intresiconfront, uniformOptions);
        } catch (Exception e) {
        }
    }
    //theming vars-----------------------------------------------
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inDither = true;

    for (int i = 0; i < shortcuts.size(); i++) {
        if (themeRes != null) {
            iconResource = null;
            intres = 0;
            iconResource = ThemeTools.getResourceName(themeRes, resPacName,
                    shortcuts.get(i).getTargetComponent().toString());
            if (iconResource != null) {
                intres = themeRes.getIdentifier(iconResource, "drawable", resPacName);
            }

            if (intres != 0) {//has single drawable for app
                shortcuts.get(i).setIcon(BitmapFactory.decodeResource(themeRes, intres, uniformOptions));
            } else {

                Drawable drawable = Utilities.createIconDrawable(shortcuts.get(i).getIcon(mIconCache));
                orig = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                        Bitmap.Config.ARGB_8888);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                drawable.draw(new Canvas(orig));

                scaledOrig = Bitmap.createBitmap(ICONSIZE, ICONSIZE, Bitmap.Config.ARGB_8888);
                scaledBitmap = Bitmap.createBitmap(ICONSIZE, ICONSIZE, Bitmap.Config.ARGB_8888);
                canvas = new Canvas(scaledBitmap);
                if (back != null) {
                    canvas.drawBitmap(back, Tools.getResizedMatrix(back, ICONSIZE, ICONSIZE), p);
                }

                origCanv = new Canvas(scaledOrig);
                orig = Tools.getResizedBitmap(orig, ((int) (ICONSIZE * scaleFactor)),
                        ((int) (ICONSIZE * scaleFactor)));
                origCanv.drawBitmap(orig,
                        scaledOrig.getWidth() - (orig.getWidth() / 2) - scaledOrig.getWidth() / 2,
                        scaledOrig.getWidth() - (orig.getWidth() / 2) - scaledOrig.getWidth() / 2, origP);

                if (mask != null) {
                    origCanv.drawBitmap(mask, Tools.getResizedMatrix(mask, ICONSIZE, ICONSIZE), maskp);
                }

                if (back != null) {
                    canvas.drawBitmap(Tools.getResizedBitmap(scaledOrig, ICONSIZE, ICONSIZE), 0, 0, p);
                } else
                    canvas.drawBitmap(Tools.getResizedBitmap(scaledOrig, ICONSIZE, ICONSIZE), 0, 0, p);

                if (front != null)
                    canvas.drawBitmap(front, Tools.getResizedMatrix(front, ICONSIZE, ICONSIZE), p);

                shortcuts.get(i).setIcon(scaledBitmap);
            }
        }
    }
}