Java Code Examples for android.graphics.Paint#setShader()

The following examples show how to use android.graphics.Paint#setShader() . 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: 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 2
Source File: CircleTransform.java    From faveo-helpdesk-android-app with Open Software License 3.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());

    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;

    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();
    }

    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);

    float r = size/2f;
    canvas.drawCircle(r, r, r, paint);

    squaredBitmap.recycle();
    return bitmap;
}
 
Example 3
Source File: GlideTransformUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
    if (source == null) return null;

    Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_4444);
    if (result == null) {
        result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_4444);
    }

    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
    paint.setAntiAlias(true);
    RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
    canvas.drawRoundRect(rectF, radius, radius, paint);
    return result;
}
 
Example 4
Source File: ImageUtils.java    From ViewPager with Apache License 2.0 6 votes vote down vote up
public static Bitmap getReverseBitmapById(Context context, int resId, float percent) {
    // get the source bitmap
    Bitmap srcBitmap=BitmapFactory.decodeResource(context.getResources(), resId);
    // get the tow third segment of the reverse bitmap
    Matrix matrix=new Matrix();
    matrix.setScale(1, -1);
    Bitmap rvsBitmap=Bitmap.createBitmap(srcBitmap, 0, (int) (srcBitmap.getHeight()*(1-percent)),
            srcBitmap.getWidth(), (int) (srcBitmap.getHeight()*percent), matrix, false);
    // combine the source bitmap and the reverse bitmap
    Bitmap comBitmap=Bitmap.createBitmap(srcBitmap.getWidth(),
            srcBitmap.getHeight()+rvsBitmap.getHeight()+20, srcBitmap.getConfig());
    Canvas gCanvas=new Canvas(comBitmap);
    gCanvas.drawBitmap(srcBitmap, 0, 0, null);
    gCanvas.drawBitmap(rvsBitmap, 0, srcBitmap.getHeight()+20, null);
    Paint paint=new Paint();
    LinearGradient shader=new LinearGradient(0, srcBitmap.getHeight()+20, 0, comBitmap.getHeight(),
            Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    gCanvas.drawRect(0, srcBitmap.getHeight()+20, srcBitmap.getWidth(), comBitmap.getHeight(), paint);
    return comBitmap;
}
 
Example 5
Source File: GlideRoundTransform.java    From AssistantBySDK with Apache License 2.0 6 votes vote down vote up
private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
    if (source == null) return null;

    Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    if (result == null) {
        result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
    paint.setAntiAlias(true);
    RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
    canvas.drawRoundRect(rectF, radius, radius, paint);
    return result;
}
 
Example 6
Source File: CenterCropRoundCornerTransform.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
    if (source == null)
        return null;
    Bitmap result = pool.get(source.getWidth(), source.getHeight(),
            Bitmap.Config.ARGB_8888);
    if (result == null) {
        result = Bitmap.createBitmap(source.getWidth(), source.getHeight(),
                Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP,
            BitmapShader.TileMode.CLAMP));
    paint.setAntiAlias(true);
    RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
    canvas.drawRoundRect(rectF, radius, radius, paint);
    return result;
}
 
Example 7
Source File: SplashScreen.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 8
Source File: RoundImage.java    From Android-Bluetooth-Fingerprint with Apache License 2.0 5 votes vote down vote up
public RoundImage(Bitmap bitmap) {
    mBitmap = bitmap;
    mRectF = new RectF();
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mPaint.setShader(shader);

    mBitmapWidth = mBitmap.getWidth();
    mBitmapHeight = mBitmap.getHeight();
}
 
Example 9
Source File: TransformToCircle.java    From GitJourney with Apache License 2.0 5 votes vote down vote up
@Override
public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());

    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;

    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();
    }

    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap,
            BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);

    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);

    squaredBitmap.recycle();
    return bitmap;
}
 
