android.graphics.Shader Java Examples

The following examples show how to use android.graphics.Shader. 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: MusicPlayerView.java    From IdealMedia with Apache License 2.0 6 votes vote down vote up
/**
 * Create shader and set shader to mPaintCover
 */
private void createShader() {

    if (mWidth == 0)
        return;

    //if mBitmapCover is null then create default colored cover
    if (mBitmapCover == null) {
        mBitmapCover = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        mBitmapCover.eraseColor(mCoverColor);
    }

    mCoverScale = ((float) mWidth) / (float) mBitmapCover.getWidth();

    mBitmapCover = Bitmap.createScaledBitmap(mBitmapCover,
            (int) (mBitmapCover.getWidth() * mCoverScale),
            (int) (mBitmapCover.getHeight() * mCoverScale),
            true);

    mShader = new BitmapShader(mBitmapCover, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mPaintCover = new Paint();
    mPaintCover.setAntiAlias(true);
    mPaintCover.setShader(mShader);

}
 
Example #2
Source File: OpacityBar.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Set the bar color. <br>
 * <br>
 * Its discouraged to use this method.
 * 
 * @param color
 */
public void setColor(int color) {
	int x1, y1;
	if (mOrientation == ORIENTATION_HORIZONTAL) {
		x1 = (mBarLength + mBarPointerHaloRadius);
		y1 = mBarThickness;
	} else {
		x1 = mBarThickness;
		y1 = (mBarLength + mBarPointerHaloRadius);
	}

	Color.colorToHSV(color, mHSVColor);
	shader = new LinearGradient(mBarPointerHaloRadius, 0, x1, y1,
			new int[] { Color.HSVToColor(0x00, mHSVColor), color }, null,
			Shader.TileMode.CLAMP);
	mBarPaint.setShader(shader);
	calculateColor(mBarPointerPosition);
	mBarPointerPaint.setColor(mColor);
	if (mPicker != null) {
		mPicker.setNewCenterColor(mColor);
	}
	invalidate();
}
 
Example #3
Source File: FlikerProgressBar.java    From FlickerProgressBar with Apache License 2.0 6 votes vote down vote up
/**
 * 进度
 */
private void drawProgress(Canvas canvas) {
    pgPaint.setColor(progressColor);

    float right = (progress / maxProgress) * getMeasuredWidth();
    pgCanvas.save(Canvas.CLIP_SAVE_FLAG);
    pgCanvas.clipRect(0, 0, right, getMeasuredHeight());
    pgCanvas.drawColor(progressColor);
    pgCanvas.restore();

    if(!isStop){
        pgPaint.setXfermode(xfermode);
        pgCanvas.drawBitmap(flikerBitmap, flickerLeft, 0, pgPaint);
        pgPaint.setXfermode(null);
    }

    //控制显示区域
    bitmapShader = new BitmapShader(pgBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    pgPaint.setShader(bitmapShader);
    canvas.drawRoundRect(bgRectf, radius, radius, pgPaint);
}
 
Example #4
Source File: GradientRunnable.java    From AnimatedGradientTextView with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    long currentTime = SystemClock.uptimeMillis();
    long delta = currentTime - lastTime;

    totalDelta += delta;
    float totalPercentage = totalDelta / ((float) speed);
    totalPercentage = totalPercentage > 1 ? 1 : totalPercentage;

    for (int colorIndex = 0; colorIndex < currentColors.length; colorIndex++) {
        currentColors[colorIndex] = (int) (new ArgbEvaluator().evaluate(totalPercentage, colors[(currentGradient + colorIndex) % colors.length], colors[(currentGradient + (colorIndex + 1)) % colors.length]));
    }

    if (totalPercentage == 1) {
        totalDelta = 0;
        currentGradient = (currentGradient + 1) % colors.length;
    }

    Shader shader = new LinearGradient(gradientsPositions[0].x, gradientsPositions[0].y, gradientsPositions[1].x, gradientsPositions[1].y, currentColors, null, Shader.TileMode.CLAMP);
    textView.getPaint().setShader(shader);

    textView.postInvalidate();
    lastTime = currentTime;
}
 
Example #5
Source File: Utils.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void setShadow(View view, Drawable sd) {
    RoundRectShape rss = new RoundRectShape(new float[]{12f, 12f, 12f,
            12f, 12f, 12f, 12f, 12f}, null, null);
    ShapeDrawable sds = new ShapeDrawable(rss);
    sds.setShaderFactory(new ShapeDrawable.ShaderFactory() {

        @Override
        public Shader resize(int width, int height) {
            LinearGradient lg = new LinearGradient(0, 0, 0, height,
                    new int[]{Color.parseColor("#e5e5e5"),
                            Color.parseColor("#e5e5e5"),
                            Color.parseColor("#e5e5e5"),
                            Color.parseColor("#e5e5e5")}, new float[]{0,
                    0.50f, 0.50f, 1}, Shader.TileMode.REPEAT);
            return lg;
        }
    });

    LayerDrawable ld = new LayerDrawable(new Drawable[]{sds, sd});
    ld.setLayerInset(0, 5, 5, 0, 0); // inset the shadow so it doesn't start right at the left/top
    ld.setLayerInset(1, 0, 0, 5, 5); // inset the top drawable so we can leave a bit of space for the shadow to use

    view.setBackgroundDrawable(ld);
}
 
Example #6
Source File: CustomButton.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
public CustomButton(Context context) {
	super(context);
	paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	paint.setStyle(Style.FILL);
	paint.setColor(Color.BLACK);
	paint.setTextSize(40);

	fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	fillPaint.setStyle(Style.FILL);
	fillPaint.setColor(Color.RED);
	CornerPathEffect effect = new CornerPathEffect(30);
	fillPaint.setPathEffect(effect);
	fillPaint.setStyle(Style.FILL_AND_STROKE);
	fillPaint.setShader(new LinearGradient(0F, 120, getWidth(),
			getHeight(), Color.CYAN, Color.RED, Shader.TileMode.CLAMP));

}
 
Example #7
Source File: VolumeView.java    From zone-sdk with MIT License 6 votes vote down vote up
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mWidth = getWidth();
    mRectHeight = getHeight();
    mRectWidth = (int) (mWidth * 0.6 / mRectCount);
    mLinearGradient = new LinearGradient(
            0,
            0,
            mRectWidth,
            mRectHeight,
            Color.YELLOW,
            Color.BLUE,
            Shader.TileMode.CLAMP);
    mPaint.setShader(mLinearGradient);
}
 
