I've been stumped by this one for a little bit, so I figured I'd ask here to see if anyone has any pointers.
In a nutshell, I have an app where I want multiple complex shapes to be drawn onto a canvas, which will then be drawn onto the screen (I'll be implementing panning around the large canvas as suggested in this question's answer: Android: Drawing and rotating on a canvas)
How exactly do I go about creating a custom shape and drawing it at arbitrary locations/rotations on a Canvas in Android?
Below is an example of a simple custom shape, as I've tried to implement it so far (typically they'd be created at run-time)
public class Symbol {
public Bitmap b = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
public Symbol() {
Canvas canvas = new Canvas(b);;
Paint paint = new Paint();
paint.setColor(Color.GRAY);
paint.setStyle(Paint.Style.STROKE);
paint.setTextSize(25);
paint.setStrokeWidth(4);
paint.setDither(true);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setAntiAlias(true);
String str="TestStr";
canvas.drawText(str, 250,250, paint);
}
}
Wow, I derped hardcore on this one.
Android: canvas.drawBitmap performance issue
Adding this in my main class made everything happy, at least as far as this simple case went.:
Symbol s = new Symbol();
canvas.save();
canvas.rotate(5);
canvas.drawBitmap(s.b, 0,0, null);
canvas.restore();
Related
There are many posts how to draw a dotted/dashed line or draw a border around circle. but I cannot find any information how to fill it. For example with DashPathEffect one can draw a stroke.
Is there any simple tool android provides similar to JS createPattern() ?
There is BitmapShader, which will allow you to create patterns. You can use it like this:
Paint paint = new Paint();
Shader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
paint.setShader(shader);
Then you use paint to draw on the canvas, and it will be filled with the specified pattern from the bitmap.
Hello and sorry for my bad English.
On my application, onDraw() method draws 10x10 field with lines using canvas.
Then, in other method I want some cells to be painted in yellow for example.
The code is:
Paint ship = new Paint();
ship.setColor(getResources().getColor(R.color.ship_color));
Canvas canvas = new Canvas();
Rect r = new Rect(x*(rebro_piece),y*rebro_piece, x*(rebro_piece+1), y*(rebro_piece+1));
canvas.drawRect(r, ship);
but nothing happens. What shall I do?
UPD: am I right, that Canvas only draws within onDraw() method and from nothing else?
Background: Using Canvas, Paint and Path objects I draw several geometries on the canvas, mostly polygons and circles. They fill most of the Android screen.
Question: With Mathematica I can 'fast-copy' Graphics using Translate ( in x and y direction ), after which the resulting image is automatically zoomed out such that all copies are visible. ( For example. Draw a square that fills the entire screen, copy it using (2,2) and four squares appear. ) The premise is that copying is a faster operation. - Is a similar operation possible on Android?
There's nothing as convenient as that, but to achieve the effect you can draw directly to a Bitmap and re-use it - scaling and translating it yourself.
public void onDraw(Canvas canvas) {
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas bmpCanvas = new Canvas(bmp);
// draw into bmpCanvas
// ...
// draw bitmap using
// public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)
canvas.drawBitmap(bmp, ...);
I am developing an Android Games. I have created a canvas on screen. I want to show an object for 2 second on its screen and then it should be disappear.
How can I achieve this?
Use this code to remove your canvas object
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
canvas.drawPaint(paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
else
Canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
Keep 2 copies of bitmap. On 2nd dont draw the object and draw this 2 bitmaps alternatively, or look into this
will help you
How to get a well understanding the concept of the classes of Canvas,Drawable,Bitmap and Paint? What's the relationship among them ?
Could someone please give me an example?
Thanks a lot.
From the Canvas class documentation:
The Canvas class holds the "draw"
calls. To draw something, you need 4
basic components: A Bitmap to hold the
pixels, a Canvas to host the draw
calls (writing into the bitmap), a
drawing primitive (e.g. Rect, Path,
text, Bitmap), and a paint (to
describe the colors and styles for the
drawing).
So you need 4 components in order to draw something.
Bitmap
Canvas
Drawable
Paint
Let's say you want to draw a circle on a background image from your drawable folder.
Canvas canvas;
Bitmap mBG;
Paint mPaint = new mPaint();
mBG = BitmapFactory.decodeResource(res, R.drawable.apple); //Return a bitmap from the image from drawable folder
Canvas.drawBitmap(mBG, 0,0, null); //Draw the bitmap
mPaint.setColor(Color.BLACK); //Set paint to BLACK color
Canvas.drawCircle(100,100,30, mPaint); //Draw circle at coordinate x:100,y:100, radius 30 with the paint defined
Android's Official documentation covers all the details about the API. And best way to learn something is learn by doing, so after reading the docs, google for tutorials and experiment, all your doubts will be cleared gradually.
Canvas
Drawable
Bitmap
Paint