android image rotate x axis - android

I have found a lot of solutoins how to rotate image relatively to point like whis.
But I need rotate image relatively to x axis, like this.
Any help appreciate.

try this,
ImageView imageView;
Matrix mMatrix = new Matrix();
imageView = (ImageView) findViewById(R.id.myimage);
Bitmap bmp = ((BitmapDrawable) getResources().getDrawable(
R.drawable.icon_150_380882090)).getBitmap();
Camera camera = new Camera();
camera.save();
camera.rotateY(50f);
//camera.rotateX(50f);
//camera.rotateZ(50f);
camera.getMatrix(mMatrix);
camera.restore();
Bitmap bm = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), mMatrix, true);
imageView.setImageBitmap(bm);

Related

Android: What is the correct way to rotate a bitmap?

The problem is when the bitmap gets rotated "R0" degrees, it looks like if it changed the pivot point. What I want is for it to rotate around the middle of the bitmap, but I don't know how.
Bitmap bitmap = (Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.base);
Matrix matrix = new Matrix();
matrix.reset();
matrix.setScale(bitmap.getWidth(), bitmap.getHeight());
matrix.setTranslate(-bitmap.getHeight()/2, -bitmap.getHeight()/2);
matrix.postRotate(R0);
Bitmap b = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
canvas.drawBitmap(b, x-24, y-40, null);
Thanks very much.
Try using matrix.postTranslate, instead of setTranslate, the latter will reset your matrix and setScale call will make no effect.

Null pointer Exception in Imageview.getDrawingCache on Secondtime

View vvv=mViewFlipper.getCurrentView();
RelativeLayout rrr=(RelativeLayout)vvv;
ImageView img=(ImageView) rrr.getChildAt(0);
img.setRotation(90);
img.setDrawingCacheEnabled(true);
img.buildDrawingCache();
Bitmap bitmap =img.getDrawingCache();
img.destroyDrawingCache();
img.setDrawingCacheEnabled(false);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);
rrr.removeViewAt(0);
ImageView img_new=new ImageView(ImageSlideShow.this);
img_new.setImageDrawable(bmd);
img_new.setScaleType(ScaleType.CENTER);
rrr.addView(img_new, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
I have Used Above code for Image rotating in ViewFlipper.
First time it is executed but second time it return NullPointerException...Error line:
Bitmap bitmap =Bitmap.createBitmap(img.getDrawingCache());
hi use this code and hopefully this works
basically your image view is null so first inintiallise it
ImageView img= new ImageView();
img=(ImageView) rrr.getChildAt(0);
img.setRotation(90);
img.setDrawingCacheEnabled(true);
img.buildDrawingCache();
if(img.isDrawingCacheEnabled()){
Bitmap bitmap =img.getDrawingCache(true);
}img.destroyDrawingCache();
img.setDrawingCacheEnabled(false);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
bao);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);
rrr.removeViewAt(0);
ImageView img_new=new ImageView(ImageSlideShow.this);
img_new.setImageDrawable(bmd);
img_new.setScaleType(ScaleType.CENTER);
rrr.addView(img_new, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
I got Solution For this problem
ImageView img=(ImageView) rrr.getChildAt(0);
Matrix matrix = new Matrix();
matrix.set(img.getImageMatrix());
matrix.postRotate(90, img.getWidth() / 2, img.getHeight() / 2);
img.setImageMatrix(matrix);
above code complete working for image rotation and you must set android:scaleType="matrix" in your imageview.

Rotate bitmap image around a center : Android [duplicate]

I've been looking for over a day for a solution to this problem but nothing helps, even the answers here. Documentation doesn't explain anything too.
I am simply trying to get a rotation in the direction of another object. The problem is that the bitmap is not rotated around a fixed point, but rather around the bitmaps (0,0).
Here is the code I am having troubles with:
Matrix mtx = new Matrix();
mtx.reset();
mtx.preTranslate(-centerX, -centerY);
mtx.setRotate((float)direction, -centerX, -centerY);
mtx.postTranslate(pivotX, pivotY);
Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, spriteWidth, spriteHeight, mtx, true);
this.bitmap = rotatedBMP;
The weird part is, it doesn't matter how I change the values within pre/postTranslate() and the float arguments in setRotation(). Can someone please help and push me in the right direction? :)
I hope the following sequence of code will help you:
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, config);
Canvas canvas = new Canvas(targetBitmap);
Matrix matrix = new Matrix();
matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2);
canvas.drawBitmap(source, matrix, new Paint());
If you check the following method from ~frameworks\base\graphics\java\android\graphics\Bitmap.java
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
Matrix m, boolean filter)
this would explain what it does with rotation and translate.
Edited: optimized code.
public static Bitmap RotateBitmap(Bitmap source, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
To get Bitmap from resources:
Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img);
I came back to this problem now that we are finalizing the game and I just thought to post what worked for me.
This is the method for rotating the Matrix:
this.matrix.reset();
this.matrix.setTranslate(this.floatXpos, this.floatYpos);
this.matrix.postRotate((float)this.direction, this.getCenterX(), this.getCenterY());
(this.getCenterX() is basically the bitmaps X position + the bitmaps width / 2)
And the method for Drawing the bitmap (called via a RenderManager Class):
canvas.drawBitmap(this.bitmap, this.matrix, null);
So it is prettey straight forward but I find it abit strange that I couldn't get it to work by setRotate followed by postTranslate. Maybe some knows why this doesn't work? Now all the bitmaps rotate properly but it is not without some minor decrease in bitmap quality :/
Anyways, thanks for your help!
You can also rotate the ImageView using a RotateAnimation:
RotateAnimation rotateAnimation = new RotateAnimation(from, to,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(ANIMATION_DURATION);
rotateAnimation.setFillAfter(true);
imageView.startAnimation(rotateAnimation);
You can use something like following:
Matrix matrix = new Matrix();
matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2);
RectF rectF = new RectF(0, 0, source.getWidth(), source.getHeight());
matrix.mapRect(rectF);
Bitmap targetBitmap = Bitmap.createBitmap(rectF.width(), rectF.height(), config);
Canvas canvas = new Canvas(targetBitmap);
canvas.drawBitmap(source, matrix, new Paint());
Look at the sample from Google called Lunar Lander, the ship image there is rotated dynamically.
Lunar Lander code sample
I used this configurations and still have the problem of pixelization :
Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.map_pin);
Bitmap targetBitmap = Bitmap.createBitmap((bmpOriginal.getWidth()),
(bmpOriginal.getHeight()),
Bitmap.Config.ARGB_8888);
Paint p = new Paint();
p.setAntiAlias(true);
Matrix matrix = new Matrix();
matrix.setRotate((float) lock.getDirection(),(float) (bmpOriginal.getWidth()/2),
(float)(bmpOriginal.getHeight()/2));
RectF rectF = new RectF(0, 0, bmpOriginal.getWidth(), bmpOriginal.getHeight());
matrix.mapRect(rectF);
targetBitmap = Bitmap.createBitmap((int)rectF.width(), (int)rectF.height(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(targetBitmap);
tempCanvas.drawBitmap(bmpOriginal, matrix, p);
matrix.reset();
matrix.setTranslate( anchor.x, anchor.y );
matrix.postRotate((float) rotation , 0,0);
matrix.postTranslate(positionOfAnchor.x, positionOfAnchor.x);
c.drawBitmap(bitmap, matrix, null);

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

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