Example usage for android.graphics BitmapShader BitmapShader

List of usage examples for android.graphics BitmapShader BitmapShader

Introduction

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

Prototype

private BitmapShader(Bitmap bitmap, int tileX, int tileY) 

Source Link

Usage

From source file:Main.java

public static Bitmap createRoundedBitmap(Context context, Bitmap bitmap, int leftTop, int rightTop,
        int leftBottm, int rightBottom) {
    Bitmap bmp;/*from  ww w  .ja v a2 s  . c  o  m*/

    bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);

    float radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftTop,
            context.getResources().getDisplayMetrics());
    Canvas canvas = new Canvas(bmp);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
    canvas.drawRoundRect(rect, radius, radius, paint);
    return bmp;
}

From source file:me.uucky.colorpicker.internal.ColorView.java

public ColorView(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);
    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ColorView);
    mRect = new RectF();
    mColorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    final Bitmap bitmap = Utils
            .getAlphaPatternBitmap(Math.round(getResources().getDisplayMetrics().density * 8));
    mAlphaPatternPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mAlphaPatternPaint.setShader(new BitmapShader(bitmap, TileMode.REPEAT, TileMode.REPEAT));
    mRadius = a.getDimension(R.styleable.ColorView_cp_cornerRadius, 0);
    ViewCompat.setElevation(this, a.getDimension(R.styleable.ColorView_cp_elevation, 0));
    a.recycle();/*ww  w. java  2 s  .c  o m*/
    if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
        Internal.setOutlineProvider(this);
    }
}

From source file:android.support.v7.widget.AppCompatProgressBarHelper.java

/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 *//*from  w ww . ja v a  2  s . co  m*/
private Drawable tileify(Drawable drawable, boolean clip) {
    if (drawable instanceof DrawableWrapper) {
        Drawable inner = ((DrawableWrapper) drawable).getWrappedDrawable();
        if (inner != null) {
            inner = tileify(inner, clip);
            ((DrawableWrapper) drawable).setWrappedDrawable(inner);
        }
    } else if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i),
                    (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }
        LayerDrawable newBg = new LayerDrawable(outDrawables);

        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }

        return newBg;

    } else if (drawable instanceof BitmapDrawable) {
        final BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        final Bitmap tileBitmap = bitmapDrawable.getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }

        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
        final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT,
                Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);
        shapeDrawable.getPaint().setColorFilter(bitmapDrawable.getPaint().getColorFilter());
        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL) : shapeDrawable;
    }

    return drawable;
}

From source file:android.support.v7.internal.widget.TintRatingBar.java

/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 *//*  w  w w  . j  a  va  2  s  .  c o m*/
private Drawable tileify(Drawable drawable, boolean clip) {
    if (drawable instanceof DrawableWrapper) {
        Drawable inner = ((DrawableWrapper) drawable).getWrappedDrawable();
        if (inner != null) {
            inner = tileify(inner, clip);
            ((DrawableWrapper) drawable).setWrappedDrawable(inner);
        }
    } else if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i),
                    (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }
        LayerDrawable newBg = new LayerDrawable(outDrawables);

        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }

        return newBg;

    } else if (drawable instanceof BitmapDrawable) {
        final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }

        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
        final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT,
                Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);
        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL) : shapeDrawable;
    }

    return drawable;
}

From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java

private static Shader createShader(Bitmap b) {
    return new BitmapShader(b, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}

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

private void init(Context context, AttributeSet attrs) {
    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ColorPanelView);
    shape = a.getInt(R.styleable.ColorPanelView_cpv_colorShape, ColorShape.CIRCLE);
    showOldColor = a.getBoolean(R.styleable.ColorPanelView_cpv_showOldColor, false);
    if (showOldColor && shape != ColorShape.CIRCLE) {
        throw new IllegalStateException("Color preview is only available in circle mode");
    }/*  ww w . j  a v  a  2 s  .co  m*/
    borderColor = a.getColor(R.styleable.ColorPanelView_cpv_borderColor, DEFAULT_BORDER_COLOR);
    a.recycle();
    if (borderColor == DEFAULT_BORDER_COLOR) {
        // If no specific border color has been set we take the default secondary text color as border/slider color.
        // Thus it will adopt to theme changes automatically.
        final TypedValue value = new TypedValue();
        TypedArray typedArray = context.obtainStyledAttributes(value.data,
                new int[] { android.R.attr.textColorSecondary });
        borderColor = typedArray.getColor(0, borderColor);
        typedArray.recycle();
    }
    borderWidthPx = DrawingUtils.dpToPx(context, 1);
    borderPaint = new Paint();
    borderPaint.setAntiAlias(true);
    colorPaint = new Paint();
    colorPaint.setAntiAlias(true);
    if (showOldColor) {
        originalPaint = new Paint();
    }
    if (shape == ColorShape.CIRCLE) {
        Bitmap bitmap = ((BitmapDrawable) context.getResources().getDrawable(R.drawable.cpv_alpha)).getBitmap();
        alphaPaint = new Paint();
        alphaPaint.setAntiAlias(true);
        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        alphaPaint.setShader(shader);
    }
}

