How to Convert Matrix to Bitmap/Drawable? - android

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.

Related

rotates image just once. how to rotate again when selected rotate option multiple times in menu item list?

public void Rotate(View v)
{
ImageView img = (ImageView)findViewById(R.id.imageView1);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),arr[current]);
// Getting width & height of the given image.
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting pre rotate to 90
Matrix mtx = new Matrix();
mtx.preRotate(90);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
img.setImageBitmap(rotatedBMP);
}
when i select rotate option in menu it only rotates first time when i again select it, image wont rotate. any soln to it
actually, it does rotate your Bitmap each time for 90 degrees. Problem is you always get the same(not rotated) bitmap from the resources, not already rotated from your ImageView.
Instead of:
BitmapFactory.decodeResource(getResources(),arr[current]);
Try with this:
Bitmap bmp = ((BitmapDrawable)img.getDrawable()).getBitmap();
This solution is also better because you don't decode bitmap from resources each time.
i just used simple Math.
public void Rotate_r(View v)
{
rcount++;
if(rdgr==360) rdgr=90;
else rdgr=rdgr+90;
if(rcount==1){
ldgr=rdgr;
rcount--;
}
ImageView img = (ImageView)findViewById(iv);
Bitmap bmp =BitmapFactory.decodeResource(getResources(),arr[current]);
// Getting width & height of the given image.
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting pre rotate to 90
Matrix mtx = new Matrix();
mtx.preRotate(rdgr);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
#SuppressWarnings("deprecation")
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
img.setImageBitmap(rotatedBMP);
}
i used a counter rcount which simply increments if image is rotated right and decrements if it is rotated left. also a variable rdgr is initialized to 90 as rdgr=90 and when rcount is incremented it adds 90 more to rdgr and plots the image.

android image rotate x axis

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);

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.

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 translate and rotate in bitmap in 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);

Categories

Resources