How to resize / scaletype imageview on Android 8.0 Oreo? - imageview

I have code to scaletype imageview and succesfully tried on Android 4, 5, 6, 7.
But on Oreo, my imageview cant change
I tested on Asus Max Pro M1 Android 8.1
Example of my code below
iv1 = (ImageView) findViewById(R.id.imageView1);
iv1.setScaleType(ImageView.ScaleType.FIT_CENTER);
iv2 = (ImageView) findViewById(R.id.imageView2);
iv2.setScaleType(ImageView.ScaleType.CENTER_CROP);

I want to answer by myself hehe
My solution is change imageView height to equal with device height
If Zenfone max pro m1 display height is 2000 px, so I changed resolution of imageView height to 2000 px too
On my question, my imageView height is 1000 px, so there was nothing happen on Zenfon maxpro m1 when I used CENTER_CROP
Thanks ^_^

Related

Disabled fullscreen in qt program

I search to disable F11 keys or ctr+ F11 who put my program qt in fullscreen.
In Design mode change QWidget minimumSize and maximumSize parameters to equal value (bottom right corner of Qt creator).
For example: for minimumSize width = 400, height = 250; for maximumSize equal values width = 400, height = 250. You can see it on a screenshot:
example of values for necessary parameters

Resizing ImageViews

I have a GridPane (4x5), all it's cells have as child an AnchorPane which cointains an ImageView. I need to resize the image so it fully cover the cell as soon as the gridPane (and thus it's cells) change size.
I managed to resize the image correctly when the size of the cell grows, but when the cell gets tinier the image doesn't resize back.
This leads into partially covering images of the confinant cells.
Can anyone explain what i'm doing wrong or give me the instruction to implement a proper resize?
This is my code:
ImageView image = new ImageView("/dice/" + draftList.get(i) + ".png");
AnchorPane pane = ((AnchorPane)(gridpane.getChildren().get(i)));
pane.getChildren().add(image);
fitToParent(image,pane);
//method in the same class
private void fitToParent(ImageView image, AnchorPane pane) {
image.fitWidthProperty().bind(pane.widthProperty());
image.fitHeightProperty().bind(pane.heightProperty());
}
You can try to use the setPreserveRatio(boolean) function of the ImageView class to true. This will allow you to keep the aspect ratio constant.
Eg:
ImageView iv = new ImageView(/*file path*/);
iv.setPreserveRatio(true);
Src: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView.html
Other than this you can also try to limit the resizable property to false or set the min width and height so that the image is not partially covered
Src: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Region.html#resize-double-double-

How to get exact height and width of android screen with weight?

I need a square block of 20% of the screen,
If i get a width of 100 DP then i also need height of 100 DP.
How to achieve this in android with the use of weight?
You can do like this
public void setImageSize(byte[] imageThumbnail)
{
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Bitmap bitmapOrg = new BitmapDrawable(getResources(), new ByteArrayInputStream(imageThumbnail)).getBitmap();
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
float scaleWidth = metrics.scaledDensity/5;
//set image in imageView
imageView.setImageBitmap(bitmapOrg);
//set imageView dynamic width and height
imageView.getLayoutParam().width = scaleWidth;
imageView.getLayoutParam().height= scaleWidth;
}
Or
Maybe try a different approach...you can create SquareImageView to scaling of images to screen density of width and height.

JavaFX: maximum ImageView size

This may sound like a duplicate of Set maximum size for JavaFX ImageView but it's different.
I'd like to restrict the maximum size of an ImageView. Unfortunately, the only way to set the ImageView's size seems to be fitWidth and fitHeight which however enlarges the image if it's smaller than the fit values.
I tried setting fitWidth/fitHeight to 0/0 and wrap the ImageView into a pane with maxWidth set - no success (image is displayed in original size).
To me it seems as this is not achievable by JavaFX, however I can't believe it. But I couldn't find anything online. Are there any tricks/bugs/workarounds on this?
If you dont want the image to stretch, you can use:
setPreserveRatio(true);
Then you are able to fit width and height as you want
ImageView setPreserveRatio javadoc
In javadoc in this method there are the rules of scaling too
You just needed to add in if statement to set those larger than maxsize to a fixed size. The rest would remain their original size.
ImageView im = new ImageView();
im.setImage(image);
im.maxHeight(690 - 10);
int yoursize = 690;
if(im.maxHeight(690 - 10)> yoursize ){
im.setFitHeight(690- 10);
System.out.println("Fix Size : 690 ");
}
im.setPreserveRatio(true);
im.setSmooth(true);
im.setCache(true);
You can get the actual Image resource and query its width/height to use for the fit values:
Image image = imageView.getImage();
double nativeWidth = image.getWidth();
double nativeHeight = image.getHeight();

ImageView not resizing image

I want to use an ImageView to resize an image, however the image is not being resized:
ImageView imageView = new ImageView(image);
imageView.setPreserveRatio(true);
imageView.setFitHeight(40);
System.out.println("imageview image width = " + imageView.getImage().getWidth());
System.out.println("imageview image height = " + imageView.getImage().getHeight());
The output is
imageview image width = 674.0
imageview image height = 888.0
However, the width should be 40. My ImageView is not attached to any scene and I also don't want to attach it, it shall only be used for image resizing. Is there any way to force the ImageView to resize its image, even though the ImageView is not attached to any scene? The reason I am using an ImageView for resizing is, that I want to resize an Image in RAM, without reading it again from the disk, please see this question for more details.
Thanks for any hint!
Using an ImageView for resizing seems to be very hacky.
A better approach is to convert your Image into a BufferedImage and do the resizing the old way. (JavaFx does not (yet) provide an internal way to resize memory images)
int width = 500; // desired size
int height = 400;
Image original = ...; // fx image
BufferedImage img = new BufferedImage(
(int)original.getWidth(),
(int)original.getHeight(),
BufferedImage.TYPE_INT_ARGB);
SwingFXUtils.fromFXImage(original, img);
BufferedImage rescaled = Scalr.rescaleImage(img, width, heigth); // the actual rescale
// convert back to FX image
WritableImage rescaledFX = new WritableImage(width, heigth);
SwingFXUtils.toFXImage(rescaled, rescaledFX);
Where as Scalr is a nice library for resizing images in native java. Obviously, you can use other/simpler methods of rescaling, but the image quality won't be that nice.

Resources