I have tried to modify a Paint variable, but have been unsuccessful - how can I make a bitmap appear "semi-transparent"?
canvas.drawColor(Color.WHITE);
BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.loading);
Bitmap bm = bd.getBitmap();
Paint paint = new Paint();
paint.setAlpha(60); //you can set your transparent value here
canvas.drawBitmap(bm, 0, 0, paint);
Paint p = new Paint();
p.setAlpha(70);
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper);
canvas.drawBitmap(image, xPosition, yPosition, p);
Related
I have a bitmap and I am trying to draw a text over the bitmap at the top right corner. But at the first place I am not able to draw any text over it. I am converting a layout to bitmap and then trying to draw text over it. But its not working out. Here is my code:
private Bitmap viewToBitmap(LinearLayout layout) {
Bitmap bitmap = Bitmap.createBitmap(layout.getWidth(), layout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
layout.draw(canvas);
canvas=new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setTextSize(14.f);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("Hello Android!", 0, 0, paint);
return bitmap;
}
Please pay much attention to the text position & alignment.
private Bitmap viewToBitmap(View view)
{
Bitmap result = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
view.draw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setTextSize(14);
paint.setTextAlign(Paint.Align.RIGHT);
canvas.drawText("Hello Android!", view.getWidth(), 14, paint); // draw watermark at top right corner
return result;
}
I have a question regarding a circular ImageView. When I try to set the background color of my ImageView, it actually ends up being a square and not a circle when in reality I would want the ImageView to be a simple circular ImageView with some text as the image. Here was my attempt.
ImageView imageView = postViewHolder.fourthCommenter;
Bitmap b=Bitmap.createBitmap(30, 30, Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(b, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(b);
c.drawCircle(b.getWidth()/2, b.getHeight()/2, b.getWidth()/2, paint);
c.drawText("+10",30,30,paint);
imageView.setBackgroundColor(ContextCompat.getColor(context, R.color.material_color_grey_200));
imageView.setImageBitmap(b);
All I want is to have the image show a grey circular Imageview with the word "+10" in it. What shows up is a simple square Imageview with no text in it. I can't see what I am doing wrong.
Any help would be apprecaited.
Thanks!
EDIT: Here is an example of what I would want. I want to have an image that looks exactly like the +16 image shown on this photo:
All I want is to have the image show a grey circular Imageview
Try this code
Bitmap picture = BitmapFactory.decodeResource(getResources(), R.mipmap.add_image);
ImageView imageView = (ImageView) findViewById(R.id.imgProfilePicture);
imageView.setImageBitmap(getRoundedBitmap(picture));
public Bitmap getRoundedBitmap(Bitmap bitmap){
Bitmap circleBitmap = 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.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
return circleBitmap;
}
Your xml file
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imgProfilePicture"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_marginBottom="20dp"
app:civ_border_width="3dp"
app:civ_border_color="#color/light_gray" />
and add this in build.gradle
compile 'de.hdodenhof:circleimageview:2.1.0'
Cirular ImageView Done !
Not sure if it's the proper way to do it , but it should work for your case :
First use this to create the bitmap with the required circle shape :
public Bitmap getCircledBmp(Context mContext,Bitmap bmp){
Canvas canvas = new Canvas(bmp);
int color = ContextCompat.getColor(mContext, R.color.material_color_grey_200);
Paint paint = new Paint();
Rect rect = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
RectF rectFloat = new RectF(rect);
paint.setAntiAlias(true);
paint.setColor(color);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawOval(rectFloat, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bmp, rect, rect, paint);
return bmp;
}
Then add the text that you want :
public Bitmap drawText(String text, Bitmap bmp, int textSize) {
//text dimensions
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(textSize);
textPaint.setColor(Color.BLACK);
textPaint.setTextAlign(Paint.Align.CENTER);
StaticLayout mTextLayout = new StaticLayout(text, textPaint,
bmp.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
Canvas canvas = new Canvas(bmp);
//draw text
canvas.save();
canvas.translate((canvas.getWidth() / 2), (canvas.getHeight() / 2) -
((mTextLayout.getHeight() / 2)));
mTextLayout.draw(canvas);
canvas.restore();
return bmp;
}
In the end you should get your desired image like this :
Bitmap bmp = Bitmap.createBitmap(100,100,Bitmap.Config.ARGB_8888); //change the values whatever you like
ImageView imageView = postViewHolder.fourthCommenter;
imageview.setImageBitmap(drawText("+10",getCircledBmp(this,bmp),30));
Can we construct a bitmap from a rect.
I draw a bitmap in a rect and want strokes drawn on the bitmap image become part of the image.
I am wondering if I can construct a bitmap from a Rect so the new bitmap has the old image and the strokes as a single image.
Thank You
You can always take a canvas to help you create an already decoded bitmap the way you want:
Bitmap originalBmp = null;//Here goes original Bitmap...
ImageView img = null;//Any imageview holder you are using...
Bitmap modifiedBmp = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);//Configure with your proper size and color
Canvas canvas = new Canvas(modifiedBmp);
//At this point the modified bitmap has the original one, starting from here, you can add any overlay you want...
canvas.drawBitmap(originalBmp, 0, 0, new Paint());
//And do all the other modifications you want here...
canvas.drawLines(new float[]{}, null);
canvas.drawCircle(x, y, radius, null);
//At this point the modified bitmap will have anything you added
img.setImageBitmap(modifiedBmp);
// IF YOU ARE OVERRIDING ONDRAW METHOD
public void onDraw(Canvas canvas){
//Here DO your DRAW BITMAP NOTE: paint must be already created...
canvas.drawBitmap(bt, 0, 0, paint);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(30, 30, 80, 80, paint);
super.onDraw(canvas);
}
Regards!
Yes you can , Using canvas you can draw something on your old bimtap .
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// do some canvas drawing
canvas.drawBitmap(bitmap, rect, rect, paint);
I need to create a custom view amd in onDraw method I need to draw some bitmaps, using a mask. I created a paint:
Paint maskPaint = new Paint();
maskPaint.setAntiAlias(true);
maskPaint.setXfermode(new AvoidXfermode(Color.RED, 0, AvoidXfermode.Mode.TARGET));
and I draw my bitmap on canvas using this paint. My problem is that at the corners, my mask have some pixels with alpha less than 255. Is there a way to draw my bitmap's pixels on the mask with the same alpha that mask image has on those pixels with aplha greater than zero?
private Bitmap maskingImage(Bitmap s, int drawable) {
Bitmap original = s;
Bitmap mask = BitmapFactory.decodeResource(getResources(),drawable);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(),Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(original, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
return result;
}
Resources resources = this.getResources();
Bitmap mask = BitmapFactory.decodeResource(resources,R.drawable.fx_lightleak2_small);
int width=bMap.getWidth();
int height=bMap.getHeight();
Bitmap resizedbitmap=Bitmap.createScaledBitmap(mask, width, height, true);
Bitmap result = Bitmap.createBitmap(bMap.getWidth(), bMap.getHeight(), Config.ARGB_8888);
Canvas c = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
c.drawBitmap(bMap, 0, 0, null);
paint.setAlpha(200);
c.drawBitmap(resizedbitmap, 0, 0, paint);
paint.setXfermode(null);
effect_5.setImageBitmap(result);
I have a bitmap on which I'm trying to write a text using canvas.After setting the bitmap in a canvas and doing the necessary operations(writting a text on the canvas), I draw the resulting canvas on a ImageView.The big problem is that there is no image displayed...the screen turns black.Now, I know that the bitmap is returned ok because I displayed right before doing the canvas operations.
So,here is how I did it:
image= (ImageView) findViewById(R.id.imageview);
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
int heightOfOld=bitmap.getHeight();
int widthOfOld=bitmap.getWidth();
android.graphics.Bitmap.Config hasAlpha=bitmap.getConfig();
Bitmap bitmapResult=bitmap.createBitmap(widthOfOld, heightOfOld, hasAlpha);
Canvas c=new Canvas(bitmapResult);
Canvas c1=drawTextImage(c);
image.draw(c1);
And here is the method used for drawing text on the canvas:
private Canvas drawTextImage(Canvas c){
Paint paint=new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setTextSize(20);
c.drawText("Golden Stag", 30, 200, paint);
return c;
}
Could someone tell me where is the problem,please!?
Since the documentation for draw(Canvas) says:
"Manually render this view (and all of its children) to the given Canvas."
Maybe try with:
image.setImageBitmap(bitmapResult);
"Sets a Bitmap as the content of this ImageView."
Update: Example
I think this should work (can't test it though):
image = (ImageView) findViewById(R.id.imageview);
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
Canvas c = new Canvas(bitmap);
drawTextImage(bitmap);
image.setImageBitmap(bitmap);
private Canvas drawTextImage(Bitmap b){
Canvas c = new Canvas(b);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setTextSize(20);
c.drawText("Golden Stag", 30, 200, paint);
}