Bu çözümlerin hiçbiri benim için işe yaramadı, çünkü yatay veya dikey yönde rastgele bir mahsulü destekleyen bir sınıf istedim ve mahsulü dinamik olarak değiştirmeme izin vermesini istedim. Ayrıca Picasso uyumluluğuna da ihtiyacım vardı ve Picasso, resim çekmecelerini tembel bir şekilde ayarladı.
Benim uygulamam doğrudan AOSP'deki ImageView.java'dan uyarlandı . Kullanmak için XML'de böyle beyan edin:
<com.yourapp.PercentageCropImageView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"/>
En iyi mahsulün olmasını istiyorsanız kaynaktan arayın:
imageView.setCropYCenterOffsetPct(0f);
Alt kısımda bir ürün istiyorsanız, arayın:
imageView.setCropYCenterOffsetPct(1.0f);
Yolun 1 / 3'ünde bir mahsul almak istiyorsanız, arayın:
imageView.setCropYCenterOffsetPct(0.33f);
Ayrıca, fit_center gibi başka bir kırpma yöntemi kullanmayı seçerseniz, bunu yapabilirsiniz ve bu özel mantığın hiçbiri tetiklenmeyecektir. (Diğer uygulamalar YALNIZCA kırpma yöntemlerini kullanmanıza izin verir).
Son olarak, bir yöntem (redraw ()) ekledim, böylece kırpma yönteminizi / scaleType'ı kodda dinamik olarak değiştirmeyi seçerseniz, görünümü yeniden çizmeye zorlayabilirsiniz. Örneğin:
fullsizeImageView.setScaleType(ScaleType.FIT_CENTER);
fullsizeImageView.redraw();
Özel üst-orta-üçüncü ekininize geri dönmek için şu numarayı arayın:
fullsizeImageView.setScaleType(ScaleType.MATRIX);
fullsizeImageView.redraw();
İşte sınıf:
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class PercentageCropImageView extends ImageView{
private Float mCropYCenterOffsetPct;
private Float mCropXCenterOffsetPct;
public PercentageCropImageView(Context context) {
super(context);
}
public PercentageCropImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PercentageCropImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public float getCropYCenterOffsetPct() {
return mCropYCenterOffsetPct;
}
public void setCropYCenterOffsetPct(float cropYCenterOffsetPct) {
if (cropYCenterOffsetPct > 1.0) {
throw new IllegalArgumentException("Value too large: Must be <= 1.0");
}
this.mCropYCenterOffsetPct = cropYCenterOffsetPct;
}
public float getCropXCenterOffsetPct() {
return mCropXCenterOffsetPct;
}
public void setCropXCenterOffsetPct(float cropXCenterOffsetPct) {
if (cropXCenterOffsetPct > 1.0) {
throw new IllegalArgumentException("Value too large: Must be <= 1.0");
}
this.mCropXCenterOffsetPct = cropXCenterOffsetPct;
}
private void myConfigureBounds() {
if (this.getScaleType() == ScaleType.MATRIX) {
Drawable d = this.getDrawable();
if (d != null) {
int dwidth = d.getIntrinsicWidth();
int dheight = d.getIntrinsicHeight();
Matrix m = new Matrix();
int vwidth = getWidth() - this.getPaddingLeft() - this.getPaddingRight();
int vheight = getHeight() - this.getPaddingTop() - this.getPaddingBottom();
float scale;
float dx = 0, dy = 0;
if (dwidth * vheight > vwidth * dheight) {
float cropXCenterOffsetPct = mCropXCenterOffsetPct != null ?
mCropXCenterOffsetPct.floatValue() : 0.5f;
scale = (float) vheight / (float) dheight;
dx = (vwidth - dwidth * scale) * cropXCenterOffsetPct;
} else {
float cropYCenterOffsetPct = mCropYCenterOffsetPct != null ?
mCropYCenterOffsetPct.floatValue() : 0f;
scale = (float) vwidth / (float) dwidth;
dy = (vheight - dheight * scale) * cropYCenterOffsetPct;
}
m.setScale(scale, scale);
m.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
this.setImageMatrix(m);
}
}
}
@Override
protected boolean setFrame(int l, int t, int r, int b) {
boolean changed = super.setFrame(l, t, r, b);
this.myConfigureBounds();
return changed;
}
@Override
public void setImageDrawable(Drawable d) {
super.setImageDrawable(d);
this.myConfigureBounds();
}
@Override
public void setImageResource(int resId) {
super.setImageResource(resId);
this.myConfigureBounds();
}
public void redraw() {
Drawable d = this.getDrawable();
if (d != null) {
this.setImageDrawable(null);
this.setImageDrawable(d);
}
}
}