mori mori - 1 month ago 21
Android Question

How to aspect fill programmatically? [Android]

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.

Answer

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;
}
}