Example #8
Source File: GradientStrokeContent.java    From lottie-android with Apache License 2.0 6 votes vote down vote up
@Override public void draw(Canvas canvas, Matrix parentMatrix, int parentAlpha) {
  if (hidden) {
    return;
  }
  getBounds(boundsRect, parentMatrix, false);

  Shader shader;
  if (type == GradientType.LINEAR) {
    shader = getLinearGradient();
  } else {
    shader = getRadialGradient();
  }
  shader.setLocalMatrix(parentMatrix);
  paint.setShader(shader);

  super.draw(canvas, parentMatrix, parentAlpha);
}
 
Example #9
Source File: OpacityBar.java    From HoloColorPicker with Apache License 2.0 6 votes vote down vote up
/**
 * Set the bar color. <br>
 * <br>
 * Its discouraged to use this method.
 * 
 * @param color
 */
public void setColor(int color) {
	int x1, y1;
	if(mOrientation == ORIENTATION_HORIZONTAL) {
		x1 = (mBarLength + mBarPointerHaloRadius);
		y1 = mBarThickness;
	}
	else {
		x1 = mBarThickness;
		y1 = (mBarLength + mBarPointerHaloRadius);
	}
	
	Color.colorToHSV(color, mHSVColor);
	shader = new LinearGradient(mBarPointerHaloRadius, 0,
			x1, y1, new int[] {
					Color.HSVToColor(0x00, mHSVColor), color }, null,
			Shader.TileMode.CLAMP);
	mBarPaint.setShader(shader);
	calculateColor(mBarPointerPosition);
	mBarPointerPaint.setColor(mColor);
	if (mPicker != null) {
		mPicker.setNewCenterColor(mColor);
	}
	invalidate();
}
 
Example #10
Source File: SyncPlayFragment.java    From Musync with MIT License 6 votes vote down vote up
public Bitmap addGradient(Bitmap originalBitmap){

        int width = originalBitmap.getWidth();
        int height = originalBitmap.getHeight();
        Bitmap updatedBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(updatedBitmap);
        canvas.drawBitmap(originalBitmap,0,0,null);
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0,0,0,height,getResources().getColor(R.color.colorAccent),getResources().getColor(R.color.colorAccent1), Shader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawRect(0,0,width,height,paint);

        return updatedBitmap;

    }
 
