How to translate and rotate in bitmap in android? - android

I am trying to translate and rotate a bitmap in android ImageView. But I can't get the result. Any pointers will be helpful.
int width = mb.getWidth();
int height = mb.getHeight();
// create a matrix for the manipulation
Matrix matrix = new Matrix();
matrix.preTranslate(width/2, height/2)
matrix.postRotate(angle);
Bitmap resizedBitmap = Bitmap.createBitmap(mb, 0, 0,
width, height, matrix, true);
imageview.setImageBitmap(resizedBitmap);

Related

android mirroring imageresource in src of imagebutton

I set the ImageResource of an ImageButton programmatically, while itself is created in xml:
<ImageButton
android:id="#+id/id_of_image_button"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_below="#+id/id_of_other_image_button"
android:layout_centerHorizontal="true"
android:background="#drawable/background_of_image_button"
android:contentDescription="#string/description_of_image_button"
android:onClick="onButtonClick"
android:scaleType="fitCenter" />
in java I set the src (depends on other code...)
ImageButton ib = (ImageButton) findViewById(R.id.id_of_image_button);
ib.setImageResource(R.drawable.src_of_image_button);
How can I mirror the ImageResource (src only, NOT the background)? Is there any solution (in Java/XML) which doesn't blow up the simple code? ;)
Add this method to your code
private final static Bitmap makeImageMirror(final Bitmap bmp)
{
final int width = bmp.getWidth();
final int height = bmp.getHeight();
// This will not scale but will flip on the X axis.
final Matrix mtx = new Matrix();
mtx.preScale(-1, 1);
// Create a Bitmap with the flip matrix applied to it.
final Bitmap reflection = Bitmap.createBitmap(bmp, 0, 0, width, height, mtx, false);
// Create a new Canvas with the bitmap.
final Canvas cnv = new Canvas(reflection);
// Draw the reflection Image.
cnv.drawBitmap(reflection, 0, 0, null);
//
final Paint pnt = new Paint(Paint.ANTI_ALIAS_FLAG);
// Set the Transfer mode to be porter duff and destination in.
pnt.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint.
cnv.drawRect(0, 0, width, height, pnt);
return reflection;
}
Then, get your mirrored image like so:
final ImageView imgMirror = (ImageView) findViewById(R.id.imgMirror);
imgMirror.setImageBitmap
(
makeImageMirror
(
BitmapFactory.decodeResource(getResources(), R.drawable.head_prof)
)
);
Result:
[EDIT]
You can get the VERTICAL mirror by using this matrix: mtx.preScale(1, -1);
You can get the HORIZONTAL + VERTICAL mirror by using this matrix: mtx.preScale(-1, -1);
If you want to flip it in the code you can do something like this:
BitmapDrawable flip(BitmapDrawable d)
{
Matrix m = new Matrix();
m.preScale(-1, 1);
Bitmap src = d.getBitmap();
Bitmap dst = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);
dst.setDensity(DisplayMetrics.DENSITY_DEFAULT);
return new BitmapDrawable(dst);
}
You can switch the preScale values if you want to mirror on the X axis.
Try this
Assuming you are going to mirror on Y axis
private void mirrorMatrix(){
float[] mirrorY = { -1, 0, 0,
0, 1, 0,
0, 0, 1
};
matrixMirrorY = new Matrix();
matrixMirrorY.setValues(mirrorY);
}
then get the mirrored bitmap and set it to the next imageButton.
private void drawMatrix()
{
Matrix matrix = new Matrix();
matrix.postConcat(matrixMirrorY);
Bitmap mirrorBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
ib.setImageBitmap(mirrorBitmap);
}
Note:
mirrorY with the Matrix.postConcat() generates a mirror image about Y axis.

Set ImageView orientation directly in Android?

I understand that I can rotate an ImageView in Android by
matrix.postRotate(finalOrientation - initOrientation, width, height);
But I want to set it as finalOrientation directly instead of rotating it by the difference, finalOrientation - initOrientation.
So I am expecting something like:
matrix.setOrientation(finalOrientation);
How can I do that?
Perfect! thanks #Bette Devine!
I made a function just for convenience.
public Bitmap rotateBmp(Bitmap bmp){
Matrix matrix = new Matrix();
//set image rotation value to 90 degrees in matrix.
matrix.postRotate(90);
//supply the original width and height, if you don't want to change the height and width of bitmap.
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),bmp.getHeight(), matrix, true);
return bmp;
}
I had to do this with a downsized image. I got a "java.lang.OutOfMemoryError" when I tried to do this with a full size image. : (
Matrix matrix = new Matrix();
//set image rotation value to 90 degrees in matrix.
matrix.postRotate(90);
//supply the original width and height, if you don't want to change the height and width of bitmap.
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, newWidth, newHeight, matrix, true);
Please refer the following link:
http://docs.xamarin.com/guides/android/application_fundamentals/handling_rotation/

android bitmap translate up and down

hello im new in android programming, i wanna ask how to translate bitmap up and down with float input ?
here is my code :
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.horizonsx);
bmpWidth = bitmap.getWidth();
bmpHeight = bitmap.getHeight();
drawMatrix();
private void drawMatrix(){
Matrix matrix = new Matrix();
matrix.setTranslate(0,10);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0,0, bmpWidth, bmpHeight, matrix, true);
imageviewrot.setImageBitmap(resizedBitmap);
imageviewrot.setScaleType(ScaleType.CENTER); ///THIS WAS THE KEY LINE
}
but failed to translate the bitmap up with value of 10. How i can do it ? thank you
make use of sprites...where a sprite will hold ur bitmap and then we can make translation/rotation on sprite
take some tips from here

error rotate image in android , image missing

I have code to rotate image like this
private void rotate(Bitmap src, float degree) {
// create new matrix
Matrix matrix = new Matrix();
// setup rotation degree
matrix.postRotate(degree);
// return new bitmap rotated using matrix
Bitmap ro = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
mImageView.setImageBitmap(ro);
recycleBitmap();
}
It work to rotate , but if I click rotate button continuously , my Image is not vissible , How to solve this? thanks
Try setting the postScale before the postRotate
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45);
What you are doing in your code is changing the martrix value each time you click on the rotate button.Changing values continuously may destroy the structure of the image.You can adjust or scale the matrix.Try this'
private void rotate(Bitmap src, float degree) {
// create new matrix
Matrix matrix = new Matrix();
matrix.postRotate(degree,px,py);//(px,py)is pivot point with reference to which the image is rotated
// return new bitmap rotated using matrix
Bitmap ro = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
mImageView.setImageBitmap(ro);
recycleBitmap();
}

How to Convert Matrix to Bitmap/Drawable?

How do you convert Matrix to Bitmap/Drawable?
Actually I do not understand your question. But if you want to resize a image then this might be helpful for you:
Bitmap bmap = BitmapFactory.decodeStream(getClass().getResourceAsStream("Your image path"));
int oldHeight= ..
int newHeight = ..
int oldWidth = ..
int newWidth = ..
scaleheight= newHeight/oldHeight
scaleWidth = newWidth/oldWidth
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap and set it back
Bitmap resizedBatteryImage = Bitmap.createBitmap(bmap, 0, 0,oldWidth, oldHeight, matrix, true);
try this code
ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
Matrix mat = new Matrix();
Bitmap bMapimage = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), mat, true);
the result of bMapimage have your bitmap image. And one more thing ,remember about the memory management while using bitmap,bitmapfactory...After using this try to set it as null.

Categories

Resources