1212import android .graphics .Rect ;
1313import android .graphics .RectF ;
1414import android .graphics .Xfermode ;
15+
1516import androidx .appcompat .widget .AppCompatImageView ;
17+
1618import android .util .AttributeSet ;
1719import android .util .DisplayMetrics ;
20+ import android .util .Log ;
1821import android .view .MotionEvent ;
1922import android .view .WindowManager ;
2023
@@ -46,6 +49,7 @@ public class ClipImageView extends AppCompatImageView {
4649 private float mCircleCenterX , mCircleCenterY ;
4750 private float mCircleX , mCircleY ;
4851 private boolean isCutImage ;
52+ private float mRatio = 1.0f ;
4953
5054 public ClipImageView (Context context ) {
5155 super (context );
@@ -95,23 +99,23 @@ public void run() {
9599 @ Override
96100 protected void onLayout (boolean changed , int left , int top , int right , int bottom ) {
97101 super .onLayout (changed , left , top , right , bottom );
102+ setRadius ();
103+ }
104+
105+ private void setRadius () {
106+ mTargetWidth = getScreenWidth (getContext ());
107+ mTargetHeight = (int ) (mTargetWidth * mRatio );
98108 mCircleCenterX = getWidth () / 2 ;
99109 mCircleCenterY = getHeight () / 2 ;
100110 mCircleX = mCircleCenterX - mTargetWidth / 2 ;
101111 mCircleY = mCircleCenterY - mTargetHeight / 2 ;
102112 }
103113
104- private void setRadius () {
105-
106- int width = getScreenWidth (getContext ());
107- int height = getScreenHeight (getContext ());
108-
109- if (width > height ) {
110- mTargetWidth = height ;
111- mTargetHeight = height ;
112- } else {
113- mTargetWidth = width ;
114- mTargetHeight = width ;
114+ public void setRatio (float ratio ) {
115+ if (mRatio != ratio ) {
116+ mRatio = ratio ;
117+ setRadius ();
118+ invalidate ();
115119 }
116120 }
117121
@@ -126,14 +130,17 @@ protected void onDraw(Canvas canvas) {
126130 rf = new RectF (r );
127131 }
128132 // 画入前景圆形蒙板层
129- int sc = canvas .saveLayer (rf , null , Canvas .ALL_SAVE_FLAG );
133+ int sc = canvas .saveLayer (rf , null , Canvas .ALL_SAVE_FLAG );
130134 //画入矩形黑色半透明蒙板层
131135 canvas .drawRect (r , mFrontGroundPaint );
132136 //设置Xfermode,目的是为了去除矩形黑色半透明蒙板层和圆形的相交部分
133137 mFrontGroundPaint .setXfermode (mXfermode );
134138 //画入正方形
135- canvas .drawRect (mCircleCenterX - mTargetWidth / 2 , mCircleCenterY - mTargetHeight / 2 ,
136- mCircleCenterX + mTargetWidth / 2 , mCircleCenterY + mTargetHeight / 2 , mFrontGroundPaint );
139+ float left = mCircleCenterX - mTargetWidth / 2 ;
140+ float top = mCircleCenterY - mTargetHeight / 2 ;
141+ float right = mCircleCenterX + mTargetWidth / 2 ;
142+ float bottom = mCircleCenterY + mTargetHeight / 2 ;
143+ canvas .drawRect (left , top , right , bottom , mFrontGroundPaint );
137144
138145 canvas .restoreToCount (sc );
139146 //清除Xfermode,防止影响下次画图
@@ -153,10 +160,11 @@ public Bitmap clipImage() {
153160 Bitmap targetBitmap = Bitmap .createBitmap (mTargetWidth , mTargetHeight ,
154161 Bitmap .Config .ARGB_8888 );
155162 Canvas canvas = new Canvas (targetBitmap );
156- RectF dst = new RectF (-bitmap .getWidth () / 2 + mTargetWidth / 2 , -getHeight ()
157- / 2 + mTargetHeight / 2 , bitmap .getWidth () / 2
158- + mTargetWidth / 2 , getHeight () / 2 + mTargetHeight / 2 );
159-
163+ int left = -bitmap .getWidth () / 2 + mTargetWidth / 2 ;
164+ int top = -getHeight () / 2 + mTargetHeight / 2 ;
165+ int right = bitmap .getWidth () / 2 + mTargetWidth / 2 ;
166+ int bottom = getHeight () / 2 + mTargetHeight / 2 ;
167+ RectF dst = new RectF (left , top , right , bottom );
160168 canvas .drawBitmap (bitmap , null , dst , paint );
161169 setDrawingCacheEnabled (false );
162170 bitmap .recycle ();
@@ -300,33 +308,12 @@ private void midPoint(PointF point, MotionEvent event) {
300308 * 横向、纵向居中
301309 */
302310 protected void center () {
303-
304311 float height = mBitmapHeight ;
305312 float width = mBitmapWidth ;
306- float screenWidth = getWidth ();
307- float screenHeight = getHeight ();
308- float scale = 1f ;
309- if (width >= height ) {
310- scale = screenWidth / width ;
311-
312- if (scale * height < mTargetHeight ) {
313- scale = mTargetHeight / height ;
314- }
315-
316- } else {
317- if (height <= screenHeight ) {
318- scale = screenWidth / width ;
319- } else {
320- scale = screenHeight / height ;
321- }
322-
323- if (scale * width < mTargetWidth ) {
324- scale = mTargetWidth / width ;
325- }
326- }
313+ float scale = Math .max (mTargetWidth / width ,mTargetHeight / height );
327314
328- float deltaX = ( screenWidth - width * scale ) / 2f ;
329- float deltaY = ( screenHeight - height * scale ) / 2f ;
315+ float deltaX = -( width * scale - getWidth ()) / 2.0f ;
316+ float deltaY = -( height * scale - getHeight ()) / 2.0f ;
330317 mMatrix .postScale (scale , scale );
331318 mMatrix .postTranslate (deltaX , deltaY );
332319 setImageMatrix (mMatrix );
0 commit comments