From source file:de.vanita5.twittnuker.view.ShapedImageView.java

/**
 * Given the source bitmap and a canvas, draws the bitmap through a circular
 * mask. Only draws a circle with diameter equal to the destination width.
 *
 * @param bitmap The source bitmap to draw.
 * @param canvas The canvas to draw it on.
 * @param source The source bound of the bitmap.
 * @param dest   The destination bound on the canvas.
 *///from w ww.  j a v a  2s  .  c o m
public void drawBitmapWithCircleOnCanvas(Bitmap bitmap, Canvas canvas, RectF source, @NonNull RectF dest) {
    if (bitmap == null) {
        if (getStyle() == SHAPE_CIRCLE) {
            canvas.drawCircle(dest.centerX(), dest.centerY(), Math.min(dest.width(), dest.height()) / 2f,
                    mSolidColorPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mSolidColorPaint);
        }
        return;
    }
    // Draw bitmap through shader first.
    final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mMatrix.reset();

    switch (getScaleType()) {
    case CENTER_CROP: {
        final float srcRatio = source.width() / source.height();
        final float dstRatio = dest.width() / dest.height();
        if (srcRatio > dstRatio) {
            // Source is wider than destination, fit height
            mTempDestination.top = dest.top;
            mTempDestination.bottom = dest.bottom;
            final float dstWidth = dest.height() * srcRatio;
            mTempDestination.left = dest.centerX() - dstWidth / 2;
            mTempDestination.right = dest.centerX() + dstWidth / 2;
        } else if (srcRatio < dstRatio) {
            mTempDestination.left = dest.left;
            mTempDestination.right = dest.right;
            final float dstHeight = dest.width() / srcRatio;
            mTempDestination.top = dest.centerY() - dstHeight / 2;
            mTempDestination.bottom = dest.centerY() + dstHeight / 2;
        } else {
            mTempDestination.set(dest);
        }
        break;
    }
    default: {
        mTempDestination.set(dest);
        break;
    }
    }

    // Fit bitmap to bounds.
    mMatrix.setRectToRect(source, mTempDestination, ScaleToFit.CENTER);

    shader.setLocalMatrix(mMatrix);
    mBitmapPaint.setShader(shader);

    if (getStyle() == SHAPE_CIRCLE) {
        canvas.drawCircle(dest.centerX(), dest.centerY(), Math.min(dest.width(), dest.height()) / 2f,
                mBitmapPaint);
    } else {
        final float cornerRadius = getCalculatedCornerRadius();
        canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint);
    }
}

