I'm creating a live wallpaper that I want a Picture to cover the entire canvas so I draw it like this when in landscape mode
Rectangle r = new Rect(0, canvas.getHeight()- (canvas.getHeight()-statusBarHeight), canvas.getWidth(), canvas.getHeight());
c.drawPicture(pictureBackground, r);
Notice I have to take into consideration the status bar. Okay, that works fine. Now what I want to do is rotate the canvas 90 degrees for portrait mode (btw I can't just rotate the picture outside of code, have to do it this way) so I do this:
canvas.rotate(90,canvas.getWidth()/2,canvas.getHeight()/2);
Now what rectangle should I use to cover the canvas in its entirety when I go to draw the picture again (remember I have to account for the status bar)????
Rectangle r = new Rect(?,?,?,?)
I can't figure it out, I've tried so many possible combinations
Related
I'm creating a view that will allow me to draw over a given bitmap.
As I create the canvas I set the bitmap as the base for it. It works perfectly on an emulator but, when I try to use it on my phone it behave strangely and rotate the bitmap by 90 degrees.
Here is how I create the canvas. I get the right backgroundPicture from my previous activity.
mBitmap = backgroundPicture.copy(Bitmap.Config.ARGB_8888, true);
mBitmap = Bitmap.createScaledBitmap(mBitmap, width, height, true);
mCanvas = new Canvas(mBitmap);
ANd here are the screenshots
When I take the picture :
What it displays in my recycler view (displays normally)
What it displays on the canvas (90 degrees on the left)
Thank you for your help, I'm not used to work with canvases so I'm a bit lost :/
After some digging it appears that the problem comes from my smartphone, a Samsung Galaxy A3. In some Samsung models the camera is rotated to gain place for the others components of the phone. Some Samsung handle the difference but if they don't you'll get an rotation of 0 and you'll have to handle the rotation problem model by model manually.
I hope this will help other people.
Cordially,
Matthieu
I'm currently working on an Android app. I have a canvas onto which the user will draw something. I need to have a picture as the background of this canvas.
I already managed to import a pic and set it as the background using
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_background_pic));
However, the picture appears ten times bigger than the screen.
Is it possible to keep the picture resized, so it fills the canvas and not more?
Also, later I will need to add other things to the activity. The canvas will not be the only thing on the screen, so I need the picture to be the size of the canvas, and not the screen.
Thanks a lot.
Nevermind I found it:
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvasWidth, canvasHeight), null);
By Kai at stackoverflow.com/a/27466127/8205282
I have an activity with an imageView which has a canvas, and under it a button. when I click the button a new bitmap gets drawn into the canvas with text ..
How do I make the drawn bitmap moveable like I drag it into any place I want in the canvas ?
Canvas c = new Canvas(WorkBitmap);
c.drawText("Bitmap", 360, 520, null);
c.drawBitmap(ball, x, y, null);
I saw some tutorials on SurfaceView and I did them, thye worked fine. But I want the bitmap in my canvas to move, in the tutorials its the whole screen a surfaceView canvas ..
You need to use the SurfaceView widget that is available in the XML
But if you want the image to be move-able around all the screen you could use SurfaceView
Check this Youtube video, It's very informative (watch all the SurfaceView playlist to benefit)
I'm using the matrix.rotate(deg, fx,fy) method to rotate an image.
The image has an alpha channel and only has something visual in the right-hand corner. I want to rotate the canvas, create the new image with that matrix and have the visual part be the only part that is rotating.
At the moment the image rotates but doesn't rotate around the desired axis. It always rotates around the original width of the object.
Here is the code I use:
Matrix matrix = new Matrix();
matrix.setRotate(rotation, xPivot, yPivot);
Bitmap pic1a = Bitmap.createBitmap(pic1, 0, 0, pic1.getWidth(), pic1.getHeight(), matrix, true);
This rotates the image correctly but only seems to rotate within the same location of the original canvas.
What I want is for the point that the image rotates around (xPivot, yPivot) becoming the centre location on the display.
I would like to display a transperent PNG of a "light" line shape according to user's sliding path.
I'd like to make similar effect like Fruit Ninja has, and leave a track after user slides his finger.
I already have the x,y points of his finger - using onTouch method, and checking the x,y on MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP but how do i draw an image that will be tilted and displayed at those positions? all i know is to add padding/margin to an image, not how to place it using x,y, or how to rotate it..
For positioning and rotating use a canvas (getSurfaceHolder().lockCanvas()) and draw inside it using drawBitmap.
public void drawBitmap (Bitmap bitmap, Matrix mtx, Rect dst, Paint paint)
the matrix can include the rotation:
Matrix mtx = new Matrix();
mtx.postRotate(90);
You might want to se the code from API demos, FingerPaint.