Jpoliachik's risposta era abbastanza fresco per fare venire voglia di generalizzare per supportare sia superiore/inferiore e sinistra/destra, da un importo variabile. :) A questo punto è sufficiente chiamare il numero setOffset(0,0)
, il raccolto inferiore setOffset(0,1)
, il ritaglio a sinistra è anche setOffset(0,0)
e il ritaglio destro setOffset(1,0)
. Se vuoi spostare la vista di una frazione dell'immagine in una dimensione, puoi chiamare ad es. setOffset(0, 0.25f)
per spostarlo verso il basso del 25% dello spazio non visibile, mentre 0.5f lo centrerebbe. Saluti!
public class CropImageView extends ImageView {
float mWidthPercent = 0, mHeightPercent = 0;
public CropImageView(Context context) {
super(context);
setup();
}
public CropImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setup();
}
public CropImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setup();
}
// How far right or down should we place the upper-left corner of the cropbox? [0, 1]
public void setOffset(float widthPercent, float heightPercent) {
mWidthPercent = widthPercent;
mHeightPercent = heightPercent;
}
private void setup() {
setScaleType(ScaleType.MATRIX);
}
@Override
protected boolean setFrame(int l, int t, int r, int b) {
Matrix matrix = getImageMatrix();
float scale;
int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
int drawableWidth = 0, drawableHeight = 0;
// Allow for setting the drawable later in code by guarding ourselves here.
if (getDrawable() != null) {
drawableWidth = getDrawable().getIntrinsicWidth();
drawableHeight = getDrawable().getIntrinsicHeight();
}
// Get the scale.
if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
// Drawable is flatter than view. Scale it to fill the view height.
// A Top/Bottom crop here should be identical in this case.
scale = (float) viewHeight/(float) drawableHeight;
} else {
// Drawable is taller than view. Scale it to fill the view width.
// Left/Right crop here should be identical in this case.
scale = (float) viewWidth/(float) drawableWidth;
}
float viewToDrawableWidth = viewWidth/scale;
float viewToDrawableHeight = viewHeight/scale;
float xOffset = mWidthPercent * (drawableWidth - viewToDrawableWidth);
float yOffset = mHeightPercent * (drawableHeight - viewToDrawableHeight);
// Define the rect from which to take the image portion.
RectF drawableRect = new RectF(xOffset, yOffset, xOffset + viewToDrawableWidth,
yOffset + viewToDrawableHeight);
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.FILL);
setImageMatrix(matrix);
return super.setFrame(l, t, r, b);
}
}
Come convertirlo in TopCropImage? – Mani
@Mani Ho finito per fare entrambe le cose, ho pensato che sarebbe stato utile! [Link Github] (https://github.com/Jpoliachik/BottomTopCropImage/blob/master/example/src/com/justinpoliachik/bottomtopcropimage/TopCropImage.java) – Jpoliachik
L'ho fatto da solo, perché devo completarlo ieri stesso , grazie comunque, ho fatto lo stesso come hai fatto – Mani