From source file:org.zeroxlab.benchmark.Benchmark.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    orientation = getResources().getConfiguration().orientation;
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, TAG);
    mWakeLock.acquire();//from  ww  w.  jav  a  2 s .  c  o  m
    sTexture = new BitmapShader(BitmapFactory.decodeResource(getResources(), R.drawable.crate), TileMode.REPEAT,
            TileMode.REPEAT);

    setContentView(R.layout.main);
    mCases = new LinkedList<Case>();
    Case arith = new CaseArithmetic();
    Case javascript = new CaseJavascript();
    Case scimark2 = new CaseScimark2();
    Case glcube = new CaseGLCube();
    Case nehe08 = new CaseNeheLesson08();
    Case nehe16 = new CaseNeheLesson16();
    Case teapot = new CaseTeapot();
    Case gc = new CaseGC();
    Case libMicro = new NativeCaseMicro();
    Case libUbench = new NativeCaseUbench();

    Case canvas = new CaseCanvas(true, false, false);
    Case dc2 = new CaseDrawCircle2();
    Case dr = new CaseDrawRect();
    Case da = new CaseDrawArc();
    Case di = new CaseDrawImage();
    Case dt = new CaseDrawText();

    mCategory.put(D2, new HashSet<Case>());
    mCategory.put(D2HW, new HashSet<Case>());
    mCategory.put(D2SW1, new HashSet<Case>());
    mCategory.put(D2SW2, new HashSet<Case>());
    mCategory.put(D3, new HashSet<Case>());
    mCategory.put(MATH, new HashSet<Case>());
    mCategory.put(VM, new HashSet<Case>());
    mCategory.put(NATIVE, new HashSet<Case>());
    mCategory.put(MISC, new HashSet<Case>());

    // mflops
    mCases.add(arith);
    mCases.add(scimark2);
    mCases.add(javascript);
    mCategory.get(MATH).add(arith);
    mCategory.get(MATH).add(scimark2);
    mCategory.get(MISC).add(javascript);

    // 2d
    mCases.add(canvas);
    mCases.add(dc2);
    mCases.add(dr);
    mCases.add(da);
    mCases.add(di);
    mCases.add(dt);

    mCategory.get(D2).add(canvas);
    mCategory.get(D2).add(dc2);
    mCategory.get(D2).add(dr);
    mCategory.get(D2).add(da);
    mCategory.get(D2).add(di);
    mCategory.get(D2).add(dt);

    Case canvas2 = new CaseCanvas(false, false, false);//HW
    Case canvas3 = new CaseCanvas(false, true, false);//SW Window
    Case canvas4 = new CaseCanvas(false, false, true);//SW Layer
    Case circle = new CaseDrawCircle(false, false, false);//HW
    Case circle2 = new CaseDrawCircle(false, true, false);//SW Window
    Case circle3 = new CaseDrawCircle(false, false, true);//SW Layer
    Case dc22 = new CaseDrawCircle2(false, false, false);//HW
    Case dc23 = new CaseDrawCircle2(false, true, false);//SW Window
    Case dc24 = new CaseDrawCircle2(false, false, true);//SW Layer
    Case dr2 = new CaseDrawRect(false, false, false);//HW
    Case dr3 = new CaseDrawRect(false, true, false);//SW Window
    Case dr4 = new CaseDrawRect(false, false, true);//SW Layer
    Case da2 = new CaseDrawArc(false, false, false);//HW
    Case da3 = new CaseDrawArc(false, true, false);//SW Window
    Case da4 = new CaseDrawArc(false, false, true);//SW Layer
    Case di2 = new CaseDrawImage(false, false, false);//HW
    Case di3 = new CaseDrawImage(false, true, false);//SW Window
    Case di4 = new CaseDrawImage(false, false, true);//SW Layer
    Case dt2 = new CaseDrawText(false, false, false);//HW
    Case dt3 = new CaseDrawText(false, true, false);//SW Window
    Case dt4 = new CaseDrawText(false, false, true);//SW Layer

    mCases.add(canvas2);
    mCases.add(circle);
    mCases.add(dc22);
    mCases.add(dr2);
    mCases.add(da2);
    mCases.add(di2);
    mCases.add(dt2);

    mCases.add(canvas3);
    mCases.add(circle2);
    mCases.add(dc23);
    mCases.add(dr3);
    mCases.add(da3);
    mCases.add(di3);
    mCases.add(dt3);

    mCases.add(canvas4);
    mCases.add(circle3);
    mCases.add(dc24);
    mCases.add(dr4);
    mCases.add(da4);
    mCases.add(di4);
    mCases.add(dt4);

    //2d(HW)
    mCategory.get(D2HW).add(canvas2);
    mCategory.get(D2HW).add(circle);
    mCategory.get(D2HW).add(dc22);
    mCategory.get(D2HW).add(da2);
    mCategory.get(D2HW).add(dr2);
    mCategory.get(D2HW).add(di2);
    mCategory.get(D2HW).add(dt2);

    //2d(SW1)
    mCategory.get(D2SW1).add(canvas3);
    mCategory.get(D2SW1).add(circle2);
    mCategory.get(D2SW1).add(dc23);
    mCategory.get(D2SW1).add(da3);
    mCategory.get(D2SW1).add(dr3);
    mCategory.get(D2SW1).add(di3);
    mCategory.get(D2SW1).add(dt3);

    //2d(SW2)
    mCategory.get(D2SW2).add(canvas4);
    mCategory.get(D2SW2).add(circle3);
    mCategory.get(D2SW2).add(dc24);
    mCategory.get(D2SW2).add(da4);
    mCategory.get(D2SW2).add(dr4);
    mCategory.get(D2SW2).add(di4);
    mCategory.get(D2SW2).add(dt4);

    // 3d
    mCases.add(glcube);
    mCases.add(nehe08);
    mCases.add(nehe16);
    mCases.add(teapot);

    mCategory.get(D3).add(glcube);
    mCategory.get(D3).add(nehe08);
    mCategory.get(D3).add(nehe16);
    mCategory.get(D3).add(teapot);

    // vm
    mCases.add(gc);
    mCategory.get(VM).add(gc);

    // native
    mCases.add(libMicro);
    mCases.add(libUbench);

    mCategory.get(NATIVE).add(libMicro);
    mCategory.get(NATIVE).add(libUbench);

    initViews();

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        mAutoRun = bundle.getBoolean("autorun");
        mCheckMath = bundle.getBoolean("math");
        mCheck2D = bundle.getBoolean("2d");
        mCheck3D = bundle.getBoolean("3d");
        mCheckVM = bundle.getBoolean("vm");
        mCheckNative = bundle.getBoolean("native");
        mAutoUpload = bundle.getBoolean("autoupload");
    }

    if (mCheckMath && !mathCheckBox.isChecked()) {
        mathCheckBox.performClick();
    }

    if (mCheck2D && !d2CheckBox.isChecked()) {
        d2CheckBox.performClick();
    }

    if (mCheck3D && !d3CheckBox.isChecked()) {
        d3CheckBox.performClick();
    }

    if (mCheckVM && !vmCheckBox.isChecked()) {
        vmCheckBox.performClick();
    }

    if (mCheckNative && !nativeCheckBox.isChecked()) {
        nativeCheckBox.performClick();
    }

    if (mCheckMisc && !miscCheckBox.isChecked()) {
        miscCheckBox.performClick();
    }
    /*
    if (intent.getBooleanExtra("AUTO", false)) {
    ImageView head = (ImageView)findViewById(R.id.banner_img);
    head.setImageResource(R.drawable.icon_auto);
    mTouchable = false;
    initAuto();
    }
    */
    if (mAutoRun) {
        onClick(mRun);
    }
}

