Im trying to create a smooth line using Canvas.drawLine() and saving it to bitmap.
This is the code Im using
Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(ContextCompat.getColor(context, R.color.black));
paint.setStrokeWidth(3.5f);
paint.setDither(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setAntiAlias(true);
canvas.drawLine(x1, y1, x2, y2, paint);
What I get is this
But instead I would like to get something like this
Related
I'm tryin to write a text on canvas and set as GL10 object texture like this:
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
bitmap.eraseColor(0);
Canvas canvas = new Canvas(bitmap);
canvas.translate(width, height);
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(0xffffffff & Color.MAGENTA);
paint.setAlpha(255);
canvas.drawText("Hello world", 0, 30, paint);
the texture color is drawn if i change but the text not apears.
some one know why ?
It looks like the alpha setting is missing on the paint object that draws the text:
paint.setAlpha(255); //This line must be after paint.setColor(Color.MAGENTA);
or
paint.setColor(0xffffffff & Color.MAGENTA);
I currently have a button template (layout) as XML, where I load it off resources in my code and dynamically create the button.
So here's the question, is there any possible way to crop/cut the buttons' (or any view for that matter) into a specific shape?
Let's say I have a rectangle button, am I able to cut it to create some triangular form with it? here's an example:
What are the possibilities on there without having to create a custom Bitmap for it? (since the XML uses specific stroke/radius stuff which wouldn't work correctly on a Bitmap)
Is it possible for it to keep its size and margins even after the cuts? I'm really interested to know.
Thanks in advance to anyone willing to help!
You can use Canvas for what you want. It will allow you to modify the view by drawing shapes, erasing some area etc.
For example, following function will return a circular cropped bitmap of the initial bitmap provided:
public Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
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);
canvas.drawCircle((bitmap.getWidth() / 2), (bitmap.getHeight() / 2),
(output.getWidth() / 2), paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
canvas = null;
paint = null;
// bitmap.recycle();
return output;
}
To convert view into bitmap, see this.
I try to create a circle with help of Canvas, using the method drawCircle(...). But in the end why get I an ellipse instead of a circle ? How can I get "true" circle ?
Bitmap bitmap = Bitmap.createBitmap((int) (widthView), heightView, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.parseColor("#FF950000"));
paint.setAntiAlias(true);
paint.setStyle(Style.FILL);
canvas.drawRect(0.0f, 0.0f, widthView, heightLevel, paint);
paint.setColor(Color.parseColor("#FFD4A503"));
canvas.drawRect(0.0f, heightLevel, widthView, 2 * heightLevel, paint);
paint.setColor(Color.parseColor("#FF4A7816"));
canvas.drawRect(0.0f, 2 * heightLevel, widthView, 3 * heightLevel,
paint);
paint.setColor(Color.parseColor("#FFFFFFFF"));
canvas.drawCircle(30 * density, getLevelHeight(1), 20f, paint); graphicView.setBackgroundDrawable(new BitmapDrawable(getResources(),
bitmap));
Result:
Nexus 4
Huawei G500Pro (Shine)
You should use setImageBitmap(Bitmap) in place of setBackgroundDrawable().
Moreover, setBackgroundDrawable() has been deprecated.
I have an image, I can draw an circle over it like the code below:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.girl).copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(200);
paint.setStyle(Style.STROKE.FILL);
paint.setAntiAlias(true);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, 100,
paint);
And the circle will be red color! But because of some reason, I want the color of circle same as its under bitmap. Can I do it? and how? Thanks everyone!
Edited: One more question, How I can get all the pixels inside circle?
why dont you simply remove the paint.setStyle(Style.STROKE.FILL); or try changing the paint.setColor(Color.RED); to Transparent color, that should be it
you can get the color of a specified pixel by the method in this article and then make a RGB Color then set this color instead of the color.RED you mentioned.
I have an android application in which I need to download text from a website, convert it into bitmap format and display it on an LED-based display board.
I am struggling with the bitmap conversion.
Tried to use the following:
Bitmap mybitmap = Bitmap.createBitmap(100, 16, Bitmap.Config.ALPHA_8);
Canvas c = new Canvas(mybitmap);
c.drawText("0", 0, 0, paint);
But it doesn't seem to be working. Any suggestions?
Update:
Paint object is initialized like this:
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setTextSize(16);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.MONOSPACE);
I think you draw outside the image. Try setting y to 16.
c.drawText("0", 0, 16, paint);
Note that when drawing text the coordinate origin is the lower left coordinate corner.