I am taking an image and drawing it to the screen using canvas. I want to scale it based on the screen size.
This is what I tried, but it cuts off a large chunk of the image:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.myimage);
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
Rect frameToDraw = new Rect(0, 0, width, height);
RectF whereToDraw = new RectF(0, 0, width, height);
canvas.drawBitmap(bitmap,frameToDraw,whereToDraw, paint);
I know I am doing a few things wrong, but I'm not exactly sure what. This code causes the image to exceed the size of the screen. I want it to be scaled to the size of the screen. I'll be drawing smaller images in too that I also will need to scale according to the size of the screen, though not to the full screen.
Rect frameToDraw = new Rect(0, 0, width, height);
RectF whereToDraw = new RectF(0, 0, width, height);
Take a look at the above code. Your are not scaling the picture. You are simply taking a part of the picture (in this case the whole picture) and pasting it somewhere (in this case the size of the original picture).
The width and heigth of the frameToDraw rectangle should be the width and height of your picture.
dont need creat scale bitmap, instead:
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,frameBufferHeight, Config.RGB_565);
frameBufferWidth and frameBufferHeight are the sizes that you want to make your app base on those(for example 480*800)
then simply add this code:
Rect dstRect = new Rect();
canvas.getClipBounds(dstRect); // get canvas size base screen size
canvas.drawBitmap(framebuffer, null, dstRect, null);
it draw your final bitmap according to screen size
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
If you get your width and height like this, you aren't going to take the size of any toolbar/status bar/navigation bar into account.
If you are coding an app that has a View, inflate a layout with an ImageView that is full size (i.e. match_parent), then use the scaleType to scale/fit/crop the image how you want.
Related
I am taking an image and drawing it to the screen using canvas. I want to scale it based on the screen size.
This is what I tried, but it cuts off a large chunk of the image:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.myimage);
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
Rect frameToDraw = new Rect(0, 0, width, height);
RectF whereToDraw = new RectF(0, 0, width, height);
canvas.drawBitmap(bitmap,frameToDraw,whereToDraw, paint);
I know I am doing a few things wrong, but I'm not exactly sure what. This code causes the image to exceed the size of the screen. I want it to be scaled to the size of the screen. I'll be drawing smaller images in too that I also will need to scale according to the size of the screen, though not to the full screen.
Rect frameToDraw = new Rect(0, 0, width, height);
RectF whereToDraw = new RectF(0, 0, width, height);
Take a look at the above code. Your are not scaling the picture. You are simply taking a part of the picture (in this case the whole picture) and pasting it somewhere (in this case the size of the original picture).
The width and heigth of the frameToDraw rectangle should be the width and height of your picture.
dont need creat scale bitmap, instead:
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,frameBufferHeight, Config.RGB_565);
frameBufferWidth and frameBufferHeight are the sizes that you want to make your app base on those(for example 480*800)
then simply add this code:
Rect dstRect = new Rect();
canvas.getClipBounds(dstRect); // get canvas size base screen size
canvas.drawBitmap(framebuffer, null, dstRect, null);
it draw your final bitmap according to screen size
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
If you get your width and height like this, you aren't going to take the size of any toolbar/status bar/navigation bar into account.
If you are coding an app that has a View, inflate a layout with an ImageView that is full size (i.e. match_parent), then use the scaleType to scale/fit/crop the image how you want.
I want to place the watermark on the bottom right of the picture. But, in some mobiles it is coming perfectly and in some mobile phones, the watermark is coming on the whole image.
Here is my code:
rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0,
loadedImage.getWidth(), loadedImage.getHeight(),
rotateMatrix, false);
int w = rotatedBitmap.getWidth();
int h = rotatedBitmap.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, rotatedBitmap.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(rotatedBitmap, 0, 0, null);
Bitmap waterMark = BitmapFactory.decodeResource(getResources(), R.drawable.watermark2);
int ww = waterMark.getWidth();
int hh = waterMark.getHeight();
canvas.drawBitmap(waterMark,(w-ww),(h-hh), null);
EDIT: Here are the screenshots of the result. In second picture, the watermark is coming perfectly and in first picture, it is coming on the whole picture.
Make use of aspect ratio. Calculate aspect ratio of device and accordingly calculate appropriate size for your water mark.
How to calculate aspect ratio?
Check for width and height of screen whichever is bigger divide it by smaller one
example:
if(screen_width > screen_height){
aspectRatio = screen_width/screen_height
}else{
aspectRatio = screen_height/screen_width
}
now that you have calculated aspect ratio multiply it with size of you water mark and scale it accordingly.
like:
float ww = watermark.getWidth()*aspectRatio;
float wh = watermark.getHeight()*aspectRatio;
and use these values.
Hope this helps...
Just Give ratio and you go to go,try changing ratio values that best fit for your src bitmap .
Embeds an image watermark over a source image to produce a
watermarked one.
#param source The source image where watermark should be placed
#param ratio A float value < 1 to give the ratio of watermark's
height to image's height,
try changing this from 0.20 to 0.60 to obtain right results
public static Bitmap addWatermark(Context context, Bitmap source, float ratio)
{
Canvas canvas;
Paint paint;
Bitmap bmp;
Matrix matrix;
RectF r;
int width, height;
float scale;
Bitmap waterMark = BitmapFactory.decodeResource(context.getResources(), R.drawable.watermark3x);
width = source.getWidth();
height = source.getHeight();
// Create the new bitmap
bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);
// Copy the original bitmap into the new one
canvas = new Canvas(bmp);
canvas.drawBitmap(source, 0, 0, paint);
// Scale the watermark to be approximately to the ratio given of the source image height
scale = (float) (((float) height * ratio) / (float) waterMark.getHeight());
// Create the matrix
matrix = new Matrix();
matrix.postScale(scale, scale);
// Determine the post-scaled size of the watermark
r = new RectF(0, 0, waterMark.getWidth(), waterMark.getHeight());
matrix.mapRect(r);
// Move the watermark to the bottom right corner
matrix.postTranslate(width - r.width(), height - r.height());
// Move the watermark to the bottom left corner
// matrix.postTranslate(0 , height - r.height());
// Move the watermark to the top-left corner
// matrix.postTranslate(0 ,0);
// Move the watermark to the top right corner
// matrix.postTranslate(width - r.width(), 0l̥);
// Draw the watermark
canvas.drawBitmap(waterMark, matrix, paint);
return bmp;
}
I have many icons in 512 x 512 resolution and i want to set them
on ImageButtons. Ofcourse i dont want the ImageButtons to be
512 x 512 big i want them to be as big as its optimal for the
screen size of the current device.
I create my buttons like this:
this.buttonMeasure = new ImageButton(this);
this.buttonMeasure.setImageResource(R.drawable.ic_measure);
this.buttonMeasure.setOnLongClickListener(this);
How can i scale down the images to an optimal size dependent
to the screen size of the device programmatically?
I tried this but that leads to an outOfMemoryError:
public static Bitmap getScaledBitMap(Context context, Drawable sourceDrawable) {
Bitmap sourceBitmap = ((BitmapDrawable)sourceDrawable).getBitmap();
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
int width = sourceBitmap.getWidth();
int height = sourceBitmap.getHeight();
float scaleWidth = metrics.scaledDensity;
float scaleHeight = metrics.scaledDensity;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
return Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true);
}
I don't think you will be needing this at all. You specify the size of your ImageButton using properties like android:layout_width and android:layout_height for different layouts in folders, Similarly you need to place copy of images of various resolutions in respective folders with the same name.The appropriate image is automatically picked up by android itself at run time.
I am trying to make a square larger by using createScaledBitmap. However, the square becomes a rectangle with width longer than height. What is the proper way to do this in order to keep the aspect ratio constant?
public Bitmap getScaledBitmap(Bitmap bitmap){
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
scaledBitmapW = (int)(dm.widthPixels/(10)); *//make the square just 1/10 of the screen width*
scaledBitmapH = (int)(scaledBitmapW*(bitmap.getHeight()/bitmap.getWidth()));
scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledBitmapW, scaledBitmapH, true);
return scaledBitmap;
}
I want user to select a picture from gallery. Since size of most pictures are very large I want to scale it to the most appropriate width. Is there a way to do that when I only know the path of the picture?
For example, if your Bitmap is bmp, and ImageView is framePhoto:
int iWidth=bmp.getWidth();
int iHeight=bmp.getHeight();
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
int dWidth=dm.widthPixels;
int dHeight=dm.heightPixels;
float sWidth=((float) dWidth)/iWidth;
float sHeight=((float) dHeight)/iHeight;
if(sWidth>sHeight) sWidth=sHeight;
else sHeight=sWidth;
Matrix matrix=new Matrix();
matrix.postScale(sWidth,sHeight);
newImage=Bitmap.createBitmap(bmp, 0, 0, iWidth, iHeight, matrix, true);
framePhoto.setImageBitmap(newImage);