1919import android .view .WindowManager ;
2020
2121public class ClipImageView extends AppCompatImageView {
22-
2322 private PointF mDownPoint ;
2423 private PointF mMiddlePoint ;
2524 private Matrix mMatrix ;
@@ -46,6 +45,7 @@ public class ClipImageView extends AppCompatImageView {
4645 private float mCircleCenterX , mCircleCenterY ;
4746 private float mCircleX , mCircleY ;
4847 private boolean isCutImage ;
48+ private float mRatio = 1.0f ;
4949
5050 public ClipImageView (Context context ) {
5151 super (context );
@@ -95,23 +95,23 @@ public void run() {
9595 @ Override
9696 protected void onLayout (boolean changed , int left , int top , int right , int bottom ) {
9797 super .onLayout (changed , left , top , right , bottom );
98+ setRadius ();
99+ }
100+
101+ private void setRadius () {
102+ mTargetWidth = getScreenWidth (getContext ());
103+ mTargetHeight = (int ) (mTargetWidth * mRatio );
98104 mCircleCenterX = getWidth () / 2 ;
99105 mCircleCenterY = getHeight () / 2 ;
100106 mCircleX = mCircleCenterX - mTargetWidth / 2 ;
101107 mCircleY = mCircleCenterY - mTargetHeight / 2 ;
102108 }
103109
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 ;
110+ public void setRatio (float ratio ) {
111+ if (mRatio != ratio ) {
112+ mRatio = ratio ;
113+ setRadius ();
114+ invalidate ();
115115 }
116116 }
117117
@@ -126,14 +126,17 @@ protected void onDraw(Canvas canvas) {
126126 rf = new RectF (r );
127127 }
128128 // 画入前景圆形蒙板层
129- int sc = canvas .saveLayer (rf , null , Canvas .ALL_SAVE_FLAG );
129+ int sc = canvas .saveLayer (rf , null , Canvas .ALL_SAVE_FLAG );
130130 //画入矩形黑色半透明蒙板层
131131 canvas .drawRect (r , mFrontGroundPaint );
132132 //设置Xfermode,目的是为了去除矩形黑色半透明蒙板层和圆形的相交部分
133133 mFrontGroundPaint .setXfermode (mXfermode );
134134 //画入正方形
135- canvas .drawRect (mCircleCenterX - mTargetWidth / 2 , mCircleCenterY - mTargetHeight / 2 ,
136- mCircleCenterX + mTargetWidth / 2 , mCircleCenterY + mTargetHeight / 2 , mFrontGroundPaint );
135+ float left = mCircleCenterX - mTargetWidth / 2 ;
136+ float top = mCircleCenterY - mTargetHeight / 2 ;
137+ float right = mCircleCenterX + mTargetWidth / 2 ;
138+ float bottom = mCircleCenterY + mTargetHeight / 2 ;
139+ canvas .drawRect (left , top , right , bottom , mFrontGroundPaint );
137140
138141 canvas .restoreToCount (sc );
139142 //清除Xfermode,防止影响下次画图
@@ -153,10 +156,11 @@ public Bitmap clipImage() {
153156 Bitmap targetBitmap = Bitmap .createBitmap (mTargetWidth , mTargetHeight ,
154157 Bitmap .Config .ARGB_8888 );
155158 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-
159+ int left = -bitmap .getWidth () / 2 + mTargetWidth / 2 ;
160+ int top = -getHeight () / 2 + mTargetHeight / 2 ;
161+ int right = bitmap .getWidth () / 2 + mTargetWidth / 2 ;
162+ int bottom = getHeight () / 2 + mTargetHeight / 2 ;
163+ RectF dst = new RectF (left , top , right , bottom );
160164 canvas .drawBitmap (bitmap , null , dst , paint );
161165 setDrawingCacheEnabled (false );
162166 bitmap .recycle ();
@@ -300,33 +304,12 @@ private void midPoint(PointF point, MotionEvent event) {
300304 * 横向、纵向居中
301305 */
302306 protected void center () {
303-
304307 float height = mBitmapHeight ;
305308 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- }
309+ float scale = Math .max (mTargetWidth / width ,mTargetHeight / height );
322310
323- if (scale * width < mTargetWidth ) {
324- scale = mTargetWidth / width ;
325- }
326- }
327-
328- float deltaX = (screenWidth - width * scale ) / 2f ;
329- float deltaY = (screenHeight - height * scale ) / 2f ;
311+ float deltaX = -(width * scale - getWidth ()) / 2.0f ;
312+ float deltaY = -(height * scale - getHeight ()) / 2.0f ;
330313 mMatrix .postScale (scale , scale );
331314 mMatrix .postTranslate (deltaX , deltaY );
332315 setImageMatrix (mMatrix );
@@ -351,4 +334,5 @@ public static int getScreenHeight(Context context) {
351334 wm .getDefaultDisplay ().getMetrics (outMetrics );
352335 return outMetrics .heightPixels ;
353336 }
337+
354338}
0 commit comments