Android 에서 ImageCropper 사각형,원형 재단 상자 의 실현 방법
5934 단어 imagecropper원형재단 틀
원형 재단 상 자 를 지원 하고 재단 후 원형 도안 을 생 성 합 니 다.
코드 는 오픈 소스 프로젝트 수정 을 바탕 으로 github 의 프로젝트 링크:https://github.com/shengge/android-crop ( 로 컬 다운로드)
아니면 효과 그림 을 붙 일 까요?
원형 재단 실현 부분 에 대해 말씀 드 리 겠 습 니 다.
1.UI 의 경우,사용자 정의 CircleHighlightView 를 HighlightView(기 존의 사각형 재단 상자 구현)에 계승 하여 draw 방법 을 직접 보십시오.
@Override
protected void draw(Canvas canvas) {
canvas.save();
Path path = new Path();
outlinePaint.setStrokeWidth( outlineWidth);
if(!hasFocus()) {// ,
outlinePaint.setColor( Color.BLACK);
canvas.drawRect( drawRect, outlinePaint);
}
else {
Rect viewDrawingRect = new Rect();
viewContext.getDrawingRect( viewDrawingRect);
// drawRect,
float radius = (drawRect.right - drawRect.left) / 2;
//
path.addCircle( drawRect.left + radius, drawRect.top + radius, radius, Direction.CW);
outlinePaint.setColor( highlightColor);
// ,path , outsidePaint
canvas.clipPath( path, Region.Op.DIFFERENCE);
canvas.drawRect( viewDrawingRect, outsidePaint);
canvas.restore();
// , outlinePaint Paint.Style.STROKE: 。
canvas.drawPath( path, outlinePaint);
// modifyMode grow , handles,
if(handleMode == HandleMode.Always || (handleMode == HandleMode.Changing && modifyMode == ModifyMode.Grow)) {
drawHandles( canvas);
}
}
}
여기에 원형 재단 틀 을 그 리 는 작업 이 이 루어 졌 다.2.사용자 터치 이벤트 응답 및 처리
1).터치 포인트 좌표 와 원 의 위 치 를 판단 한다.
/**
* x,y , ( 、 、 )
* @param x
* @param y
* @return
*/
private int getHitOnCircle(float x, float y) {
Rect r = computeLayout();
int retval = GROW_NONE;
final float hysteresis = 20F;
int radius = (r.right - r.left) / 2;
int centerX = r.left + radius;
int centerY = r.top + radius;
//
float ret = (x - centerX) * (x - centerX) + (y - centerY) * (y - centerY);
double rRadius = Math.sqrt( ret);
double gap = Math.abs( rRadius - radius);
if(gap <= hysteresis) {// 。 HighlightView( ) , , , 。 。
if(x < centerX) {// left
retval |= GROW_LEFT_EDGE;
}
else {
retval |= GROW_RIGHT_EDGE;
}
if(y < centerY) {// up
retval |= GROW_TOP_EDGE;
}
else {
retval |= GROW_BOTTOM_EDGE;
}
}
else if(rRadius > radius) {// outside
retval = GROW_NONE;
}
else if(rRadius < radius) {// inside, move
retval = MOVE;
}
return retval;
}
HighlightView(사각형 상자)에 계승 하여 이 루어 진 것 이기 때문에 점(x,y)위치 가 원 에 있 으 면 다른 상한 선 을 판단 하고 사각형 의 상하 좌우 위 치 를 판단 해 야 합 니 다.2). 이동 재단 상자
이전 단계 에서 판단 하면 터치 포인트 가 원 안에 있 으 면 MOVE 로 돌아 가 이동 과정 을 처리한다.
// Grows the cropping rectangle by (dx, dy) in image space
void moveBy(float dx, float dy) {
Rect invalRect = new Rect(drawRect);
//
cropRect.offset(dx, dy);
// Put the cropping rectangle inside image rectangle
cropRect.offset(
Math.max(0, imageRect.left - cropRect.left),
Math.max(0, imageRect.top - cropRect.top));
cropRect.offset(
Math.min(0, imageRect.right - cropRect.right),
Math.min(0, imageRect.bottom - cropRect.bottom));
drawRect = computeLayout();
invalRect.union(drawRect);
invalRect.inset(-(int) handleRadius, -(int) handleRadius);
viewContext.invalidate(invalRect);
}
재단 상 자 를 이동 하여 다른 이미지 그림 의 표시 범 위 를 확보 합 니 다.3).재단 상자 크기 조정
이 과정 은 이전 단계 와 유사 합 니 다.cropRect 행렬 을 등비 크기 로 조정 하면 됩 니 다.여기 서 자세히 설명 하 겠 습 니 다.코드:HighlightView.growby(float dx,float dy)
3.재단 그림 을 원형 으로 저장
/**
* @param bitmap src
* @return
*/
public static Bitmap getCircleBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap( bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas( output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect( 0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias( true);
paint.setFilterBitmap( true);
paint.setDither( true);
canvas.drawARGB( 0, 0, 0, 0);
paint.setColor( color);
//
canvas.drawCircle( bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
paint.setXfermode( new PorterDuffXfermode( Mode.SRC_IN));
canvas.drawBitmap( bitmap, rect, rect, paint);
return output;
}
메모:bitmap 를 file 로 저장 할 때 형식 은 png 를 선택 하 십시오.그렇지 않 으 면 검은색 배경 이 나타 납 니 다.수준 이 제한 되 어 있 음 을 감안 하여 어 렸 을 때 부터 국 어 를 잘 배우 지 못 했 고 묘사 가 비교적 난잡 하 므 로 깊이 이해 해 야 할 것 은 읽 으 세 요소스 코드.
첨부:또 하나의 좋 은 오픈 소스 프로젝트https://github.com/edmodo/cropper ( 로 컬 다운로드 )
총결산
이상 은 이 글 의 전체 내용 입 니 다.본 논문 의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 참고 학습 가치 가 있 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 댓 글 을 남 겨 주 셔 서 저희 에 대한 지지 에 감 사 드 립 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Rails】프로필 화상 둥글게 사이즈나 형태(메모)원 또는 원 안에 이미지를 표시합니다. 이미지 게시 기능 구현 CarrierWave 소개 css는 사용하지 않고 이번에는 image_tag 뒤에 원형 지정 "rounded-circle" 와 크기를 지정하고 있습니다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.