Example 10
Source File: SaturationBar.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
private void init(AttributeSet attrs, int defStyle) {
	final TypedArray a = getContext().obtainStyledAttributes(attrs,
			R.styleable.ColorBars, defStyle, 0);
	final Resources b = getContext().getResources();

	mBarThickness = a.getDimensionPixelSize(
			R.styleable.ColorBars_bar_thickness,
			b.getDimensionPixelSize(R.dimen.bar_thickness));
	mBarLength = a.getDimensionPixelSize(R.styleable.ColorBars_bar_length,
			b.getDimensionPixelSize(R.dimen.bar_length));
	mPreferredBarLength = mBarLength;
	mBarPointerRadius = a.getDimensionPixelSize(
			R.styleable.ColorBars_bar_pointer_radius,
			b.getDimensionPixelSize(R.dimen.bar_pointer_radius));
	mBarPointerHaloRadius = a.getDimensionPixelSize(
			R.styleable.ColorBars_bar_pointer_halo_radius,
			b.getDimensionPixelSize(R.dimen.bar_pointer_halo_radius));
	mOrientation = a.getBoolean(
			R.styleable.ColorBars_bar_orientation_horizontal,
			ORIENTATION_DEFAULT);

	a.recycle();

	mBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	mBarPaint.setShader(shader);

	mBarPointerPosition = mBarLength + mBarPointerHaloRadius;

	mBarPointerHaloPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	mBarPointerHaloPaint.setColor(Color.BLACK);
	mBarPointerHaloPaint.setAlpha(0x50);

	mBarPointerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	mBarPointerPaint.setColor(0xff81ff00);

	mPosToSatFactor = 1 / ((float) mBarLength);
	mSatToPosFactor = ((float) mBarLength) / 1;
}
 
Example 11
Source File: CircleBar.java    From CircleProgressBar with MIT License 5 votes vote down vote up
private void init(AttributeSet attrs, int defStyle) {

        /**
         * 设置画笔渐变色
         */
        mColors = new int[]{0xFF6BB7ED, 0xFF47D9CE, 0xFF56CADC};
        Shader s = new SweepGradient(0, 0, mColors, null);
        mColorWheelPaint = new Paint();
        mColorWheelPaint.setShader(s);
        mColorWheelPaint.setStyle(Paint.Style.STROKE);// 空心,只绘制轮廓线
        mColorWheelPaint.setStrokeCap(Paint.Cap.ROUND);// 圆角画笔
        mColorWheelPaint.setAntiAlias(true);// 去锯齿

        mColorWheelPaintCentre = new Paint();
        mColorWheelPaintCentre.setColor(Color.rgb(214, 246, 254));
        mColorWheelPaintCentre.setStyle(Paint.Style.STROKE);
        mColorWheelPaintCentre.setStrokeCap(Paint.Cap.ROUND);
        mColorWheelPaintCentre.setAntiAlias(true);

        mDefaultWheelPaint = new Paint();
        mDefaultWheelPaint.setColor(Color.rgb(127, 127, 127));
        mDefaultWheelPaint.setStyle(Paint.Style.STROKE);
        mDefaultWheelPaint.setStrokeCap(Paint.Cap.ROUND);
        mDefaultWheelPaint.setAntiAlias(true);

        anim = new BarAnimation();
    }
 
Example 12
Source File: LinearGradientView.java    From support with Apache License 2.0 5 votes vote down vote up
public LinearGradientView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mGradient = new LinearGradient(0, 0, 500, 0, new int[]{
            Color.RED, Color.YELLOW, Color.GREEN
    }, null, Shader.TileMode.CLAMP);
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setShader(mGradient);
}
 
Example 13
Source File: RxImageTool.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
/**
 * 添加倒影
 *
 * @param src              源图片的
 * @param reflectionHeight 倒影高度
 * @param recycle          是否回收
 * @return 带倒影图片
 */
