-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClipImageView.java
More file actions
70 lines (60 loc) · 2.12 KB
/
ClipImageView.java
File metadata and controls
70 lines (60 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.gdmm.lib.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* clip contents image view
* Created by Jocoo on 2017/4/27.
*/
public class ClipImageView extends ImageView {
private Paint mPaint;
public ClipImageView(Context context) {
this(context, null);
}
public ClipImageView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public ClipImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setScaleType(ScaleType.MATRIX);
}
@Override
public void setScaleType(ScaleType scaleType) {
if (scaleType != ScaleType.MATRIX) {
throw new IllegalArgumentException(
"This ImageView only supports Matrix scale type.");
}
super.setScaleType(scaleType);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
final Drawable drawable = getDrawable();
if (drawable == null) return;
Matrix matrix = new Matrix();
float dx = 0;
float scale;
int vwidth = w - getPaddingLeft() - getPaddingRight();
int vheight = h - getPaddingTop() - getPaddingBottom();
int dwidth = drawable.getIntrinsicWidth();
int dheight = drawable.getIntrinsicHeight();
if (dheight * vwidth > vheight * dwidth) {
scale = (float) vwidth / (float) dwidth;
} else {
scale = (float) vheight / (float) dheight;
dx = (vwidth - dwidth * scale) * 0.5f;
}
matrix.setScale(scale, scale);
matrix.postTranslate(dx, 0);
setImageMatrix(matrix);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}