Example #11
Source File: TilesOverlay.java    From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Draw a 'loading' placeholder with a canvas.
 */
private SafePaint getLoadingTilePaint() {
    if (mLoadingTilePaint == null && mLoadingBackgroundColor != Color.TRANSPARENT) {
        try {
            final int tileSize =
                    mTileProvider.getTileSource() != null ? mTileProvider.getTileSource()
                            .getTileSizePixels() : 256;
            mLoadingTileBitmap =
                    Bitmap.createBitmap(tileSize, tileSize, Bitmap.Config.ARGB_8888);
            final Canvas canvas = new Canvas(mLoadingTileBitmap);
            canvas.drawColor(mLoadingBackgroundColor);
            final int lineSize = tileSize / 16;
            for (int a = 0; a < tileSize; a += lineSize) {
                canvas.drawLine(0, a, tileSize, a, mLoadingPaint);
                canvas.drawLine(a, 0, a, tileSize, mLoadingPaint);
            }
            mLoadingTilePaint = new SafePaint();
            mLoadingTilePaint.setShader(new BitmapShader(mLoadingTileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
        } catch (final OutOfMemoryError e) {
            Log.e(TAG, "OutOfMemoryError getting loading tile: " + e.toString());
            System.gc();
        }
    }
    return mLoadingTilePaint;
}
 
Example #12
Source File: ImageUtils.java    From Asteroid with Apache License 2.0 6 votes vote down vote up
public static Bitmap gradientBitmap(Bitmap src, int color1, int color2) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);

    canvas.drawBitmap(src, 0, 0, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, 0, 0, height, color1, color2, Shader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawRect(0, 0, width, height, paint);

    return result;
}
 
Example #13
Source File: ShrinkingTextView.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    if (mInteractive && !mExpanded || isAnimating()) {
        Rect r = new Rect();
        this.getDrawingRect(r);
        Paint p = new Paint();

        float f = this.getTextSize();
        int px = (int)(f / 2);

        int bottom = r.bottom - this.getPaddingBottom();
        int top = bottom - px;

        int start = Color.argb(0, 255, 255, 255);
        int end = Color.argb(220, 255, 255, 255);

        Shader shader = new LinearGradient(r.left, top, r.left, bottom, start, end, TileMode.CLAMP);
        p.setShader(shader);

        r.set(r.left, top, r.right, bottom);
        p.setStyle(Paint.Style.FILL);
        canvas.drawRect(r, p);
    }
}
 
Example #14
Source File: BubbleImageView.java    From CloudPan with Apache License 2.0 6 votes vote down vote up
private void setup() {
	if (mBitmap == null) {
		return;
	}

	mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP,
			Shader.TileMode.CLAMP);

	mBitmapPaint = new Paint();
	mBitmapPaint.setAntiAlias(true);
	mBitmapPaint.setShader(mBitmapShader);

	mBitmapHeight = mBitmap.getHeight();
	mBitmapWidth = mBitmap.getWidth();

	updateShaderMatrix();
	invalidate();
}
 
Example #15
Source File: RoundedImageView.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
private Bitmap transform(Bitmap source) {
    final RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight());
    final Bitmap result = createBitmap(source.getWidth(), source.getHeight(),
            source.getConfig());
    final BitmapShader bitmapShader = new BitmapShader(source, Shader.TileMode.CLAMP,
            Shader.TileMode.CLAMP);

    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(bitmapShader);

    final Path path = new Path();
    path.addRoundRect(rect, roundedCornerRadii, Path.Direction.CCW);

    final Canvas canvas = new Canvas(result);
    canvas.drawPath(path, paint);
    return result;
}
 
Example #16
Source File: ShimmerViewHelper.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
private void resetLinearGradient() {

        // our gradient is a simple linear gradient from textColor to reflectionColor. its axis is at the center
        // when it's outside of the view, the outer color (textColor) will be repeated (Shader.TileMode.CLAMP)
        // initially, the linear gradient is positioned on the left side of the view
        linearGradient = new LinearGradient(-view.getWidth(), 0, 0, 0,
                new int[]{
                        primaryColor,
                        reflectionColor,
                        primaryColor,
                },
                new float[]{
                        0,
                        0.5f,
                        1
                },
                Shader.TileMode.CLAMP
        );

        paint.setShader(linearGradient);
    }
 
