rotate bitmap and maintain size - android

I am creating a tile board game.
I want to rotate a bitmap tile piece by a few pre-determined degrees.
When I rotate my bitmap, the size changes.
For example, if I want a 75x75 triangle tile piece, on rotation I get a 68x68 back from this code. How can I keep things the same size so everything remains the size for the board?
Here's what I'm using to rotate:
public class RotatebitmapActivity extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.t123);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 75;
int newHeight = 75;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(120);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);
// center the Image
imageView.setScaleType(ScaleType.CENTER);
// add ImageView to the Layout
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// set LinearLayout as ContentView
setContentView(linLayout);
}

What is happening in your code, is that the bitmap that is created is the correct size. However, all Bitmaps are automatically scaled to fit the density of the screen that is currently in use.
There are two ways (that I know of) to get the result you desire.
You can set the density of a bitmap after creating it and before wrapping it in a drawable. To do this, add code similar to:
resizedBitmap.setDensity(DisplayMetric.DENSITY_HIGH);
This code sets the density that the bitmap is designed for, and can prevent Android from auto-scaling the image.
The second solution is more of a methodology than a specific solution to this particular problem. I won't go into detail, for that see Supporting Multiple Screens | Android Developers
Hopefully someone will happen along that can answer your question better than I can.

Related

Resizing an image to fit on a imageview

I am developing an application in which I need to fit an Bitmap into Imageview with specific dimensions(let's suppose 350dpx50dp - height*width).
I wanted to do something similar like this: http://gyazo.com/d739d03684e46411feb58d66acea1002
I have looked here for solutions. I found this code for scale the bitmap and fit it into imageview, but the problem is that imageview becomes greater when I add the bitmap into him:
private void scaleImage(Bitmap bitmap, ImageView view)
{
// Get current dimensions AND the desired bounding box
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int bounding = dpToPx(350);
// Determine how much to scale: the dimension requiring less scaling is
// closer to the its side. This way the image always stays inside your
// bounding box AND either x/y axis touches it.
float xScale = ((float) bounding) / width;
float yScale = ((float) bounding) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
// Create a new bitmap and convert it to a format understood by the ImageView
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
// Apply the scaled bitmap
view.setImageBitmap(scaledBitmap);
}
Using this code I can get this : http://gyazo.com/e9871db2130ac33668156fc0cf773594
But that's not what I wanted, I want to keep the dimensions of imageview and add the bitmap into imageview without modifying the dimensions of imageview and occupying all the imageview's surface. Like the first image.
Why don't you just add android:scaleType="fitXY" to your ImageView in xml?

Image not showing in imageview when selecting rear camera pictures

I have a weird issue running in my application. My application works like this, something similar to setting up your profile picture on Facebook or Twitter.
Similar to that I have an ImageView which when I click, invokes an intent to open my gallery pictures. But the issue is, all my pictures get selected expect for those that I clicked with my rear camera. The resolution of my camera pictures are 4208x3120.
I tried to re-size the pictures using Bitmap.createscaledimage() method as my first option but no luck (rear camera images get selected but are not showing in the imageview and here's my option 2 code.
public class bitmaptest extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);
// center the Image
imageView.setScaleType(ScaleType.CENTER);
// add ImageView to the Layout
}
}
Even this turns out to be negative. Only pictures that are taken from the front camera appear on my imageview.
When I select any rear camera images , it again gets selected but doesn't show up on the ImageView.
Can someone help me to solve this? Thanks in advance.

ImageView cutting from sides after rotation

I have a custom ImageView. I want to rotate my imageview at some angle. Here is its onDraw() method:
canvas.save();
canvas.rotate(currentAngle,getWidth()/2,getHeight()/2);
super.onDraw(canvas);
The image is rotating but the problem is after rotation, the images is cutting from sides. How to avoid that cutting thing?
Sample Screenshot:
Try for the following code:
public class bitmaptest extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);
// center the Image
imageView.setScaleType(ScaleType.CENTER);
// add ImageView to the Layout
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// set LinearLayout as ContentView
setContentView(linLayout);
}
}
Was once I have a similar problem. I was decided this problem that
way. I zoomed image in so that it looked like
|_ __ |
| | | |
||_||
|_ __ _|
Half width image added left and right. And half height added top and bottom. New space fill some color, or do alapha canal equals+ 0. When you rotate the image, you needed cut the excess. I did that not on Android, but i hope this can help you.

