-1

I want to Aspect Fill my image. The size of my image is 1242x168. What I want is that if my screen is small my image will be cut and left align but it seems that it keeps on being aspect fit.

Here is my code

DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int device_width = displaymetrics.widthPixels;

float density  = getResources().getDisplayMetrics().density;
            float dpWidth  = displaymetrics.widthPixels / density;

if(dpWidth >= 768) {
                int aspectRatio = (int) (0.135 * device_width);
                gifImageView.getLayoutParams().height = aspectRatio;
                gifImageView.getLayoutParams().width = device_width;
            }

Here is my xml

<pl.droidsonroids.gif.GifImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/displayImage"
    android:layout_gravity="left"
    android:scaleType="fitStart"
    android:background="@color/red"
    android:adjustViewBounds="true"
    />

I tried using android:scaleType="centerCrop" but it is crop in center I wanted to cut the right side that is why I wanted to be left align.

natsumiyu
  • 3,217
  • 7
  • 30
  • 54

2 Answers2

0

Try Scaling your image based on screen width:

public static float getScreenWidth(Activity activity) {
    Display display = activity.getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);
    float pxWidth = outMetrics.widthPixels;
    return pxWidth;
}

float screenWidth=getScreenWidth(act)
float newHeight = screenWidth;
if (bitmap.getWidth() != 0 && bitmap.getHeight() != 0) {
 newHeight = (screenWidth * bitmap.getHeight()) / bitmap.getWidth();
}

Bitmap scaledBitmap=Bitmap.createScaledBitmap(bitmap, (int) screenWidth, (int) newHeight, true);

android:adjustViewBounds=true.
android:scaleType="centerCrop"

Links: Resizing ImageView to fit to aspect ratio, https://gist.github.com/JakeWharton/2856179

Community
  • 1
  • 1
YLS
  • 1,475
  • 2
  • 15
  • 35
0

I tried doing this

//Get width of device
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

    float density  = getResources().getDisplayMetrics().density;
    float dpWidth  = displaymetrics.widthPixels / density;

   if(dpWidth > 768 ) { 
      int aspectRatio = (int) (0.135 * device_width);
      gifImageView.getLayoutParams().height = aspectRatio;
      gifImageView.getLayoutParams().width = device_width;
      gifImageView.setIsScale(false);
    }else{
        int aspectRatio = (int) (0.219 * device_width);
        gifImageView.getLayoutParams().height = aspectRatio;
        gifImageView.getLayoutParams().width = device_width;
     gifImageView.setScaleType(ProportionalImageView.ScaleType.MATRIX);
        gifImageView.setIsScale(true);
    }

And create a cuztomized class that extends to ImageView / GifImageView

 public class ProportionalImageView extends GifImageView {

Boolean isScale = false;

public ProportionalImageView(Context context) {
    super(context);
    if(isScale) {
        setup();
    }
}

public ProportionalImageView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    if(isScale) {
        setup();
    }
}

public ProportionalImageView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    if(isScale) {
        setup();
    }
}

private void setup() {
    setScaleType(ScaleType.MATRIX);
}

@Override
protected boolean setFrame(int l, int t, int r, int b)
{
    if(isScale) {
        computeMatrix();
    }

    return super.setFrame(l, t, r, b);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    if(isScale) {
        computeMatrix();
    }
}

private void computeMatrix() {
    Matrix matrix = getImageMatrix();
    float scaleFactor, scaleFactorWidth, scaleFactorHeight;
    scaleFactorWidth = (float) getWidth() / (float) getDrawable().getIntrinsicWidth();
    scaleFactorHeight = (float) getHeight() / (float) getDrawable().getIntrinsicHeight();

    if (scaleFactorHeight > scaleFactorWidth) {
        scaleFactor = scaleFactorHeight;
    } else {
        scaleFactor = scaleFactorWidth;
    }

    matrix.setScale(scaleFactor, scaleFactor, 0, 0);
    setImageMatrix(matrix);
}

public void setIsScale(Boolean isScale){
    this.isScale = isScale;
}

public Boolean getIsScale() {
    return isScale;
}
}
natsumiyu
  • 3,217
  • 7
  • 30
  • 54