public static Bitmap addReflection(Bitmap src, int reflectionHeight, boolean recycle) {
    if (isEmptyBitmap(src)) return null;
    final int REFLECTION_GAP = 0;
    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();
    if (0 == srcWidth || srcHeight == 0) return null;
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    Bitmap reflectionBitmap = Bitmap.createBitmap(src, 0, srcHeight - reflectionHeight,
            srcWidth, reflectionHeight, matrix, false);
    if (null == reflectionBitmap) return null;
    Bitmap ret = Bitmap.createBitmap(srcWidth, srcHeight + reflectionHeight, src.getConfig());
    Canvas canvas = new Canvas(ret);
    canvas.drawBitmap(src, 0, 0, null);
    canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP, null);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    LinearGradient shader = new LinearGradient(0, srcHeight, 0,
            ret.getHeight() + REFLECTION_GAP,
            0x70FFFFFF, 0x00FFFFFF, Shader.TileMode.MIRROR);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(
            PorterDuff.Mode.DST_IN));
    canvas.save();
    canvas.drawRect(0, srcHeight, srcWidth,
            ret.getHeight() + REFLECTION_GAP, paint);
    canvas.restore();
    if (!reflectionBitmap.isRecycled()) reflectionBitmap.recycle();
    if (recycle && !src.isRecycled()) src.recycle();
    return ret;
}
 
Example 14
Source File: ValueBar.java    From HoloColorPicker with Apache License 2.0 5 votes vote down vote up
private void init(AttributeSet attrs, int defStyle) {
	final TypedArray a = getContext().obtainStyledAttributes(attrs,
			R.styleable.ColorBars, defStyle, 0);
	final Resources b = getContext().getResources();

	mBarThickness = a.getDimensionPixelSize(
			R.styleable.ColorBars_bar_thickness,
			b.getDimensionPixelSize(R.dimen.bar_thickness));
	mBarLength = a.getDimensionPixelSize(R.styleable.ColorBars_bar_length,
			b.getDimensionPixelSize(R.dimen.bar_length));
	mPreferredBarLength = mBarLength;
	mBarPointerRadius = a.getDimensionPixelSize(
			R.styleable.ColorBars_bar_pointer_radius,
			b.getDimensionPixelSize(R.dimen.bar_pointer_radius));
	mBarPointerHaloRadius = a.getDimensionPixelSize(
			R.styleable.ColorBars_bar_pointer_halo_radius,
			b.getDimensionPixelSize(R.dimen.bar_pointer_halo_radius));
	mOrientation = a.getBoolean(
			R.styleable.ColorBars_bar_orientation_horizontal, ORIENTATION_DEFAULT);

	a.recycle();

	mBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	mBarPaint.setShader(shader);

	mBarPointerPosition = mBarPointerHaloRadius;

	mBarPointerHaloPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	mBarPointerHaloPaint.setColor(Color.BLACK);
	mBarPointerHaloPaint.setAlpha(0x50);

	mBarPointerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	mBarPointerPaint.setColor(0xff81ff00);

	mPosToSatFactor = 1 / ((float) mBarLength);
	mSatToPosFactor = ((float) mBarLength) / 1;
}
 
Example 15
Source File: CropCircleTransformation.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
@Override
public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());

    int width = (source.getWidth() - size) / 2;
    int height = (source.getHeight() - size) / 2;

    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_4444);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader =
            new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    if (width != 0 || height != 0) {
        // source isn't square, move viewport to center
        Matrix matrix = new Matrix();
        matrix.setTranslate(-width, -height);
        shader.setLocalMatrix(matrix);
    }
    paint.setShader(shader);
    paint.setAntiAlias(true);

    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);

    source.recycle();

    return bitmap;
}
 
Example 16
Source File: BitmapUtils.java    From Cangol-appcore with Apache License 2.0 5 votes vote down vote up
/**
 * 创建倒影
 *
 * @param originalImage
 * @return
 */