Example #17
Source File: ValueBar.java    From PhotoEdit with Apache License 2.0 6 votes vote down vote up
/**
 * Set the bar color. <br>
 * <br>
 * Its discouraged to use this method.
 *
 * @param color
 */
public void setColor(int color) {
    int x1, y1;
    if(mOrientation == ORIENTATION_HORIZONTAL) {
        x1 = (mBarLength + mBarPointerHaloRadius);
        y1 = mBarThickness;
    }
    else {
        x1 = mBarThickness;
        y1 = (mBarLength + mBarPointerHaloRadius);
    }

    Color.colorToHSV(color, mHSVColor);
    shader = new LinearGradient(mBarPointerHaloRadius, 0,
            x1, y1, new int[] {
            color, Color.BLACK }, null, Shader.TileMode.CLAMP);
    mBarPaint.setShader(shader);
    calculateColor(mBarPointerPosition);
    mBarPointerPaint.setColor(mColor);
    if (mPicker != null) {
        mPicker.setNewCenterColor(mColor);
        if(mPicker.hasOpacityBar())
            mPicker.changeOpacityBarColor(mColor);
    }
    invalidate();
}
 
Example #18
Source File: RoundedTopTransformation.java    From Twire with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
    int w = toTransform.getWidth();
    int h = toTransform.getHeight();
    Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmp);
    Shader shader = new BitmapShader(toTransform, Shader.TileMode.MIRROR,
            Shader.TileMode.MIRROR);

    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    paint.setAntiAlias(true);
    paint.setShader(shader);
    RectF rec = new RectF(0, 0, w, h - (h / 3));
    c.drawRect(new RectF(0, (h / 3), w, h), paint);
    c.drawRoundRect(rec, cornerRadius, cornerRadius, paint);
    toTransform.recycle();
    return bmp;
}
 
Example #19
Source File: ColorPiker.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
private void adjustSqureColor(int color) {

        int squareWith = ivSquareColor.getWidth();
        int squareHight = ivSquareColor.getHeight();
        bitmapsqure = Bitmap.createBitmap(squareWith, squareHight, Bitmap.Config.ARGB_8888);
        bitmapsqure.eraseColor(Color.GREEN);
        ivSquareColor.setImageBitmap(bitmapsqure);
        Canvas canvasSquare = new Canvas(bitmapsqure);

        Paint mPaint = new Paint();
        mPaint.setStrokeWidth(1);

        updateMainColors(color, squareWith);

        for (int i = 0; i < squareWith; i++) {
            int[] colors = new int[2];
            colors[0] = mMainColors[i];
            colors[1] = Color.BLACK;
            Shader shader = new LinearGradient(0, 0, 0, squareHight, colors, null, Shader.TileMode.REPEAT);
            mPaint.setShader(shader);
            canvasSquare.drawLine(i, 0, i, squareHight, mPaint);
        }

        mPaint.setShader(null);
    }
 
Example #20
Source File: RoundedDrawable.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
public RoundedDrawable(Bitmap bitmap) {

    mBitmapWidth = bitmap.getWidth();
    mBitmapHeight = bitmap.getHeight();
    mBitmapRect.set(0, 0, mBitmapWidth, mBitmapHeight);

    mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mBitmapShader.setLocalMatrix(mShaderMatrix);

    mBitmapPaint = new Paint();
    mBitmapPaint.setStyle(Paint.Style.FILL);
    mBitmapPaint.setAntiAlias(true);
    mBitmapPaint.setShader(mBitmapShader);

    mBorderPaint = new Paint();
    mBorderPaint.setStyle(Paint.Style.STROKE);
    mBorderPaint.setAntiAlias(true);
    mBorderPaint.setColor(mBorderColor.getColorForState(getState(), DEFAULT_BORDER_COLOR));
    mBorderPaint.setStrokeWidth(mBorderWidth);
  }
 
Example #21
Source File: FadingShadow.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * @param shadowColor The color of the shadow, e.g. 0x11000000.
 */
public FadingShadow(int shadowColor) {
    final int n = SMOOTH_ALGORITHM_INTERPOLATION_POINTS_NUM;
    float[] positions = new float[n];
    int[] colors = new int[n];
    int transparentShadowColor = shadowColor & 0x00FFFFFF;
    int shadowAlpha = Color.alpha(shadowColor);

    // Piece-wise linear interpolation of the smooth cubic function below.
    for (int i = 0; i < n; ++i) {
        float x = (float) i / (n - 1);
        // Polynomial computation by Estrin's scheme.
        float value = (1.0f - 2.2f * x) + (1.8f - 0.6f * x) * (x * x);

        positions[i] = x;
        colors[i] = (Math.round(shadowAlpha * value) << 24) | transparentShadowColor;
    }

    mShadowShader = new LinearGradient(0, 0, 0, 1, colors, positions, Shader.TileMode.CLAMP);

}
 
