private static Bitmap circleCrop(BitmapPool pool, Bitmap source) { if (source == null) return null; int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; // TODO this could be acquired from the pool too Bitmap squared = Bitmap.createBitmap(source, x, y, size, size); Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, paint); return result; }
private BitmapShader updateShaderMatrix(BitmapShader shader) { float scale; float dx = 0; float dy = 0; calculateBounds(); Matrix shaderMatrix = new Matrix(); shaderMatrix.set(null); if (mBitmapWidth * mBoundsRect.height() > mBitmapHeight * mBoundsRect.width()) { scale = mBoundsRect.height() / (float) mBitmapHeight; dx = (mBoundsRect.width() - mBitmapWidth * scale) * 0.5f; } else { scale = mBoundsRect.width() / (float) mBitmapWidth; dy = (mBoundsRect.height() - mBitmapHeight * scale) * 0.5f; } shaderMatrix.setScale(scale, scale); shaderMatrix.postTranslate((int)(dx + 0.5f) + mBoundsRect.left, (int)(dy + 0.5f) + mBoundsRect.top); shader.setLocalMatrix(shaderMatrix); return shader; }
/** * 进度 */ 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); }
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; }
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) { if (source == null) return null; int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; Bitmap squared = Bitmap.createBitmap(source, x, y, size, size); Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, paint); return result; }
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) { if (source == null) return null; int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; Bitmap squared = Bitmap.createBitmap(source, x, y, size, size); Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, paint); squared.recycle(); return result; }
private void initPaint() { DisplayMetrics dm = getContext().getResources().getDisplayMetrics(); textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSize, dm); mPaint = new Paint(); mPaint.setColor(Color.RED); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(1); mPaint.setTextSize(35); mPaint.setColorFilter(new ColorFilter()); // mPaint.setStyle(Paint.Style.STROKE); mPath = new Path(); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg1); bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.bg2); bitmapShader1 = new BitmapShader(bitmap1, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); shader = new ComposeShader(bitmapShader, bitmapShader1, PorterDuff.Mode.SRC_OVER); }
@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { if (null == toTransform) return null; // outWidth is the width of the target ImageView,and the same to outHeight // all the ori bitmaps loaded may have different size, in order to the clipped // the bitmaps have the same size and shape,we use the target ImageView's size // to create bitmaps updateDrawBound(toTransform.getWidth(), toTransform.getHeight(), outWidth, outHeight); Bitmap bitmap = pool.get(drawWidth, drawHeight, Bitmap.Config.ARGB_8888); if (bitmap == null) { bitmap = Bitmap.createBitmap(drawWidth, drawHeight, Bitmap.Config.ARGB_8888); } bitmap.setHasAlpha(true); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(new BitmapShader(toTransform, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); drawRoundRect(canvas, paint); return bitmap; }
public SelectableRoundedCornerDrawable(Bitmap bitmap, Resources r) { mbBitmap = bitmap; mmBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); if (bitmap != null) { mmBitmapWidth = bitmap.getScaledWidth(r.getDisplayMetrics()); mmBitmapHeight = bitmap.getScaledHeight(r.getDisplayMetrics()); } else { mmBitmapWidth = mmBitmapHeight = -1; } mmBitmapRect.set(0, 0, mmBitmapWidth, mmBitmapHeight); mmBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mmBitmapPaint.setStyle(Paint.Style.FILL); mmBitmapPaint.setShader(mmBitmapShader); mmBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mmBorderPaint.setStyle(Paint.Style.STROKE); mmBorderPaint.setColor(mmBorderColor.getColorForState(getState(), DEFAULT_BORDER_COLOR)); mmBorderPaint.setStrokeWidth(mmBorderWidth); }
/** * Using the current view scale, create a bitmap tiling shader to render the workspace grid. */ void updateGridBitmap(float viewScale) { int gridSpacing = (int) (GRID_SPACING * viewScale); // For some reason, reusing the same Bitmap via Bitmap.reconfigure() leads to bad rendering, // so recycle existing Bitmap and create a new one instead. if (mGridBitmap != null) { mGridBitmap.recycle(); } mGridBitmap = Bitmap.createBitmap(gridSpacing, gridSpacing, Bitmap.Config.ARGB_8888); Canvas bitmapCanvas = new Canvas(mGridBitmap); bitmapCanvas.drawCircle(GRID_RADIUS, GRID_RADIUS, GRID_RADIUS, mCirclePaint); mGridPaint.setShader( new BitmapShader(mGridBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT)); }
@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; }
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) { if (source == null) return null; int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; // TODO this could be acquired from the pool too Bitmap squared = Bitmap.createBitmap(source, x, y, size, size); Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); } float r = size / 2f; Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setColor(Color.parseColor("#F9F9F9")); paint.setStyle(Paint.Style.FILL); canvas.drawCircle(r, r, r, paint); paint.reset(); paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); canvas.drawCircle(r, r, r, paint); return result; }
@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { if (toTransform == null) return null; int size = Math.min(toTransform.getWidth(), toTransform.getHeight()); int x = (toTransform.getWidth() - size) / 2; int y = (toTransform.getHeight() - size) / 2; Bitmap squared = Bitmap.createBitmap(toTransform, x, y, size, size); Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, paint); return result; }
public void setCancelIcon(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)); invalidateSelf(); } else { mCancelIconWidth = 0; mCancelIconHeight = 0; mCancelIconMatrix = null; mCancelIconPaint = null; } }
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); }
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); }
public static Bitmap getCircularBitmapFrom(Bitmap bitmap) { if (bitmap == null || bitmap.isRecycled()) { return null; } float radius = bitmap.getWidth() > bitmap.getHeight() ? ((float) bitmap .getHeight()) / 2f : ((float) bitmap.getWidth()) / 2f; Bitmap canvasBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(shader); Canvas canvas = new Canvas(canvasBitmap); canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, radius, paint); return canvasBitmap; }
public ImageTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ImageTextView, defStyleAttr, 0); mText = ""; mText = array.getString(R.styleable.ImageTextView_text); mTextSize = array.getDimension(R.styleable.ImageTextView_textSize, 10.0f); int id = array.getResourceId(R.styleable.ImageTextView_bitmap, R.drawable.default_album); mBitmap = BitmapFactory.decodeResource(context.getResources(), id); array.recycle(); paint.setAntiAlias(true); if (mBitmap != null) { shader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); } typeface = Typeface.SERIF; }
/** * 设置BitmapShader */ private void setBitmapShader() { Drawable drawable = getDrawable(); if (null == drawable) { return; } Bitmap bitmap = drawableToBitmap(drawable); // 将bitmap作为着色器来创建一个BitmapShader mBitmapShader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP); float scale = 1.0f; if (mType == TYPE_CIRCLE) { // 拿到bitmap宽或高的小值 int bSize = Math.min(bitmap.getWidth(), bitmap.getHeight()); scale = mWidth * 1.0f / bSize; } else if (mType == TYPE_ROUND || mType == TYPE_OVAL) { // 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值; scale = Math.max(getWidth() * 1.0f / bitmap.getWidth(), getHeight() * 1.0f / bitmap.getHeight()); } // shader的变换矩阵,我们这里主要用于放大或者缩小 mMatrix.setScale(scale, scale); // 设置变换矩阵 mBitmapShader.setLocalMatrix(mMatrix); mPaint.setShader(mBitmapShader); }
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(); }
@Override public Resource<Bitmap> transform(Context context, Resource<Bitmap> resource, int outWidth, int outHeight) { Bitmap source = resource.get(); int width = source.getWidth(); int height = source.getHeight(); Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); drawRoundRect(canvas, paint, width, height); return BitmapResource.obtain(bitmap, mBitmapPool); }
@Override public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) { Bitmap source = resource.get(); int width = source.getWidth(); int height = source.getHeight(); Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.RGB_565); if (bitmap == null) { bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); } Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); drawRoundRect(canvas, paint, width, height); return BitmapResource.obtain(bitmap, mBitmapPool); }
public static Bitmap getImageRound(byte[] imageByte) { Bitmap image; image = BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length); //TODO: Verificar se o formato da imagem está correto; Bitmap imageRounded = Bitmap.createBitmap(image.getWidth(), image.getHeight(), image.getConfig()); Canvas canvas = new Canvas(imageRounded); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(new BitmapShader(image, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); canvas.drawRoundRect((new RectF(0, 0, image.getWidth(), image.getHeight())), image.getWidth() / 2, image.getWidth() / 2, paint);// Round Image Corner 100 100 100 100 return imageRounded; }
@Override public Bitmap transform(Bitmap source) { int width = source.getWidth(); int height = source.getHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); drawRoundRect(canvas, paint, width, height); source.recycle(); return bitmap; }
private void setup() { if (!this.mReady) { this.mSetupPending = true; } else if (this.mBitmap != null) { this.mBitmapShader = new BitmapShader(this.mBitmap, TileMode.CLAMP, TileMode.CLAMP); this.mBitmapPaint.setAntiAlias(true); this.mBitmapPaint.setShader(this.mBitmapShader); this.mBorderPaint.setStyle(Style.STROKE); this.mBorderPaint.setAntiAlias(true); this.mBorderPaint.setColor(this.mBorderColor); this.mBorderPaint.setStrokeWidth((float) this.mBorderWidth); this.mBitmapHeight = this.mBitmap.getHeight(); this.mBitmapWidth = this.mBitmap.getWidth(); this.mBorderRect.set(0.0f, 0.0f, (float) getWidth(), (float) getHeight()); this.mBorderRadius = Math.min((this.mBorderRect.height() - ((float) this.mBorderWidth)) / 2.0f, (this.mBorderRect.width() - ((float) this.mBorderWidth)) / 2.0f); this.mDrawableRect.set((float) this.mBorderWidth, (float) this.mBorderWidth, this.mBorderRect.width() - ((float) this.mBorderWidth), this.mBorderRect.height() - ((float) this.mBorderWidth)); this.mDrawableRadius = Math.min(this.mDrawableRect.height() / 2.0f, this.mDrawableRect.width() / 2.0f); updateShaderMatrix(); invalidate(); } }
@Override protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { if (toTransform == null) return null; int size = Math.min(toTransform.getWidth(), toTransform.getHeight()); int x = (toTransform.getWidth() - size) / 2; int y = (toTransform.getHeight() - size) / 2; Bitmap squared = Bitmap.createBitmap(toTransform, x, y, size, size); Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); RectF rectF = new RectF(0f, 0f, result.getWidth(), result.getHeight()); canvas.drawRoundRect(rectF, radius, radius, paint); return result; }
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; }