Moving the entire canvas - android

I'm making an Android game and I have shaped that I am drawing and they get big enough to leave the screen, I want to be able to move the canvas itself, so that I can center those shapes into the screen, and then go back to them being gone.
Is there such a way to move the canvas? i.e
canvas.move(10, 10);

You can translate the canvas, draw your game and than restore it. You can also draw part of it, translate and draw some more and than restore and continue drawing.
For example:
int offsetX = 10;
int offsetY = 10;
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.translate(offsetX, offsetY);
canvas.drawRect(box.x, box.y, box.x2, box.y2, paint);
canvas.restore();
}
Other option is to define an x,y offset that represents the amount you want to move the canvas and for each draw add this offset to the drawing.
For example:
int offsetX = 10;
int offsetY = 10;
protected void onDraw(Canvas canvas) {
canvas.drawRect(offsetX + box.x, offsetY + box.y, offsetX + box.x2, offsetY + box.y2, paint);
}

Related

I need to Draw a rectangle over image with touch event

I need to draw a rectangle over an image so that user can select a specific part of that image when the user selects a rectangular part must be drawn over it.
for example say if the user wants click image if a parking lot then user can draw rectangle on parking space
You have to override the onDraw() method on your view (ImageView), get the canvas and draw a rectangle. Something like that:
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint myPaint = new Paint();
int left = 10; // left padding from your view left border
int top = 10; // top padding from your view top border
int rectWidth = 50;
int rectHeight = 30;
myPaint.setColor(Color.rgb(0, 0, 0));
myPaint.setStrokeWidth(10);
canvas.drawRect(left, top, left + rectWidth, top + rectHeight, myPaint);
}

Keep bitmap inside a rectangle - android

I am trying to understand the basics of moving objects on the screen etc. I have a bitmap and I am moving that bitmap on the screen. How can I keep the bitmap inside the rectangle and still move it. I would like to put the bitmap in a rectangle as that will help in collision detection with other objects. Below is my code so far.
Thanks
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.yellow_ball);
x1 = 0;
y1 = 100;
#Override
protected void onDraw(Canvas canvas) {
if(x1 < canvas.getWidth()){
x1 += 5;
}
else{
x1 = 0;
}
canvas.drawBitmap(bitmap1, x1, y1, null);
}
You have the basic idea.
First, decide where you want to move the object to.
Then compare the new X coordinate to the rectangle's left and right bounds,
and if the new X coordinate is outside of the rectangle's bounds, reset the coordinate to the bound that it is exceeding.
Then, do the same for the Y coordinate.
Finally, move your bitmap to the adjusted X and Y coordinates.
I wrote code to do this a while back, see https://github.com/rfreedman/android-constrained-drag-and-drop-view for an example.

Draw Circle at Center of Canvas

