0

I am developing an application in which I need to fit an Bitmap into Imageview with specific dimensions(let's suppose 350dpx50dp - height*width).

I wanted to do something similar like this: http://gyazo.com/d739d03684e46411feb58d66acea1002

I have looked here for solutions. I found this code for scale the bitmap and fit it into imageview, but the problem is that imageview becomes greater when I add the bitmap into him:

private void scaleImage(Bitmap bitmap, ImageView view)
{
    // Get current dimensions AND the desired bounding box
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int bounding = dpToPx(350);

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) bounding) / width;
    float yScale = ((float) bounding) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Create a new bitmap and convert it to a format understood by the ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

    // Apply the scaled bitmap
    view.setImageBitmap(scaledBitmap);
}

Using this code I can get this : http://gyazo.com/e9871db2130ac33668156fc0cf773594

But that's not what I wanted, I want to keep the dimensions of imageview and add the bitmap into imageview without modifying the dimensions of imageview and occupying all the imageview's surface. Like the first image.

tmt
  • 7,611
  • 4
  • 32
  • 46

1 Answers1

1

Why don't you just add android:scaleType="fitXY" to your ImageView in xml?

Lamorak
  • 10,957
  • 9
  • 43
  • 57
  • I tried it. I get the same result: http://gyazo.com/e9871db2130ac33668156fc0cf773594 – Mohamed Said Benmousa Mar 25 '15 at 17:27
  • Then definitely more info is needed to help – Lamorak Mar 25 '15 at 18:29
  • What kind of information do you need? I could not publish images neither more links due to my reputation xD – Mohamed Said Benmousa Mar 25 '15 at 18:37
  • I will show you the xml of the ImageView: http://gyazo.com/c34327780b9115c2c3eafe378cb8c4d1 I have 6 imageviews: http://gyazo.com/8a53975b4bf9746bb28eb090e5cd61a3 All of them have the same xml. When I execute a simple code like that(example of one ImageView): http://gyazo.com/320947b4e2d81ddf72541a8e1f105740 I get this: http://gyazo.com/b40272839f8a095c61289fdfbbdff1a4 – Mohamed Said Benmousa Mar 25 '15 at 18:50
  • Well definitely the problem is not in scaling the image. I don't think you have to do it manually, the `scaleType` does the same thing. Most probably the problem is in the the layout, you set `ImageView` to wrap and weight it. – Lamorak Mar 25 '15 at 20:57
  • Well, here is the whole code in xml: http://pastebin.com/KvtKDDzL And the design: http://gyazo.com/bf8a863df2e9dd516017880be394193f – Mohamed Said Benmousa Mar 25 '15 at 21:33
  • how is your `style="?android:attr/borderlessButtonStyle"` defined? Most probably there is some padding defined either in the style or in the 9 patch background. – Lamorak Mar 25 '15 at 22:30
  • From what I've found on the Internet, seems to have padding. Do you know any way to do what I intend to do? Thanks anyway. – Mohamed Said Benmousa Mar 26 '15 at 17:10
  • Just provide style without padding :) – Lamorak Mar 26 '15 at 17:11
  • I'm quite newbie in android. How can I define style? – Mohamed Said Benmousa Mar 26 '15 at 17:13
  • You should read the [docs](https://developer.android.com/guide/topics/ui/themes.html). Since you probably want to just have frame around your image, remove the `style` parameter from your `ImageView` and add this `android:background="@android:drawable/picture_frame"` This is very simple frame but looks good – Lamorak Mar 26 '15 at 17:17
  • You are right, it seems a better way. But I have executed the code and the same thing keeps happening: http://gyazo.com/50559bb307995869ac431e652629e31e. XML of ImageView: http://gyazo.com/159c573a003946038085dcbd729fdb06 – Mohamed Said Benmousa Mar 26 '15 at 17:25
  • If I understand correctly you only want to get rid of the bit padding between the image and the shadow? – Lamorak Mar 26 '15 at 17:33
  • Not exactly. I have this Imageview with specific dimension: http://gyazo.com/eb4c7c370de24a7f6c93000c10a281b2 And I have an image, I want to fit the image into the specific dimension of ImageView, without resizing the ImageView. – Mohamed Said Benmousa Mar 26 '15 at 17:37
  • so you want to stretch the image? Try `android:scaleType="fitXY"`. Or you want to crop it to fill the image with `android:scaleType="cropCenter"`? – Lamorak Mar 26 '15 at 17:56
  • I would scale the image to fit the dimensions of the ImageView. I will try what you said. – Mohamed Said Benmousa Mar 26 '15 at 18:02
  • With 'android:scaleType="fitXY"' looks great! http://gyazo.com/7deb2c326eb6eab9c4e71af927cd08fb Do you know how I could get rid of the whites of the imageview? – Mohamed Said Benmousa Mar 26 '15 at 18:06
  • The background is created by 9 patch image, you should be able to edit it or fing another one – Lamorak Mar 26 '15 at 18:18
  • I'm supposed to let the user choose an image. So if he has one similar like this one, how should I proceed to avoid that white background? Thank you, anyway! – Mohamed Said Benmousa Mar 26 '15 at 18:26
  • check [this answer](http://stackoverflow.com/questions/3693234/custom-imageview-with-drop-shadow) – Lamorak Mar 26 '15 at 18:28