首页 > 代码库 > AndroidClipSquare安卓实现方形头像裁剪
AndroidClipSquare安卓实现方形头像裁剪
安卓实现方形头像裁剪
实现思路,界面可见区域为2层View
最顶层的View是显示层,主要绘制半透明边框区域和白色裁剪区域,代码比较容易。
第二层继承ImageView,使用ImageView的Matrix实现显示部分图片,及挪动,放大缩小等操作。
比较复杂的地方在于多指操作对ImageView的影响,详见代码:
ClipSquareImageView.java
?
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | package com.h3c.androidclipsquare; import android.annotation.TargetApi; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.os.Build; import android.util.AttributeSet; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.view.VelocityTracker; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewTreeObserver; import android.widget.ImageView; /** * Created by H3c on 12/13/14. */ public class ClipSquareImageView extends ImageView implements View.OnTouchListener, ViewTreeObserver.OnGlobalLayoutListener { private static final int BORDERDISTANCE = ClipSquareView.BORDERDISTANCE; public static final float DEFAULT_MAX_SCALE = 4 .0f; public static final float DEFAULT_MID_SCALE = 2 .0f; public static final float DEFAULT_MIN_SCALE = 1 .0f; private float minScale = DEFAULT_MIN_SCALE; private float midScale = DEFAULT_MID_SCALE; private float maxScale = DEFAULT_MAX_SCALE; private MultiGestureDetector multiGestureDetector; private boolean isIniting; // 正在初始化 private Matrix defaultMatrix = new Matrix(); // 初始化的图片矩阵,控制图片撑满屏幕及显示区域 private Matrix dragMatrix = new Matrix(); // 拖拽放大过程中动态的矩阵 private Matrix finalMatrix = new Matrix(); // 最终显示的矩阵 private final RectF displayRect = new RectF(); // 图片的真实大小 private final float [] matrixValues = new float [ 9 ]; private int borderlength; public ClipSquareImageView(Context context, AttributeSet attrs) { super (context, attrs); super .setScaleType(ScaleType.MATRIX); setOnTouchListener( this ); multiGestureDetector = new MultiGestureDetector(context); } @Override protected void onAttachedToWindow() { super .onAttachedToWindow(); getViewTreeObserver().addOnGlobalLayoutListener( this ); } @SuppressWarnings (deprecation) @Override protected void onDetachedFromWindow() { super .onDetachedFromWindow(); getViewTreeObserver().removeGlobalOnLayoutListener( this ); } @Override public void onGlobalLayout() { if (isIniting) { return ; } // 调整视图位置 initBmpPosition(); } /** * 初始化图片位置 */ private void initBmpPosition() { isIniting = true ; super .setScaleType(ScaleType.MATRIX); Drawable drawable = getDrawable(); if (drawable == null ) { return ; } final float viewWidth = getWidth(); final float viewHeight = getHeight(); final int drawableWidth = drawable.getIntrinsicWidth(); final int drawableHeight = drawable.getIntrinsicHeight(); if (viewWidth < viewHeight) { borderlength = ( int ) (viewWidth - 2 * BORDERDISTANCE); } else { borderlength = ( int ) (viewHeight - 2 * BORDERDISTANCE); } float screenScale = 1f; // 小于屏幕的图片会被撑满屏幕 if (drawableWidth <= drawableHeight) { // 竖图片 screenScale = ( float ) borderlength / drawableWidth; } else { // 横图片 screenScale = ( float ) borderlength / drawableHeight; } defaultMatrix.setScale(screenScale, screenScale); if (drawableWidth <= drawableHeight) { // 竖图片 float heightOffset = (viewHeight - drawableHeight * screenScale) / 2 .0f; if (viewWidth <= viewHeight) { // 竖照片竖屏幕 defaultMatrix.postTranslate(BORDERDISTANCE, heightOffset); } else { // 竖照片横屏幕 defaultMatrix.postTranslate((viewWidth - borderlength) / 2 .0f, heightOffset); } } else { float widthOffset = (viewWidth - drawableWidth * screenScale) / 2 .0f; if (viewWidth <= viewHeight) { // 横照片,竖屏幕 defaultMatrix.postTranslate(widthOffset, (viewHeight - borderlength) / 2 .0f); } else { // 横照片,横屏幕 defaultMatrix.postTranslate(widthOffset, BORDERDISTANCE); } } resetMatrix(); } /** * Resets the Matrix back to FIT_CENTER, and then displays it.s */ private void resetMatrix() { if (dragMatrix == null ) { return ; } dragMatrix.reset(); setImageMatrix(getDisplayMatrix()); } private Matrix getDisplayMatrix() { finalMatrix.set(defaultMatrix); finalMatrix.postConcat(dragMatrix); return finalMatrix; } @Override public boolean onTouch(View view, MotionEvent motionEvent) { return multiGestureDetector.onTouchEvent(motionEvent); } private class MultiGestureDetector extends GestureDetector.SimpleOnGestureListener implements ScaleGestureDetector.OnScaleGestureListener { private final ScaleGestureDetector scaleGestureDetector; private final GestureDetector gestureDetector; private final float scaledTouchSlop; private VelocityTracker velocityTracker; private boolean isDragging; private float lastTouchX; private float lastTouchY; private float lastPointerCount; // 上一次是几个手指事件 public MultiGestureDetector(Context context) { scaleGestureDetector = new ScaleGestureDetector(context, this ); gestureDetector = new GestureDetector(context, this ); gestureDetector.setOnDoubleTapListener( this ); final ViewConfiguration configuration = ViewConfiguration.get(context); scaledTouchSlop = configuration.getScaledTouchSlop(); } @Override public boolean onScale(ScaleGestureDetector scaleGestureDetector) { float scale = getScale(); float scaleFactor = scaleGestureDetector.getScaleFactor(); if (getDrawable() != null && ((scale < maxScale && scaleFactor > 1 .0f) || (scale > minScale && scaleFactor < 1 .0f))){ if (scaleFactor * scale < minScale){ scaleFactor = minScale / scale; } if (scaleFactor * scale > maxScale){ scaleFactor = maxScale / scale; } dragMatrix.postScale(scaleFactor, scaleFactor, getWidth() / 2 , getHeight() / 2 ); checkAndDisplayMatrix(); } return true ; } @Override public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) { return true ; } @Override public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) {} public boolean onTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true ; } scaleGestureDetector.onTouchEvent(event); /* * Get the center x, y of all the pointers */ float x = 0 , y = 0 ; final int pointerCount = event.getPointerCount(); for ( int i = 0 ; i < pointerCount; i++) { x += event.getX(i); y += event.getY(i); } x = x / pointerCount; y = y / pointerCount; /* * If the pointer count has changed cancel the drag */ if (pointerCount != lastPointerCount) { isDragging = false ; if (velocityTracker != null ) { velocityTracker.clear(); } lastTouchX = x; lastTouchY = y; lastPointerCount = pointerCount; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (velocityTracker == null ) { velocityTracker = VelocityTracker.obtain(); } else { velocityTracker.clear(); } velocityTracker.addMovement(event); lastTouchX = x; lastTouchY = y; isDragging = false ; break ; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: lastPointerCount = 0 ; if (velocityTracker != null ) { velocityTracker.recycle(); velocityTracker = null ; } break ; case MotionEvent.ACTION_MOVE: { final float dx = x - lastTouchX, dy = y - lastTouchY; if (isDragging == false ) { // Use Pythagoras to see if drag length is larger than // touch slop isDragging = Math.sqrt((dx * dx) + (dy * dy)) >= scaledTouchSlop; } if (isDragging) { if (getDrawable() != null ) { dragMatrix.postTranslate(dx, dy); checkAndDisplayMatrix(); } lastTouchX = x; lastTouchY = y; if (velocityTracker != null ) { velocityTracker.addMovement(event); } } break ; } } return true ; } @Override public boolean onDoubleTap(MotionEvent event) { try { float scale = getScale(); float x = getWidth() / 2 ; float y = getHeight() / 2 ; if (scale < midScale) { post( new AnimatedZoomRunnable(scale, midScale, x, y)); } else if ((scale >= midScale) && (scale < maxScale)) { post( new AnimatedZoomRunnable(scale, maxScale, x, y)); } else { // 双击缩小小于最小值 post( new AnimatedZoomRunnable(scale, minScale, x, y)); } } catch (Exception e) { // Can sometimes happen when getX() and getY() is called } return true ; } } private class AnimatedZoomRunnable implements Runnable { // These are ‘postScale‘ values, means they‘re compounded each iteration static final float ANIMATION_SCALE_PER_ITERATION_IN = 1 .07f; static final float ANIMATION_SCALE_PER_ITERATION_OUT = 0 .93f; private final float focalX, focalY; private final float targetZoom; private final float deltaScale; public AnimatedZoomRunnable( final float currentZoom, final float targetZoom, final float focalX, final float focalY) { this .targetZoom = targetZoom; this .focalX = focalX; this .focalY = focalY; if (currentZoom < targetZoom) { deltaScale = ANIMATION_SCALE_PER_ITERATION_IN; } else { deltaScale = ANIMATION_SCALE_PER_ITERATION_OUT; } } @Override public void run() { dragMatrix.postScale(deltaScale, deltaScale, focalX, focalY); checkAndDisplayMatrix(); final float currentScale = getScale(); if (((deltaScale > 1f) && (currentScale < targetZoom)) || ((deltaScale < 1f) && (targetZoom < currentScale))) { // We haven‘t hit our target scale yet, so post ourselves // again postOnAnimation(ClipSquareImageView. this , this ); } else { // We‘ve scaled past our target zoom, so calculate the // necessary scale so we‘re back at target zoom final float delta = targetZoom / currentScale; dragMatrix.postScale(delta, delta, focalX, focalY); checkAndDisplayMatrix(); } } } @TargetApi (Build.VERSION_CODES.JELLY_BEAN) private void postOnAnimation(View view, Runnable runnable) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.postOnAnimation(runnable); } else { view.postDelayed(runnable, 16 ); } } /** * Returns the current scale value * * @return float - current scale value */ public final float getScale() { dragMatrix.getValues(matrixValues); return matrixValues[Matrix.MSCALE_X]; } /** * Helper method that simply checks the Matrix, and then displays the result */ private void checkAndDisplayMatrix() { checkMatrixBounds(); setImageMatrix(getDisplayMatrix()); } private void checkMatrixBounds() { final RectF rect = getDisplayRect(getDisplayMatrix()); if ( null == rect) { return ; } float deltaX = 0 , deltaY = 0 ; final float viewWidth = getWidth(); final float viewHeight = getHeight(); // 判断移动或缩放后,图片显示是否超出裁剪框边界 final float heightBorder = (viewHeight - borderlength) / 2 ; final float weightBorder = (viewWidth - borderlength) / 2 ; if (rect.top > heightBorder){ deltaY = heightBorder - rect.top; } if (rect.bottom < (viewHeight - heightBorder)){ deltaY = viewHeight - heightBorder - rect.bottom; } if (rect.left > weightBorder){ deltaX = weightBorder - rect.left; } if (rect.right < viewWidth - weightBorder){ deltaX = viewWidth - weightBorder - rect.right; } // Finally actually translate the matrix dragMatrix.postTranslate(deltaX, deltaY); } /** * 获取图片相对Matrix的距离 * * @param matrix * - Matrix to map Drawable against * @return RectF - Displayed Rectangle */ private RectF getDisplayRect(Matrix matrix) { Drawable d = getDrawable(); if ( null != d) { displayRect.set( 0 , 0 , d.getIntrinsicWidth(), d.getIntrinsicHeight()); matrix.mapRect(displayRect); return displayRect; } return null ; } /** * 剪切图片,返回剪切后的bitmap对象 * * @return */ public Bitmap clip(){ Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); draw(canvas); return Bitmap.createBitmap(bitmap, (getWidth() - borderlength) / 2 , (getHeight() - borderlength) / 2 , borderlength, borderlength); } } |
结伴旅游,一个免费的交友网站:www.jieberu.com
推推族,免费得门票,游景区:www.tuituizu.com
AndroidClipSquare安卓实现方形头像裁剪
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。