ImageView 缩放 等比例裁剪显示
需求控件加载图片,先进行宽度等比例缩放 然后进行超出部分裁剪
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.os.Build;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.View;
import com.swkj.share_earn.R;
import static java.lang.Integer.SIZE;
public class TailorImageView extends AppCompatImageView {
float width, height;
private int resourceId;
private Bitmap mBitmapOuter;
private float mWidth;
private int mHeight;
private Paint paint;
private Point center;
private Point bmpCenter;
private RectF oval;
public TailorImageView(Context context) {
this(context, null);
init(context, null);
}
public TailorImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init(context, attrs);
}
public TailorImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TailorImageView, defStyleAttr, R.style.AppTheme);
//拿到resourceid属性
resourceId = typedArray.getResourceId(R.styleable.TailorImageView_resourceid, SIZE);
init(context, attrs);
typedArray.recycle();
}
private void init(Context context, AttributeSet attrs) {
if (Build.VERSION.SDK_INT < 18) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getWidth();
height = getHeight();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
this.mWidth = w;
this.mHeight = h;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;//1/4
mBitmapOuter = BitmapFactory.decodeResource(getResources(), resourceId);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setAntiAlias(true);
int bbw = mBitmapOuter.getWidth();
int bbh = mBitmapOuter.getHeight();
bmpCenter = new Point(bbw / 2, bbh / 2);
float ww = width / bbw;
mBitmapOuter = scaleBitmap(mBitmapOuter, mWidth / bbw);
canvas.drawBitmap(mBitmapOuter, 0, 0, new Paint());
}
private Bitmap scaleBitmap(Bitmap origin, float scale) {
if (origin == null) {
return null;
}
int height = origin.getHeight();
int width = origin.getWidth();
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);// 使用后乘
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
if (!origin.isRecycled()) {
origin.recycle();
}
return newBM;
}
public int getResourceId() {
return resourceId;
}
public void setResourceId(int resourceId) {
this.resourceId = resourceId;
postInvalidate();
}
}
attrs文件
< ?xml version="1.0" encoding="utf-8"?>
< resources>
< declare-styleable name="TailorImageView">
< attr name="resourceid" format="dimension">< /attr>
< /declare-styleable>
< /resources>
布局文件
Res-->Layout-->xml
com.swkj.share_earn.ui.view.TailorImageView
android:id="@+id/iv_poster"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10px"
android:scaleType="fitXY"
android:src="@drawable/poster_bg"