Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How set imageview scaletype to topCrop

Tags:

I'm coding android and I have a imageview. I want to set scaletype of this to topcrop. I could find centercrop in options, but it's not my request. How do I do?

like image 888
Javad Abedi Avatar asked Apr 21 '15 21:04

Javad Abedi


People also ask

How can I bring ImageView in front?

Just one single method will let you to bring your view to the front and the magic words are : myView. bringToFront(); “.


1 Answers

Custom Android ImageView for top-crop scaling of the contained drawable.

import android.content.Context;
import android.graphics.Matrix;
import android.widget.ImageView;

/**
* ImageView to display top-crop scale of an image view.
*
* @author Chris Arriola
*/
public class TopCropImageView extends ImageView {

public TopCropImageView(Context context) {
    super(context);
    setScaleType(ScaleType.MATRIX);
}

@Override
protected boolean setFrame(int l, int t, int r, int b) {
    final Matrix matrix = getImageMatrix();

    float scale;
    final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
    final int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
    final int drawableWidth = getDrawable().getIntrinsicWidth();
    final int drawableHeight = getDrawable().getIntrinsicHeight();

    if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
        scale = (float) viewHeight / (float) drawableHeight;
    } else {
        scale = (float) viewWidth / (float) drawableWidth;
    }

    matrix.setScale(scale, scale);
    setImageMatrix(matrix);

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

https://gist.github.com/arriolac/3843346

like image 92
Jay Avatar answered Sep 29 '22 11:09

Jay