Java Code Examples for android.graphics.BitmapShader#setLocalMatrix()
The following examples show how to use
android.graphics.BitmapShader#setLocalMatrix() .
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: HoldingButton.java From HoldingButton with Apache License 2.0 | 6 votes |
public void setIcon(@Nullable Bitmap bitmap) { if (bitmap != null) { mIconWidth = bitmap.getWidth(); mIconHeight = bitmap.getHeight(); mIconShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mIconShader.setLocalMatrix(mIconMatrix); mIconPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mIconPaint.setShader(mIconShader); mIconPaint.setColorFilter(new PorterDuffColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN)); invalidate(); } else { mIconWidth = 0; mIconHeight = 0; mIconShader = null; mIconMatrix = null; mIconPaint = null; } }
Example 2
Source File: CircularImageView.java From social-app-android with Apache License 2.0 | 6 votes |
private void updateShader() { if (image == null) return; // Crop Center Image image = cropBitmap(image); // Create Shader BitmapShader shader = new BitmapShader(image, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); // Center Image in Shader Matrix matrix = new Matrix(); matrix.setScale((float) canvasSize / (float) image.getWidth(), (float) canvasSize / (float) image.getHeight()); shader.setLocalMatrix(matrix); // Set Shader in Paint paint.setShader(shader); }
Example 3
Source File: CircularImageView.java From social-app-android with Apache License 2.0 | 6 votes |
private void updateShader() { if (image == null) return; // Crop Center Image image = cropBitmap(image); // Create Shader BitmapShader shader = new BitmapShader(image, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); // Center Image in Shader Matrix matrix = new Matrix(); matrix.setScale((float) canvasSize / (float) image.getWidth(), (float) canvasSize / (float) image.getHeight()); shader.setLocalMatrix(matrix); // Set Shader in Paint paint.setShader(shader); }
Example 4
Source File: ImageDrawable.java From ucar-weex-core with Apache License 2.0 | 6 votes |
private static void updateShaderAndSize(@NonNull ImageView.ScaleType scaleType, int vWidth, int vHeight, ImageDrawable imageDrawable, BitmapShader bitmapShader) { Matrix matrix = createShaderMatrix(scaleType, vWidth, vHeight, imageDrawable.bitmapWidth, imageDrawable.bitmapHeight); int intrinsicWidth = vWidth, intrinsicHeight = vHeight; if (scaleType == ImageView.ScaleType.FIT_CENTER) { RectF bitmapRect = new RectF(0, 0, imageDrawable.bitmapWidth, imageDrawable.bitmapHeight), contentRect = new RectF(); matrix.mapRect(contentRect, bitmapRect); intrinsicWidth = (int) contentRect.width(); intrinsicHeight = (int) contentRect.height(); matrix = createShaderMatrix(scaleType, intrinsicWidth, intrinsicHeight, imageDrawable .bitmapWidth, imageDrawable.bitmapHeight); } imageDrawable.setIntrinsicWidth(intrinsicWidth); imageDrawable.setIntrinsicHeight(intrinsicHeight); bitmapShader.setLocalMatrix(matrix); }
Example 5
Source File: CircleImageView.java From DMAudioStreamer with Apache License 2.0 | 6 votes |
private void updateShader() { if (image == null) return; // Crop Center Image image = cropBitmap(image); // Create Shader BitmapShader shader = new BitmapShader(image, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); // Center Image in Shader Matrix matrix = new Matrix(); matrix.setScale((float) canvasSize / (float) image.getWidth(), (float) canvasSize / (float) image.getHeight()); shader.setLocalMatrix(matrix); // Set Shader in Paint paint.setShader(shader); }
Example 6
Source File: HoldingButton.java From HoldingButton with Apache License 2.0 | 6 votes |
public void setCancelIcon(@Nullable Bitmap bitmap) { if (bitmap != null) { mCancelIconWidth = bitmap.getWidth(); mCancelIconHeight = bitmap.getHeight(); mCancelIconShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mCancelIconShader.setLocalMatrix(mCancelIconMatrix); mCancelIconPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mCancelIconPaint.setShader(mCancelIconShader); mCancelIconPaint.setColorFilter(new PorterDuffColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN)); invalidate(); } else { mCancelIconWidth = 0; mCancelIconHeight = 0; mCancelIconMatrix = null; mCancelIconPaint = null; } }
Example 7
Source File: BorderRoundTransformation2.java From ImageLoader with Apache License 2.0 | 5 votes |
/** * 通过BitmapShader 圆角边框 * @param bitmap * @param outWidth * @param outHeight * @param radius * @param boarder * @return */ public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) { if (bitmap == null) { return null; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); float widthScale = outWidth * 1f / width; float heightScale = outHeight * 1f / height; Matrix matrix = new Matrix(); matrix.setScale(widthScale, heightScale); //创建输出的bitmap Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888); //创建canvas并传入desBitmap,这样绘制的内容都会在desBitmap上 Canvas canvas = new Canvas(desBitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); //创建着色器 BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); //给着色器配置matrix bitmapShader.setLocalMatrix(matrix); paint.setShader(bitmapShader); //创建矩形区域并且预留出border RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder); //把传入的bitmap绘制到圆角矩形区域内 canvas.drawRoundRect(rect, radius, radius, paint); if (boarder > 0) { //绘制boarder Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); boarderPaint.setColor(Color.GREEN); boarderPaint.setStyle(Paint.Style.STROKE); boarderPaint.setStrokeWidth(boarder); canvas.drawRoundRect(rect, radius, radius, boarderPaint); } return desBitmap; }
Example 8
Source File: RoundedDrawable.java From Android with MIT License | 5 votes |
@Override public void draw(@NonNull Canvas canvas) { if (mRebuildShader) { BitmapShader bitmapShader = new BitmapShader(mBitmap, mTileModeX, mTileModeY); if (mTileModeX == Shader.TileMode.CLAMP && mTileModeY == Shader.TileMode.CLAMP) { bitmapShader.setLocalMatrix(mShaderMatrix); } mBitmapPaint.setShader(bitmapShader); mRebuildShader = false; } if (mOval) { if (mBorderWidth > 0) { canvas.drawOval(mDrawableRect, mBitmapPaint); canvas.drawOval(mBorderRect, mBorderPaint); } else { canvas.drawOval(mDrawableRect, mBitmapPaint); } } else { if (any(mCornersRounded)) { float radius = mCornerRadius; if (mBorderWidth > 0) { canvas.drawRoundRect(mDrawableRect, radius, radius, mBitmapPaint); canvas.drawRoundRect(mBorderRect, radius, radius, mBorderPaint); redrawBitmapForSquareCorners(canvas); redrawBorderForSquareCorners(canvas); } else { canvas.drawRoundRect(mDrawableRect, radius, radius, mBitmapPaint); redrawBitmapForSquareCorners(canvas); } } else { canvas.drawRect(mDrawableRect, mBitmapPaint); if (mBorderWidth > 0) { canvas.drawRect(mBorderRect, mBorderPaint); } } } }
Example 9
Source File: NoiseEffect.java From Depth with MIT License | 5 votes |
public NoiseEffect(Bitmap bitmap, int grainFPS, float scale) { super(bitmap, 0, 0); shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); matrix = new Matrix(); shader.setLocalMatrix(matrix); paint.setShader(shader); paint.setAlpha(144); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN)); lastGrainOffset = System.currentTimeMillis(); this.grainFPS = grainFPS; this.scale=scale; }
Example 10
Source File: BitmapUtils.java From FastAndroid with Apache License 2.0 | 5 votes |
/** * 转为圆形图片 * * @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 11
Source File: MyUtil.java From ImageLoader with Apache License 2.0 | 5 votes |
public static Bitmap cropCirle(Bitmap source, boolean recycleOriginal) { //BitmapPool mBitmapPool = Glide.get(BigLoader.context).getBitmapPool(); int size = Math.min(source.getWidth(), source.getHeight()); int width = (source.getWidth() - size) / 2; int height = (source.getHeight() - size) / 2; //source.setHasAlpha(true); Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); //bitmap.setHasAlpha(true); Canvas canvas = new Canvas(bitmap); //canvas.drawColor(Color.TRANSPARENT); //canvas.setBitmap(bitmap); Paint paint = new Paint(); //paint.setColor(Color.TRANSPARENT); //paint.setColorFilter() 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); if (recycleOriginal) { source.recycle(); } return bitmap; }
Example 12
Source File: CropCircleTransformation.java From arcusandroid with Apache License 2.0 | 5 votes |
@Override public Bitmap transform(@NonNull 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_8888); 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 centre 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); if (bitmap != source && !source.isRecycled()) { source.recycle(); } return bitmap; }
Example 13
Source File: PictureProgressBar.java From AlbumCameraRecorder with MIT License | 5 votes |
private void updateDrawableBounds(int h) { if (backgroundDrawable != null && barDrawable != null) { int bgWidth; int barWidth; // 根据Drawable资源的宽高计算缩放比例。 int intrinsicWidth = backgroundDrawable.getIntrinsicWidth(); int intrinsicHeight = backgroundDrawable.getIntrinsicHeight(); int barIntrinsicWidth = barDrawable.getIntrinsicWidth(); int barIntrinsicHeight = barDrawable.getIntrinsicHeight(); final float bgIntrinsicAspect = (float) intrinsicWidth / intrinsicHeight; final float barIntrinsicAspect = (float) barIntrinsicWidth/barIntrinsicHeight; bgWidth = (int) (h * bgIntrinsicAspect); barWidth = (int) (h * barIntrinsicAspect); float bgScaleX = (float) bgWidth / intrinsicWidth; float bgScaleY = (float) h / intrinsicHeight; float barScaleX = (float) barWidth / barIntrinsicWidth; float barScaleY = (float) h / barIntrinsicHeight; Matrix bgMatrix = new Matrix(); bgMatrix.postScale(bgScaleX, bgScaleY); BitmapShader bgBitmapShader = new BitmapShader(backgroundDrawable.getBitmap(), Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); bgBitmapShader.setLocalMatrix(bgMatrix); Matrix barMatrix = new Matrix(); barMatrix.postScale(barScaleX, barScaleY); BitmapShader barBitmapShader = new BitmapShader(barDrawable.getBitmap(), Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); barBitmapShader.setLocalMatrix(bgMatrix); // Log.d(TAG, "updateDrawableBoun;ds: width" + bgWidth); // Log.d(TAG, "updateDrawableBounds: height" + bgHeight); paintBackGround.setShader(bgBitmapShader); paintBar.setShader(barBitmapShader); // 设置Drawable的绘制区域。 // barDrawable.setBounds(0, 0, bgWidth, bgHeight); // backgroundDrawable.setBounds(0, 0, bgWidth, bgHeight); } }
Example 14
Source File: ShapeImageView.java From WanAndroid with Apache License 2.0 | 5 votes |
private void setPaintShader() { Drawable drawable = this.getDrawable(); if(drawable == null) throw new NullPointerException("the src image can't be null!"); mBitmap = drawableToBitmap(drawable); BitmapShader bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); Matrix matrix = new Matrix(); float scaleX = (float) getWidth() / mBitmap.getWidth(); float scaleY = (float) getHeight() / mBitmap.getHeight(); matrix.setScale(scaleX, scaleY); bitmapShader.setLocalMatrix(matrix); mPaint.setShader(bitmapShader); }
Example 15
Source File: RoundedDrawable.java From Common with Apache License 2.0 | 5 votes |
@Override public void draw(@NonNull Canvas canvas) { if (mRebuildShader) { BitmapShader bitmapShader = new BitmapShader(mBitmap, mTileModeX, mTileModeY); if (mTileModeX == Shader.TileMode.CLAMP && mTileModeY == Shader.TileMode.CLAMP) { bitmapShader.setLocalMatrix(mShaderMatrix); } mBitmapPaint.setShader(bitmapShader); mRebuildShader = false; } if (mOval) { if (mBorderWidth > 0) { canvas.drawOval(mDrawableRect, mBitmapPaint); canvas.drawOval(mBorderRect, mBorderPaint); } else { canvas.drawOval(mDrawableRect, mBitmapPaint); } } else { if (any(mCornersRounded)) { float radius = mCornerRadius; if (mBorderWidth > 0) { canvas.drawRoundRect(mDrawableRect, radius, radius, mBitmapPaint); canvas.drawRoundRect(mBorderRect, radius, radius, mBorderPaint); redrawBitmapForSquareCorners(canvas); redrawBorderForSquareCorners(canvas); } else { canvas.drawRoundRect(mDrawableRect, radius, radius, mBitmapPaint); redrawBitmapForSquareCorners(canvas); } } else { canvas.drawRect(mDrawableRect, mBitmapPaint); if (mBorderWidth > 0) { canvas.drawRect(mBorderRect, mBorderPaint); } } } }
Example 16
Source File: BitmapUtils.java From DevUtils with Apache License 2.0 | 5 votes |
/** * 图片圆形处理 * @param bitmap 待操作源图片 * @param borderSize 边框尺寸 * @param borderColor 边框颜色 * @return 圆形处理后的图片 */ public static Bitmap round(final Bitmap bitmap, @IntRange(from = 0) final int borderSize, @ColorInt final int borderColor) { if (isEmpty(bitmap)) return null; int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = Math.min(width, height); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 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); if (width != height) { matrix.preScale((float) size / width, (float) size / height); } BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); shader.setLocalMatrix(matrix); paint.setShader(shader); Bitmap newBitmap = Bitmap.createBitmap(width, height, bitmap.getConfig()); Canvas canvas = new Canvas(newBitmap); 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); } return newBitmap; }
Example 17
Source File: RoundedPagerDrawable.java From letv with Apache License 2.0 | 5 votes |
public void draw(Canvas canvas) { if (this.mRebuildShader) { BitmapShader bitmapShader = new BitmapShader(this.mBitmap, this.mTileModeX, this.mTileModeY); if (this.mTileModeX == TileMode.CLAMP && this.mTileModeY == TileMode.CLAMP) { bitmapShader.setLocalMatrix(this.mShaderMatrix); } this.mBitmapPaint.setShader(bitmapShader); this.mRebuildShader = false; } if (this.mOval) { if (this.mBorderWidth > 0.0f) { canvas.drawOval(this.mDrawableRect, this.mBitmapPaint); canvas.drawOval(this.mBorderRect, this.mBorderPaint); return; } canvas.drawOval(this.mDrawableRect, this.mBitmapPaint); } else if (any(this.mCornersRounded)) { float radius = this.mCornerRadius; if (this.mBorderWidth > 0.0f) { canvas.drawRoundRect(this.mDrawableRect, radius, radius, this.mBitmapPaint); canvas.drawRoundRect(this.mBorderRect, radius, radius, this.mBorderPaint); redrawBitmapForSquareCorners(canvas); redrawBorderForSquareCorners(canvas); return; } canvas.drawRoundRect(this.mDrawableRect, radius, radius, this.mBitmapPaint); redrawBitmapForSquareCorners(canvas); } else { canvas.drawRect(this.mDrawableRect, this.mBitmapPaint); if (this.mBorderWidth > 0.0f) { canvas.drawRect(this.mBorderRect, this.mBorderPaint); } } }
Example 18
Source File: CropCircleTransformation.java From giffun with Apache License 2.0 | 5 votes |
@Override public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) { Bitmap source = resource.get(); int size = Math.min(source.getWidth(), source.getHeight()); int width = (source.getWidth() - size) / 2; int height = (source.getHeight() - size) / 2; Bitmap bitmap = mBitmapPool.get(size, size, Bitmap.Config.ARGB_8888); if (bitmap == null) { bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); } 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); return BitmapResource.obtain(bitmap, mBitmapPool); }
Example 19
Source File: RoundImageView.java From star-zone-android with Apache License 2.0 | 5 votes |
/** * 获取ImageView中资源图片的Bitmap,利用Bitmap初始化图片着色器,通过缩放矩阵将原资源图片缩放到铺满整个绘制区域,避免边界填充 */ private BitmapShader initBitmapShader() { // Bitmap bitmap = ((BitmapDrawable) getDrawable()).getBitmap(); Bitmap bitmap = getBitmapFromVectorDrawable(getDrawable()); BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); float scale = Math.max(width / bitmap.getWidth(), height / bitmap.getHeight()); matrix.setScale(scale, scale);//将图片宽高等比例缩放,避免拉伸 bitmapShader.setLocalMatrix(matrix); return bitmapShader; }
Example 20
Source File: PrettyDialog.java From PrettyDialog with Apache License 2.0 | 4 votes |
private void updateShader() { if (image == null) return; // Create Shader BitmapShader shader = new BitmapShader(image, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); // Center Image in Shader float scale = 0; float dx = 0; float dy = 0; switch (getScaleType()) { case CENTER_CROP: if (image.getWidth() * getHeight() > getWidth() * image.getHeight()) { scale = getHeight() / (float) image.getHeight(); dx = (getWidth() - image.getWidth() * scale) * 0.5f; } else { scale = getWidth() / (float) image.getWidth(); dy = (getHeight() - image.getHeight() * scale) * 0.5f; } break; case CENTER_INSIDE: if (image.getWidth() * getHeight() < getWidth() * image.getHeight()) { scale = getHeight() / (float) image.getHeight(); dx = (getWidth() - image.getWidth() * scale) * 0.5f; } else { scale = getWidth() / (float) image.getWidth(); dy = (getHeight() - image.getHeight() * scale) * 0.5f; } break; } Matrix matrix = new Matrix(); matrix.setScale(scale, scale); matrix.postTranslate(dx, dy); shader.setLocalMatrix(matrix); // Set Shader in Paint paint.setShader(shader); // Apply colorFilter paint.setColorFilter(colorFilter); }