1

I know it's bit tricky and still thinking whether is it possible or not, but i want to make my image to adjust without decreasing in image quality on any android device when i used vector Drawable it was pretty convenient but sometimes size of vectors are not memory efficient so i don't want to use them.Though i want to know if there is any way to adjust simple PNG or JPEG files irrespective of resolution and screen size in Android?

If anyone can give me way,it would be great help !!

TapanHP
  • 5,969
  • 6
  • 37
  • 66
  • I am wondering if you can use scaleType provided as attribute to ImageView to get your job done. There are various scaleType available like center, centerCrop, centerInside, fitCenter, fitEnd, fitStart, fitXY, matrix. – Sagar Trehan Jul 06 '16 at 05:30
  • suggestion to use of PNG files, check http://stackoverflow.com/a/37207973/2826147 – Amit Vaghela Jul 06 '16 at 05:38
  • ok i already tried it but by making this folders and putting different files is not a solution really @AmitVaghela – TapanHP Jul 06 '16 at 06:44
  • that will solve your this issue – Amit Vaghela Jul 06 '16 at 06:46

1 Answers1

1

Use Resize Image View (Custom Image View)

  public class ResizableImageView extends ImageView {
    public ResizableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ResizableImageView(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable d = getDrawable();
        // get drawable from imageview
        if (d == null) {
            super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
            return;
        }

        int imageHeight = d.getIntrinsicHeight();
        int imageWidth = d.getIntrinsicWidth();
        // get height and width of the drawable
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        // get width and height extracts the size from the supplied measure specification.
        float imageRatio = 0.0F;
        if (imageHeight > 0) {
            imageRatio = imageWidth / imageHeight;
        }
        float sizeRatio = 0.0F;
        if (heightSize > 0) {
            sizeRatio = widthSize / heightSize;
        }

        int width;
        int height;
        if (imageRatio >= sizeRatio) {
            // set width to maximum allowed
            width = widthSize;
            // scale height
            height = width * imageHeight / imageWidth;
        } else {
            // set height to maximum allowed
            height = heightSize;
            // scale width
            width = height * imageWidth / imageHeight;
        }

        setMeasuredDimension(width, height);
        // This method must be called to store the measured width and measured height. Failing to do so will trigger an exception at measurement time
    }
}
Evgeniy Mishustin
  • 3,343
  • 3
  • 42
  • 81
Chirag Arora
  • 816
  • 8
  • 20
  • Are you sure that it will work on all density for all devices and resolutions ? and can you make it more understandable by putting comments in it ? please it would be great help @ChiragArora – TapanHP Jul 06 '16 at 06:46
  • Please check, If you want to understand more then please check below link http://www.ryadel.com/en/android-proportionally-stretch-imageview-fit-whole-screen-width-maintaining-aspect-ratio/ – Chirag Arora Jul 06 '16 at 07:27
  • Ok yeah very helpful @Chirag – TapanHP Jul 06 '16 at 07:33