Example #22
Source File: SVGHandlerTest.java    From CustomShapeImageView with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseRadialGradient() throws Exception {
    //given
    when(picture.beginRecording(anyInt(), anyInt())).thenReturn(canvas);
    SVGParser.SVGHandler parserHandler = spy(this.parserHandler);
    RadialGradient gradient = mock(RadialGradient.class);
    PowerMockito.whenNew(RadialGradient.class).withArguments(
            eq(10.1f), eq(4.1f), eq(5.0f), eq(new int[]{-65536}), eq(new float[]{10.1f}), eq(Shader.TileMode.CLAMP)
    ).thenReturn(gradient);

    //when
    startSVG(parserHandler);
    startElement(parserHandler, attributes(attr("id", "g1"), attr("cx", "10.1"), attr("cy", "4.1"), attr("r", "5.0")), "radialGradient");
    startElement(parserHandler, attributes(attr("offset", "10.1"), attr("style", "stop-color:ff0000")), "stop");
    endElement(parserHandler, "stop");
    endElement(parserHandler, "radialGradient");
    startElement(parserHandler, attributes(attr("width", "100"), attr("height", "100"), attr("fill", "url(#g1)")), "rect");
    endElement(parserHandler, "rect");
    endSVG(parserHandler);

    //then
    verify(paint).setShader(gradient);
    ;
}
 
Example #23
Source File: HorizontalGridView.java    From TvRecyclerView with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the left edge fading length in pixels.
 */
public final void setFadingLeftEdgeLength(int fadeLength) {
    if (mLowFadeShaderLength != fadeLength) {
        mLowFadeShaderLength = fadeLength;
        if (mLowFadeShaderLength != 0) {
            mLowFadeShader = new LinearGradient(0, 0, mLowFadeShaderLength, 0,
                    Color.TRANSPARENT, Color.BLACK, Shader.TileMode.CLAMP);
        } else {
            mLowFadeShader = null;
        }
        invalidate();
    }
}
 
Example #24
Source File: SaturationBar.java    From memoir with Apache License 2.0 5 votes vote down vote up
/**
 * Set the bar color. <br>
 * <br>
 * Its discouraged to use this method.
 *
 * @param color
 */
public void setColor(int color) {
    int x1, y1;
    if (mOrientation == ORIENTATION_HORIZONTAL) {
        x1 = (mBarLength + mBarPointerHaloRadius);
        y1 = mBarThickness;
    } else {
        x1 = mBarThickness;
        y1 = (mBarLength + mBarPointerHaloRadius);
    }

    Color.colorToHSV(color, mHSVColor);
    shader = new LinearGradient(mBarPointerHaloRadius, 0,
            x1, y1, new int[]{
            Color.WHITE, color}, null,
            Shader.TileMode.CLAMP);
    mBarPaint.setShader(shader);
    calculateColor(mBarPointerPosition);
    mBarPointerPaint.setColor(mColor);
    if (mPicker != null) {
        mPicker.setNewCenterColor(mColor);
        if (mPicker.hasValueBar())
            mPicker.changeValueBarColor(mColor);
        else if (mPicker.hasOpacityBar())
            mPicker.changeOpacityBarColor(mColor);
    }
    invalidate();
}
 
Example #25
Source File: BIGChart.java    From NightWatch with GNU General Public License v3.0 5 votes vote down vote up
protected void updateRainbow() {
    animationAngle = (animationAngle + 1) % 360;
    //Animation matrix:
    int[] rainbow = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE
            , Color.CYAN};
    Shader shader = new LinearGradient(0, 0, 0, 20, rainbow,
            null, Shader.TileMode.MIRROR);
    Matrix matrix = new Matrix();
    matrix.setRotate(animationAngle);
    shader.setLocalMatrix(matrix);
    mSgv.getPaint().setShader(shader);
    invalidate();
}
 
