0

In my project, user is choosing an image from gallery which is then set as background of my custom View. And I need the size of that View to be adjusted to the size of the image.

In current version, the size of my custom View is static. But I need it to adjust to the size of the chosen image. And also, I don't know how to set that image as background without it being stretched.

XML file of fragment

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="9">

    <com.example.kreslenie.mapakreslenie.CanvasMap
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/mapping_canvas" />

    ...


</RelativeLayout>

Fragment method which loads the image and then sets it as background

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    try {

        if (requestCode == RESULT_LOAD_IMG && resultCode == -1 && null != data) {


            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };


            Cursor cursor = activity.getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);

            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String imgDecodableString = cursor.getString(columnIndex);
            cursor.close();


            Bitmap bitmap = Bitmap.createBitmap(BitmapFactory.decodeFile(imgDecodableString));
            Drawable image = new BitmapDrawable(activity.getResources(), bitmap);
            canvas.setBackground(image);


        } else {
            Toast.makeText(activity, "You haven't picked any Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(activity, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }


}
  • Don't set it as the background, set it as the source (`src`). Something like `imageView.setImageDrawable(drawable)` or `imageView.setImageBitmap(bitmap)`. Lastly, set your `layout parameters to wrap_content` or, if you want to keep the static size, use `scaleType` to manage how it is scaled. – zgc7009 Mar 18 '16 at 20:27
  • Actually I need to set that image to my custom view. I have to edit my question. –  Mar 18 '16 at 20:40
  • Just let your custom view extend ImageView, unless there is a reason against that. – zgc7009 Mar 18 '16 at 20:49
  • Thanks, that's the solution I've been looking for! –  Mar 18 '16 at 21:27

2 Answers2

0

Change your ImageView to -

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:id="@+id/compass_image"
        android:contentDescription="@string/compass_description"
        android:src="@drawable/compass"
        android:visibility="gone"
        android:layout_alignTop="@+id/mapping_canvas"
        android:layout_alignParentEnd="true" />

And while setting the image use

setImageResource() or setImageDrawbale() or setImageBitmap() instead of setBackground()

Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
  • Actually I need to set that image to my custom view. I have to edit my question. –  Mar 18 '16 at 20:40
0

I am not sure why you are using a custom view. If you are using a customview only for this image, then change it to normal android's Imageview and add this below code. Here is what I did when I have to change the imageview to fit the screen.

Whenever I use a bitmapdrawable and resize it at runtime, it did not fill the imageview in all the devices in android. The only way is to create a new layout with a new Imageview and display it.

public ImageView imageView;

//inside onCreate()
imageView = (ImageView) findViewById(R.id.yourimageviewid);

//Where you want to create the image
Drawable bitmapDrawable = new BitmapDrawable(bitmap);            

After this, add this, 

float factor = CurrentDeviceWindowWidth/(float) bitmap.getWidth();

// we are creating a new layout with width and height approximate ratio to the bitmap width and height, and add the image to it..If you want to adjust the ratio, then you can adjust the ratio formula.//

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(bitmap.getWidth(), (int) bitmap.getHeight() * factor);

imageview.setLayoutParams(layoutParams);
imageView.setAdjustViewBounds(true);
imageView.setImageDrawable(bitmapDrawable);

Try this, and let me know.

Suv
  • 121
  • 1
  • 3
  • The reason why I'm using custom view is that I need to draw onto it later. So this wouldn't help me. But thanks for answer. –  Mar 18 '16 at 21:43
  • So, you want to set one image, whatever the user clicks, as the background image for that custom view, and you are going to set another image on top of that custom view.....?... – Suv Mar 19 '16 at 01:13
  • Kind of. User chooses image from gallery, which is a map where he will be drawing points based on his location. And I need that custom view to adjust to the max. possible size of the image. –  Mar 19 '16 at 15:31