Displaying images and managing memory in android

I wrote a program that at any time displays 8 user selected images on the screen. Each image is taken from its original form and scaled down to a uniform size. In order to do this I am using the code below:
Bitmap bitmapOrg = BitmapFactory.decodeFile(File Location Here);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 100;
int newHeight = 100;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
//ImageView imageView = new ImageView(this);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
// set the Drawable on the ImageView
iv.setImageDrawable(bmd);
// center the Image
iv.setScaleType(ScaleType.CENTER);
Even though my code works its not perfect. It seems like I'm using up a lot of memory especially calling this code possibly 8 times at once. Where in the code would I "recycle" the memory and how could I make this code possibly run better?
EDIT:
So I implemented the code in my project and it was working perfect and then I tried to add it to other sections and it just stopped working all together. My code looks like this: any idea what am I doing wrong?
BitmapFactory.Options options = new BitmapFactory.Options();
options.outWidth = 50;
options.outHeight = 50; Bitmap bitmap = BitmapFactory.decodeFile(path, options);
ImageView iv = (ImageView) findViewById(R.id.imageView7);
iv.setImageBitmap(bitmap);
You can use BitmapFactory.Options to scale the image as you decode it rather than reading in a full image and then scaling it.
BitmapFactory.Options options = new BitmapFactory.Options();
// You can play with this setting depending on how large your images are
// For example, to scale ~400x400 images to ~100x100, you can use 4.
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
Edit - George is correct. You should use inSampleSize to create a smaller image of the general size you need and then have it resized to the exact size you want using your ImageView. I've corrected my answer above to reflect this.
In any case, you should be much better off memory-wise if you are scaling the bitmaps during decode.
#matthew-willis I do not think you can use outWidth and outHeight to scale a bitmap. I believe they are output parameters only: they report the size of the bitmap created after the fact--setting them prior to decoding has no effect. You should use inSampleSize if you want to scale as you decode. George

Android: Resizing and scaling a part of the image

Given an image, I want to be able to scale only a part of that image. Say, I want to expand half of the image so that, that half takes up the whole space.
How is this possible?
Will ImageView fitXY work because I thought it'll work only for the whole original image.
#Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout linearLayout = new LinearLayout(this);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = 640;
int newHeight = 480;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// create the new Bitmap object
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 50, 50, width,
height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
imageView.setImageDrawable(bmd);
imageView.setScaleType(ScaleType.CENTER);
linearLayout.addView(imageView, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(linearLayout);
}
}
This works only if in createBitmap, the X and Y coordinates of the first pixels in the source are 0. meaning, I'm not able to take a subset of the image. Only able to scale the whole image. But createBitmap is meant to subset an image.
In the log, when the parameters are not 0, I get the following exception: java.lang.IllegalArgumentException: x + width must be <= bitmap.width()
Please help
First you have to create a new bitmap with the part you want to scale using
createBitmap() //pass the source bitmap, req height and width
Now from the result bitmap, you have to create your scaled bitmap using
createScaledbitmap() //pass the result bitmap , req width, height
For exapmle:
Bitmap originalBitmap = BitmapFactory.decodeResource(res, id);
Bitmap partImage = originalBitmap.createBitmap(width, height, config);
Bitmap scaledImage = partImage.createScaledBitmap(partImage, dstWidth, dstHeight, filter);
So I had to fix a couple of Typos, but this example did a good job for me.http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
typos:
int width = bitmapOrg.width();
int height = bitmapOrg.height();
become:
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
otherwise, worked when I tried it agains SDK 7

Categories

Resources