public static Bitmap createReflectedImage(Bitmap originalImage) {
    final int reflectionGap = 4;  //倒影和原图片间的距离
    final int width = originalImage.getWidth();
    final int height = originalImage.getHeight();

    final Matrix matrix = new Matrix();
    matrix.preScale(1, -1);

    //倒影部分
    final Bitmap reflectionImage = Bitmap.createBitmap(originalImage,
            0, height / 2, width, height / 2, matrix, false);
    //要返回的倒影图片
    final Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
            (height + height / 2), Config.ARGB_8888);

    final Canvas canvas = new Canvas(bitmapWithReflection);
    //画原来的图片
    canvas.drawBitmap(originalImage, 0, 0, null);

    final Paint defaultPaint = new Paint();
    //倒影和原图片间的距离
    canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
    //画倒影部分
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    final Paint paint = new Paint();
    final LinearGradient shader = new LinearGradient(0, originalImage.getHeight(),
            0, bitmapWithReflection.getHeight() + reflectionGap,
            0x70ffffff, 0x00ffffff,
            TileMode.MIRROR);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
    return bitmapWithReflection;
}
 
Example 17
Source File: BitmapUtils.java    From FastAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * 转为圆形图片
 *
 * @param src         源图片
 * @param recycle     是否回收
 * @param borderSize  边框尺寸
 * @param borderColor 边框颜色
 * @return 圆形图片
 */
public static Bitmap toRound(@NonNull Bitmap src,
                             @IntRange(from = 0) int borderSize,
                             @ColorInt int borderColor,
                             boolean recycle) {
    int width = src.getWidth();
    int height = src.getHeight();
    int size = Math.min(width, height);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Bitmap ret = Bitmap.createBitmap(width, height, src.getConfig());
    float center = size / 2f;
    RectF rectF = new RectF(0, 0, width, height);
    rectF.inset((width - size) / 2f, (height - size) / 2f);
    Matrix matrix = new Matrix();
    matrix.setTranslate(rectF.left, rectF.top);
    matrix.preScale((float) size / width, (float) size / height);
    BitmapShader shader = new BitmapShader(src, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    shader.setLocalMatrix(matrix);
    paint.setShader(shader);
    Canvas canvas = new Canvas(ret);
    canvas.drawRoundRect(rectF, center, center, paint);
    if (borderSize > 0) {
        paint.setShader(null);
        paint.setColor(borderColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(borderSize);
        float radius = center - borderSize / 2f;
        canvas.drawCircle(width / 2f, height / 2f, radius, paint);
    }
    if (recycle && !src.isRecycled()) src.recycle();
    return ret;
}
 
Example 18
Source File: CircleTransform.java    From ListItemView with Apache License 2.0 5 votes vote down vote up
@Override
public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());

    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;

    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();
    }

    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP,
            BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);

    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);

    squaredBitmap.recycle();
    return bitmap;
}
 
Example 19
Source File: BitmapUtils.java    From Android-PdfMyXml with GNU General Public License v2.0 4 votes vote down vote up
/**
 * create a circle from cutout from a bitmap.
 * does not alter sizes.
 *
 * @param bitmap the bitmap
 * @see #cutCircleFromBitmap(String, int)
 * @return a bitmap circle cutout
 */
public static Bitmap roundBitMap(Bitmap bitmap)
{
    Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

    BitmapShader shader = new BitmapShader (bitmap,  TileMode.CLAMP, TileMode.CLAMP);
    Paint paint 				= new Paint(Paint.ANTI_ALIAS_FLAG);

    paint.setShader(shader);

    Canvas c 						= new Canvas(circleBitmap);

    c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2, paint);

    return circleBitmap;
}
 
Example 20
Source File: TextureCache.java    From PD-classes with GNU General Public License v3.0 4 votes vote down vote up
public static SmartTexture createGradient( int width, int height, int... colors ) {
	
	final String key = "" + width + "x" + height + ":" + colors;
	
	if (all.containsKey( key )) {
		
		return all.get( key );
		
	} else {
	
		Bitmap bmp = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 );
		Canvas canvas = new Canvas( bmp );
		Paint paint = new Paint();
		paint.setShader( new LinearGradient( 0, 0, 0, height, colors, null, TileMode.CLAMP ) );
		canvas.drawPaint( paint );
		
		SmartTexture tx = new SmartTexture( bmp );
		all.put( key, tx );
		return tx;
	}
	
}