3

I'm working on an application that has several application widgets, and some of them are using a GridView to display their content. The content is a bunch of movie cover art images, and I'd like to retain the original aspect ratio of the images.

Alternatively, I'm looking for a way to declare an aspect ratio for the images. It's important that the images still fit the width of the GridView when the user changes the size of the app widget, however.

Since it's not possible to use sub-classes of ImageView when dealing with RemoteViews, that's out of the question. I'm currently using ScaleType.CENTER_CROP on the ImageView as well as a defined height and width, but as you can imagine, that's not perfect.

Any suggestions?

Michell Bak
  • 13,182
  • 11
  • 64
  • 121
  • 1
    Hi, here is a function I wrote to scale an image to fit the width of the screen but also have the correct height. It includes a reference to the original article for the calculation. It may give you some inspiration for your problem. http://pastebin.com/suM5RSMz – jim Jan 27 '13 at 00:04
  • Sorry, but that doesn't really help. It's pretty easy to scale everything in code, but I don't think it's possible to do so when dealing with RemoteViews. – Michell Bak Jan 27 '13 at 00:13
  • Oh ok, sorry about that. – jim Jan 27 '13 at 00:23
  • Useful in some cases conor. Thanks. – Siddharth Feb 05 '13 at 04:13

5 Answers5

5

Did you try this method on your ImageView?

    imageView.setAdjustViewBounds(true);

I found it thanks to this link .

JJ86
  • 5,055
  • 2
  • 35
  • 64
1

Try to set GridView columnWidth as image width, numColumns as "auto_fit" and stretchMode as "spacingWidth":

<GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="50dp"
        android:numColumns="auto_fit"
        android:stretchMode="spacingWidth" />
Nik
  • 7,114
  • 8
  • 51
  • 75
  • Sorry, that doesn't provide what I'm looking for. Besides, you really ought to include the XML for the GridView item (ImageView) as well. – Michell Bak Jan 30 '13 at 16:10
1

Create a subclass of remote view where on setting bitmap use something like:

https://stackoverflow.com/a/2999707/944070

or override of onMeasure in that situation:

https://stackoverflow.com/a/4688335/944070

Community
  • 1
  • 1
madlymad
  • 6,367
  • 6
  • 37
  • 68
1

A ImageView is a bit different than a TextView. ImageView has "src" which defines the foreground image in the widget. If you set background in the imageview, it shows up behind the foreground. Both these images are controlled differently with android:stretchMode and other android parameters.

I suggest that you try to change all your android:src to android:background AND the other way around to get a pattern of what you think will work for you. After you kinda get a few images the way you want to see them, then manipulate the images using GIMP, Photoshop to size them same as the one you think you like. The images itself need to be good so that these parameters work well with it. Size, Density and X-Y ratio's of the image matter.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
0

See the RelativeLayout+ShapeDrawable technique used for another appwidget aspect ratio question here: How can I fill an ImageView in an Appwidget while mainiting the aspect ratio?

It works within the AppWidget constraints and should be applicable to this problem.

Community
  • 1
  • 1
Jim Vitek
  • 4,174
  • 3
  • 23
  • 32