From source file:org.getlantern.firetweet.view.ShapedImageView.java

/**
 * Given the source bitmap and a canvas, draws the bitmap through a circular
 * mask. Only draws a circle with diameter equal to the destination width.
 *
 * @param bitmap The source bitmap to draw.
 * @param canvas The canvas to draw it on.
 * @param source The source bound of the bitmap.
 * @param dest   The destination bound on the canvas.
 *//*from w w  w .  j a  v  a  2s. c  o  m*/
public void drawBitmapWithCircleOnCanvas(Bitmap bitmap, Canvas canvas, RectF source, @NonNull RectF dest) {
    if (bitmap == null) {
        if (getStyle() == SHAPE_CIRCLE) {
            canvas.drawCircle(dest.centerX(), dest.centerY(), Math.min(dest.width(), dest.height()) / 2f,
                    mSolidColorPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mSolidColorPaint);
        }
        return;
    }
    // Draw bitmap through shader first.
    final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mMatrix.reset();

    switch (getScaleType()) {
    case CENTER_CROP: {
        final float srcRatio = source.width() / source.height();
        final float dstRatio = dest.width() / dest.height();
        if (srcRatio > dstRatio) {
            // Source is wider than destination, fit height
            mTempDestination.top = dest.top;
            mTempDestination.bottom = dest.bottom;
            final float dstWidth = dest.height() * srcRatio;
            mTempDestination.left = dest.centerX() - dstWidth / 2;
            mTempDestination.right = dest.centerX() + dstWidth / 2;
        } else if (srcRatio < dstRatio) {
            mTempDestination.left = dest.left;
            mTempDestination.right = dest.right;
            final float dstHeight = dest.width() / srcRatio;
            mTempDestination.top = dest.centerY() - dstHeight / 2;
            mTempDestination.bottom = dest.centerY() + dstHeight / 2;
        } else {
            mTempDestination.set(dest);
        }
        break;
    }
    default: {
        mTempDestination.set(dest);
        break;
    }
    }

    // Fit bitmap to bounds.
    mMatrix.setRectToRect(source, mTempDestination, ScaleToFit.CENTER);

    shader.setLocalMatrix(mMatrix);
    mBitmapPaint.setShader(shader);

    if (mBorderEnabled) {
        final float inset = mBorderPaint.getStrokeWidth() / 2;
        if (getStyle() == SHAPE_CIRCLE) {
            final float circleRadius = Math.min(dest.width(), dest.height()) / 2f - inset / 2;
            canvas.drawCircle(dest.centerX(), dest.centerY(), circleRadius, mBitmapPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            dest.inset(inset, inset);
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint);
            dest.inset(-inset, -inset);
        }
    } else {
        if (getStyle() == SHAPE_CIRCLE) {
            final float circleRadius = Math.min(dest.width(), dest.height()) / 2f;
            canvas.drawCircle(dest.centerX(), dest.centerY(), circleRadius, mBitmapPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint);
        }
    }

}

From source file:com.liferay.mobile.screens.viewsets.defaultviews.userportrait.UserPortraitView.java

protected Paint getPaint(Bitmap bitmap) {
    BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from   www . ja  va 2 s  .co m*/
    paint.setShader(shader);
    return paint;
}