I'm just getting into basic drawing with Android. I'm starting off with a few simple shapes but I'm having a few issues. I'd like to draw a circle at the center of a canvas. I looked at a few examples but can't seem to make it work. I think it's because I don't really understand what variables go where.
Could someone please explain the proper way to draw my circle at the center of my screen. Here is my code:
public class Circle extends View{
int width = this.getWidth();
int height = this.getHeight();
public Circle(Context context) {
super(context);
setFocusable(true);
}
protected void onDraw(Canvas canvas){
canvas.drawColor(Color.CYAN);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
//canvas.drawCircle(100, 100, 50, paint);
canvas.drawCircle(width/2, height/2, 100, paint);
Display disp = ((WindowManager)this.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
float radius = 0;//Radius caused an error so I initialized this variable
canvas.drawCircle(disp.getWidth()/2, disp.getHeight()/2, radius, paint);
}
}
width and height of the view have not been yet initialized when getWidth() and getHeight() are called, just use getWidth() and getHeight() in onDraw:
canvas.drawCircle(getWidth()/2, getHeight()/2, 100, paint);
You can also override onSizeChanged and get view width and height.
PS: do not create anything in onDraw, create the paint object in the constructor.
public void drawCircle(Graphics2D g, int x, int y, int radius) {
x = x-(radius/2);
y = y-(radius/2);
g.fillOval(x,y,radius,radius);
}
here x,y is the position of canvas where you want to draw circle and you can find it with motion listener if you want to set x,y position dynamically hope this will help you
There are some links which are very useful for us and I hope they will work for you and other.
https://github.com/swapgo20/Android-Hand-Drawing
https://github.com/codepath/android_guides/wiki/Basic-Painting-with-Views
https://github.com/Korilakkuma/CanvasView
I hope above links are very useful to draw shapes on canvas.
I suggest you use third link and use only Path class (http://developer.android.com/reference/android/graphics/Path.html) of android to draw shapes.

Issue with Canvas.rotate() in Android .How does it exactly work?

Please find the code of my onDraw method below(.I'm trying to rotate the canvas(//Rotate call -b) by 25 degrees after drawing the arc .But i find that the arc is still drawn from 0 to 50 degrees.I was expecting it to move by another 25 degrees.
public class CustomView extends View {
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
int px = getMeasuredWidth() / 2;
int py = getMeasuredHeight() / 2;
// radius - min
int radius = 130;
// Defining bounds for the oval for the arc to be drawn
int left = px - radius;
int top = py - radius;
int right = left + (radius * 2);
int bottom = top + (radius * 2);
RectF rectF = new RectF(left, top, right, bottom);
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
//canvas.rotate(25,px,py);//Rotate call -a
canvas.drawArc(rectF, 0, 50, true, paint);
canvas.rotate(25,px,py);//Rotate call -b
}
}
But if i place the rotate call(//Rotate call -a) before drawing the arc i see that the arc drawn is shifted by 25 degrees more .What exactly is happening here? Can someone explain to me?
Thanks
The Canvas maintains a Matrix that is responsible for all transformations on it. Even for rotation. As you can see in the documentation, the rotate method says:
Preconcat the current matrix with the specified rotation.
All transformations are done on the Canvas Matrix,hence, on the Canvas. The arc you draw isn't rotated. You first rotate the Canvas and then draw on it.
Hence, in your code, call -a works, not call -b.
EDIT:
For issues like postrotate and prerotate check the Matrix class(postRotate and preRotate methods).
Few Examples: this and this.
And few things you might want to read : this and this.

Compass with bitmap is going everywhere in the screen

i want my compass to spin like this
but my result is that:
the compass is going everywhere in my screen...
where is my problem please?this is my compass.java code:
#Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.GRAY);
int w = canvas.getWidth();
int h = canvas.getHeight();
int cw = w / 2;
int ch = h / 2;
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.compass);
canvas.translate(cw, ch);
if (mValues != null) {
canvas.rotate(-mValues[0]);
}
int cx = (mWidth - myBitmap.getWidth()) / 2;
int cy = (mHeight - myBitmap.getHeight()) / 2;
canvas.drawBitmap(myBitmap, cx, cy, null);
}
p.s.: i m sorry for the bad pictures but i really dont know how to explain my problem in english!Thanks
Since you have already translated to the center of the canvas, you may only need to offset the compass with its half width/height to center it. Try:
int cx = -myBitmap.getWidth() / 2;
int cy = -myBitmap.getHeight() / 2;
canvas.drawBitmap(myBitmap, cx, cy, null);
Also to get a good hang of transformations (translate, rotate), read The OpenGL Red book chapter 3, specifically the part Thinking about Transformations. While this is about OpenGL, you can use the knowledge for non-OpenGL transforms too.
EDIT:
Think in turtle logic. Your first translation takes your pencil to the center of your canvas. The rotation rotates your pencil. So now you could draw the compass exactly where the pencil is (no offsets), except that drawing the compass image is done starting from its top-left corner instead of its center. Therefore you need a last translation of (-compassWidth/2, -compassHeight/2). Note that this translation already occurs on the rotated x & y axes. Also note that you may pass 0/0 for cx/cy in drawBitmap if you manually apply that translation to the canvas itself.
So the full sequence is: translate to canvas center, rotate, translate negated to image center.
Don't decode the Bitmap in onDraw - do it when the view is created and reuse the Bitmap.
Make a Matrix and matrix.postRotate(mValues[0], half_width_of_bitmap, half_height_of_bitmap); and matrix.postTranslate(cw, ch);
Draw the bitmap with canvas.drawBitmap(bitmap, matrix, null);

Categories

Resources