Example #26
Source File: ColorPicker.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private Bitmap createColorWheelBitmap(int width, int height) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    LinearGradient gradientShader = new LinearGradient(0, 0, width, 0, new int[]{Color.RED, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA, Color.RED}, null, Shader.TileMode.CLAMP);
    LinearGradient alphaShader = new LinearGradient(0, (height / 3), 0, height, new int[]{Color.WHITE, Color.TRANSPARENT}, null, Shader.TileMode.CLAMP);
    ComposeShader composeShader = new ComposeShader(alphaShader, gradientShader, PorterDuff.Mode.MULTIPLY);

    colorWheelPaint.setShader(composeShader);

    Canvas canvas = new Canvas(bitmap);
    canvas.drawRect(0, 0, width, height, colorWheelPaint);

    return bitmap;
}
 
Example #27
Source File: ScratchTextView.java    From ScratchView with Apache License 2.0 5 votes vote down vote up
/**
 * Initialises the paint drawing elements.
 */
private void init() {


    mTouchPath = new Path();

    mErasePaint = new Paint();
    mErasePaint.setAntiAlias(true);
    mErasePaint.setDither(true);
    mErasePaint.setColor(0xFFFF0000);
    mErasePaint.setStyle(Paint.Style.STROKE);
    mErasePaint.setStrokeJoin(Paint.Join.BEVEL);
    mErasePaint.setStrokeCap(Paint.Cap.ROUND);
    mErasePaint.setXfermode(new PorterDuffXfermode(
            PorterDuff.Mode.CLEAR));
    setStrokeWidth(6);

    mGradientBgPaint = new Paint();

    mErasePath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);


    Bitmap scratchBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_scratch_pattern);
    mDrawable = new BitmapDrawable(getResources(), scratchBitmap);
    mDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);


}
 
Example #28
Source File: ColorAlphaPicker.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
private void createCheckerboard() {
    mBitmap = Bitmap.createBitmap((int) (mTileSize * 2), (int) (mTileSize * 2),
            Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(mBitmap);
    Paint p = new Paint();
    p.setColor(0xFFEEEEEE);
    c.drawRect(0.f, 0.f, mTileSize, mTileSize, p);
    c.drawRect(mTileSize, mTileSize, mTileSize * 2, mTileSize * 2, p);
    p.setColor(0xFF757575);
    c.drawRect(mTileSize, 0, mTileSize * 2, mTileSize, p);
    c.drawRect(0, mTileSize, mTileSize, mTileSize * 2, p);
    mBmpPaint.setShader(new BitmapShader(mBitmap, Shader.TileMode.REPEAT,
            Shader.TileMode.REPEAT));
}
 
Example #29
Source File: FloatingActionButton.java    From FlowGeek with GNU General Public License v2.0 5 votes vote down vote up
private Drawable createInnerStrokesDrawable(final int color, float strokeWidth) {
  if (!mStrokeVisible) {
    return new ColorDrawable(Color.TRANSPARENT);
  }

  ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());

  final int bottomStrokeColor = darkenColor(color);
  final int bottomStrokeColorHalfTransparent = halfTransparent(bottomStrokeColor);
  final int topStrokeColor = lightenColor(color);
  final int topStrokeColorHalfTransparent = halfTransparent(topStrokeColor);

  final Paint paint = shapeDrawable.getPaint();
  paint.setAntiAlias(true);
  paint.setStrokeWidth(strokeWidth);
  paint.setStyle(Style.STROKE);
  shapeDrawable.setShaderFactory(new ShaderFactory() {
    @Override
    public Shader resize(int width, int height) {
      return new LinearGradient(width / 2, 0, width / 2, height,
          new int[] { topStrokeColor, topStrokeColorHalfTransparent, color, bottomStrokeColorHalfTransparent, bottomStrokeColor },
          new float[] { 0f, 0.2f, 0.5f, 0.8f, 1f },
          TileMode.CLAMP
      );
    }
  });

  return shapeDrawable;
}
 
Example #30
Source File: OwnCustomView.java    From Building-Android-UIs-with-Custom-Views with MIT License 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    if (needsUpdate) {
        int[] colors = new int[] {topLeftColor, topRightColor, bottomRightColor, bottomLeftColor};

        LinearGradient lg = new LinearGradient(0, 0, getWidth(), getHeight(), colors, null, Shader.TileMode.CLAMP);
        backgroundPaint.setShader(lg);
        needsUpdate = false;
    }

    canvas.drawRect(0, 0, getWidth(), getHeight(), backgroundPaint);
    super.onDraw(canvas);
}