I want to get bitmap from imageview with vectordrawable resource that rotated on X or Y axis but the quality of bitmap is not good. The quality without rotation or "setRotation" is good but when I use "setRotationX" or "setRotationY" or Camera to rotate, the bitmap quality comes down. This problem is just when I set "setImageResource" to a vectordrawable, jpeg or png images have not this problem. I think this is a bug on vectordrawable...
This is my code to get a bitmap:
itemBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(itemBitmap);
cv.save();
imageView.draw(canvas);
cv.restore();
If you're trying to get a bitmap from an imageView, the quality of the bitmap is more likely to be degraded. If the image is rotated it will have even less quality as the bitmap needs to be zoomed out.
You can try following things:
convert the vector resource directly into bitmap and rotate using matrix or some libraries. or,
turn off hardware acceleration. or,
Use imageView.setDrawingCacheEnabled(false); followed by Bitmap bitmap = imageView.getDrawingCache();
Pl refer to kc Ochibili's answer for question at following address:
How to Get Pixel Colour in Android?
I am using same on my VectorDrawable like (what I have shown below). I am giving code details like a narration because I am posting this from my mobile and there is problem with indentation which is not accepted by the system at stackoverflow.
First attach a listener to the ImageView like
myImageView.setOnTouchListener(new View.OnTouchListener(){
Then override the following method:
Public boolean onTouch(View view, MotionEvent motionEvent){
Then from the view parameter received take IageView
ImageView img = (ImageView) view;
Then on the image view do the following:
Img.setDrawingCacheEnablef(true);
Then take bitmap from that
Bitmap imgbmp = Bitmap.createBitmap(img.getDrawingCache());
Immediately setDrawingCacheEnabled(false) on img.
You have the bitmap.
Related
I have two ImageView's in a RelativeLayout, the first one is as large as the RelativeLayout size (as background),and the second ImageView has a small image, now I want to combine these to ImageView's image into one by screenshot. not by combine two bitmap directly.
In the snippet below, I successfully take screenshot of the RelativeLayout, but I found the image quality is not as good as the first ImageView's bitmap, can anyone help me please?
view.setDrawingCacheEnabled(true);
view.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap drawCache = view.getDrawingCache(true);
Try to get bitmap using window DecorView :
View window = activity.getWindow().getDecorView()
Canvas bitmapCanvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(window.getWidth()*2, window.getHeight()*2, Bitmap.Config.ARGB_8888);
bitmapCanvas.setBitmap(bitmap);
bitmapCanvas.scale(2.0f, 2.0f);
window.draw(bitmapCanvas);
bitmap.compress(Bitmap.CompressFormat.PNG, 0, myOutputStream);
Ref : High resolution screen shot in Android
I'm trying to capture an imageView and out it in a bmp. It currently works but, i am not able to make the captured imageView the correct size. This gives me a problem was black bar on the side of my imageView when it put the drawingcache as a bmp back into the imageview for display.
My code is like this:
imageView.setDrawingCacheEnabled(true);
Bitmap bm = Bitmap.createBitmap(imageView.getDrawingCache());
I tired doing :
Bitmap bm = Bitmap.createBitmap((int) dw, (int) dh,imageView.getDrawingCache());
but imageView.getDrawCache (BMP type) is not same type as Bitmap.Config.ARGB_8888. I'm not sure how to tackle this specific problem. Any help would be nice.
Please read this question for more detail and screenshots.
Black bars on the side of screen capture on imageView
I have some SVGs in my assets folder and I need to dynamically set them in my widget (on an ImageView).
I am using this library: http://code.google.com/p/svg-android/
This library returns a Picture or a PictureDrawable.
The only methods I can see to use on RemoteViews are setImageViewBitmap which obviously takes a bitmap.
I tried looking for code to convert a Drawable to a Bitmap like this:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;
But the bitmap is too small. When I create the bitmap in Illustrator I set the artboard size to 65 which is what comes through on the intrinsic width/height.
My widgets can be resized so the ImageView sizes are variable. Even if I set the width and height statically to some large number like this...
Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
then the resulting bitmap just has a bunch of whitespace below and to the right of a tiny image.
I guess I need to somehow draw the picture at a scaled up value as well as creating the Bitmap at size 300. Ideally I could figure out the size of the ImageView at runtime and set the proper sized Bitmap if I knew that. Is this the best approach and how would I do this? Perhaps there is a better approach I don't even know about?
I've not used android-svg but if it's using vanilla PictureDrawables, then it should be just a matter of not using the intrinsic bounds.
Try the following:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
pictureDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
pictureDrawable.draw(canvas); // do not access the Picture directly, that defeats the purpose
currentBitmap = bitmap;
In short, use the Drawable, not its Picture and set the Drawable's bounds to be the full canvas.
Have you tried createScaledBitmap?
createScaledBitmap()
I have tried svg-android and it has not worked for me for this very reason. Not to mention its severely limited feature set.
The point of using vector graphics is that I can generate images of any appropriate size to fit the UI View size. Which means the generation method must accept the size requirements at run-time, and not always use width,height declared in <svg> tag.
Hence I used the native implementation: libsvg-android, which exactly does that.
It directly renders to a canvas with given size:
long objId = SvgRaster.svgAndroidCreate();
SvgRaster.svgAndroidParseBuffer(objId, readString(mInputStream, "UTF-8"));
SvgRaster.svgAndroidSetAntialiasing(objId, true);
SvgRaster.svgAndroidRenderToArea(objId, mCanvas, 0, 0, mWidth, mHeight);
I ended up modifying the underlying Artboard size to be 300. None of the scaling up methods worked. So the final code I used was that which I originally posted:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;
Once I had a 300x300 Bitmap I was able to set it into the RemoteViews using setImageViewBitmap .
This kind of defeated the purpose of using SVGs in the first place (at least as far as using them in widgets was concerned).
My problem was not the same as User117. Perhaps he did not name the outer layer 'bounds' as was required in the library. Either way, I managed to get the library working, albeit by having to modify the SVG Artboard size.
Hopefully with the increase in screen resolution Android will introduce SVGs as part of the platform soon.
I want to crop Red part from following image, Is there any simple method available in android that can crop following image.
I have found many SO questions but all are suggesting to used following code:
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100);
This code work well if width & height are around 2MP resolution, but if that cropped part is more than 3MP resolution than application got crashed with OOM error.
Is there any way that handle image more than 3MP during cropping?
You can used following code that can solve your problem.
Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100, matrix, true);
Above method do postScalling of image before cropping, so you can get best result with cropped image without getting OOM error.
For more detail you can refer this blog
1- Change your imageview for bitmap
final Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.img);
2-use your bitmap to crop what you want
Bitmap croppedBmp = Bitmap.createBitmap(bitmap, x, y , width , height);
3-Take care x,y from Top and left
4- to preview your bitmap again in your imageview
imageView.setImageBitmap(croppedBmp);
If you want to crop image in any shape OR only selected part then
you can use ready made open source library
I have a board game app that creates a lot (hundreds) of bitmaps, and constantly changes them as the game progresses. These bitmaps are created, not loaded from a resource... so they have no R.id to refer to.
I would like to animate some of them, e.g. a bitmap moving from one loction to another when a player taps to move it. What is the best way to do this?
Note, this is 2.1 and the bitmaps are drawn on a canvas via a matrix translate.
Make this function to get image from drawable folder
private Bitmap getImageBitmap(){
int resID = getResources().getIdentifier(uri, "drawable",
"a.b.c");
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), resID);
return mBitmap;
}
For set the image , where do you you want>
ImageView imageview01.setImageBitmap(getImageBitmap("name of bitmap");
if you want more about bitmap. then you may try this links.
http://developer.android.com/reference/android/widget/ImageView.html
drawable getResources().getIdentifier problem
setImageURI / setimagebitmap to fetch image from net and display in image view