I have a custom view in an Android project and in that custom view, I want to draw a bitmap that's about 1/3th the size of the view element.
I use the following code
Bitmap bigIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
Bitmap icon = Bitmap.createScaledBitmap(bigIcon, newWidth, newHeight, false);
width newWidth and newHeight being 1/3th of the view's width and height.
The problem is now that if I want to get the view's width or height, I'd have to get it in the onMeasure or onDraw method. And I'd really prefer not to scale it every single frame (the bigIcon is loaded only once in the constructor). The createScaledBitmap needs also to be called only once but the constructor is too soon since the View is not yet created.
So the question: how can I access the view's width and height (knowing that the view's size doesn't change)?
I think you can accomplish what you want by overriding your custom View's onSizeChanged method.
Here, you can calculate the dimensions of your bitmap based on the View's dimensions only when the size has actually changed.
Related
I'm trying to create a scaled bitmap which fits in the Custom View but whenever I perform the operation the width gets cropped while the height of the bitmap fits into the custom view.
bitmapq = Bitmap.createScaledBitmap(bitmap,(custHeight*bitmap.getWidth())/bitmap.getHeight(),custHeight, true);
Custom View height - 1089.
Custom View width- 1180.
Width of bitmap - 2368.
Height of bitmap - 4208
If you'd like the scaled bitmap to be of the exact size of your custom view regardless of its aspect ratio, you can use this:
bitmapq = Bitmap.createScaledBitmap(bitmap, cusWidth, custHeight, true);
In this way, the scaled bitmap exacly fits your custom view's boundaries.
Im trying to make a custom view with some drawings in it. I want these drawings to take up the entire view but I also want the view to maintain a certain height/width ratio for these drawings.
In your onDraw(canvas) method, use canvas.getWidth() and canvas.getHeight() get to get the entire width and height of the View. Use this detail (like calculating the center of the View (width/2,height/2)) to draw your drawings. This way they will always fill the entire view and maintain the right aspect ratio.
I have an activity wich contains a ListView, the list is a thumbnail on the left and some text on the right, pretty much like a contact list. The thing is, the thumbnail is loading from an absolute path from the sdcard, and each image size is about 5mb. For avoiding running out of memory I created the classes in this tutorial which loads only a scaled version of the images and doesn't take a lot of memory.
Well, what's the problem? Since, I have to pass the width and the height (in px) that I want the image to be resized, but for differente devices I would have a problem setting absolute values. So, I just tried passing the view through args, and just manipulate it in my class to get the width and height, nice eh? Ok, but it didn't work, since the thumbnail hasn't been draw yeat (i guesss), I can't take his size values.
How can I get the size inside the method getView() from my lisview adapter?
/** short passage of getView() method */
ImageView image = (ImageView) view.findViewById(R.id.image_thumb);
Bitmap bMap = BitmapAdjusted.decodeSampleBitmap("/sdcardpath/",
image); // "image" is the view passed by args
image.setImageBitmap(bMap);
Here's the problem:
public static decodeSimpleBitmap(String path, View view){
/** unworth previous code */
int width = view.getWidth(); // always give zero, getMeasuredWidth() too
int height = view.getHeight(); // always give zero
// options is BitmapFactory.Options class, declared before that do the magic
options.inSampleSize = calculateInSampleSize(options, width, height);
}
Thanks!
I would like to obtain ImageView dimensions during bindView method.
Unfortunately during bindView measured height and width are 0.
I need those dimensions to request specific image width and height from web.
Before your AdapterView starts recycling views, the views would have not been layered-out on the screen yet, so they will be width and height of zero, because it really is zero.
After your bindView starts to receive recycled views, they will have width and height, but then it's too late.
On those situations you must find a different way to calculate their size, for example, you can make it be a fixed value in DP for every view and just use that value. Example:
int size = context.getResources().getDimensionPixelSize(R.dimen.img_size);
My question is similar to this question. I've used this code to extend ImageView to form TouchImageView. In my onCreate() method, I want to call
setImage(Bitmap bm, int displayWidth, int displayHeight)
but I don't know the width or height to use. When I call getHeight(), it gives me zero.
I tried to avoid the 0 using kcoppock's answer to the previously mentioned question, but was unsuccessful. The onPreDraw() listener is called several times per second and I can't figure out how to only call it once.
That's a brittle approach for something like this. Android Views are measured by their parent View using the onMeasure method. By providing an initial size to a setImage method to scale to (and especially if you set it based on the display size) you're inviting complexity whenever you want to nest this View within others. Larger screen devices like tablets are a good example of this.
A more idiomatic approach would implement the setImage method of TouchImageView without the width/height parameters at all. Instead it would defer setting the scale and matrices until measurement and implement onMeasure something like this:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mNeedsInitialScale) { // Set by setImage when a new image is set
// The measured width and height were set by super.onMeasure above
setInitialScale(getMeasuredWidth(), getMeasuredHeight());
mNeedsInitialScale = false;
}
}
Where the setInitialScale method does everything that the linked setImage method does after the call to super.setImageBitmap. setImage should also set the mNeedsInitialScale field to true.
This way you will never need to worry about the initial size to use, it will be obtained automatically from the View during normal measurement whenever a new image is set.
the ImageViews dimensions are 0 until your Activity draw it on the screen. You could use Activitys onFocusChanged to set the Bitmap and get correct Dimensions
if you are just looking for DisplayWidth and height you can call
Display d = getSystemService(WINDOW_SERVICE).getDefaultDisplay();
and ask Display for its dimensions