概述
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.view.ViewTreeObserver;
import com.tancheng.laikanxing.util.MethodUtils;
/**
* 根据显示图片的宽高,控件的宽高,决定显示图片的某一个部分,比如, 竖长图上半部分,横长图左半部分
*
* @author Chris Arriola
*/
public class MatrixImageViewextends ImageView {
public TopCropImageView(Contextcontext) {
super(context);
}
public TopCropImageView(Contextcontext, AttributeSetattrs) {
super(context,attrs);
}
public TopCropImageView(Contextcontext, AttributeSetattrs, int defStyle) {
super(context,attrs,defStyle);
}
@Override
protectedvoid onLayout(booleanchanged,int left,inttop,intright,
int bottom) {
super.onLayout(changed,left,top, right,bottom);
}
@Override
public void setImageMatrix(Matrix matrix) {
super.setImageMatrix(matrix);
changeMatirx(null);
}
//
@Override
protectedboolean setFrame(intl,intt,intr,intb) {
boolean result = super.setFrame(l,t,r, b);
changeMatirx(null);
returnresult;//super.setFrame(l, t, r, b);
}
@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
changeMatirx(drawable);
}
@Override
public void setImageResource(int resId) {
// TODO Auto-generated method stub
super.setImageResource(resId);
changeMatirx(getDrawable());
}
@Override
public void setImageURI(Uri uri) {
// TODO Auto-generated method stub
super.setImageURI(uri);
changeMatirx(getDrawable());
}
//
privateintdrawableWidth;
privateintdrawableHeight;
private void changeMatirx(Drawable drawable) {
if (drawable ==null) {
drawable = getDrawable();
if (drawable ==null) {
return;
}
}
drawableWidth =drawable.getIntrinsicWidth();
drawableHeight =drawable.getIntrinsicHeight();
if (drawableWidth <= 0 ||drawableHeight <= 0) {
return;
}
if (drawableHeight >drawableWidth) {
setScaleType(ScaleType.MATRIX);
} else {
setScaleType(ScaleType.CENTER_CROP);
}
}
@Override
public void setScaleType(ScaleType scaleType) {
changeMatrix(scaleType);
super.setScaleType(scaleType);
}
@Override
protectedvoid onDraw(Canvascanvas) {
super.onDraw(canvas);
}
private void changeMatrix(ScaleType scaleType) {
if (scaleType == ScaleType.MATRIX) {
final Matrixmatrix = getImageMatrix();
final int viewWidth = getWidth() - getPaddingLeft()
- getPaddingRight();
final int viewHeight = getHeight() - getPaddingTop()
- getPaddingBottom();
if (viewHeight <= 0 ||viewWidth <= 0) {
return;
}
float scale = 0f;
if (drawableWidth *viewHeight >viewWidth *drawableHeight) {
scale = (float)viewHeight / (float)drawableHeight;
} else {
scale = (float)viewWidth / (float)drawableWidth;
}
scale = (float)viewWidth / (float)drawableWidth;
matrix.setScale(scale,scale);
super.setImageMatrix(matrix);
}
}
}
最后
以上就是痴情老鼠为你收集整理的android 自定义ImageView -图片显示相关1的全部内容,希望文章能够帮你解决android 自定义ImageView -图片显示相关1所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复