设为首页 加入收藏

TOP

Android 自定义控件——图片剪裁(二)
2015-02-02 14:32:00 来源: 作者: 【 】 浏览:51
Tags:Android 定义 控件 图片 剪裁
oat.top + dy, mDrawableFloat.right
? ? ? ? ? + dx, mDrawableFloat.bottom);
? ? ? break;


? ? ?case EDGE_LB:
? ? ? mDrawableFloat.set(mDrawableFloat.left + dx,
? ? ? ? mDrawableFloat.top, mDrawableFloat.right,
? ? ? ? mDrawableFloat.bottom + dy);
? ? ? break;


? ? ?case EDGE_RB:
? ? ? mDrawableFloat.set(mDrawableFloat.left,
? ? ? ? mDrawableFloat.top, mDrawableFloat.right + dx,
? ? ? ? mDrawableFloat.bottom + dy);
? ? ? break;


? ? ?case EDGE_MOVE_IN:
? ? ? if (isTouchInSquare) {
? ? ? ?mDrawableFloat.offset((int) dx, (int) dy);
? ? ? }
? ? ? break;


? ? ?case EDGE_MOVE_OUT:
? ? ? break;
? ? ?}
? ? ?mDrawableFloat.sort();
? ? ?invalidate();
? ? }
? ?}
? ?break;
? }


? return true;
?}


?// 根据初触摸点判断是触摸的Rect哪一个角
?public int getTouch(int eventX, int eventY) {
? if (mFloatDrawable.getBounds().left <= eventX
? ? && eventX < (mFloatDrawable.getBounds().left + mFloatDrawable
? ? ? .getBorderWidth())
? ? && mFloatDrawable.getBounds().top <= eventY
? ? && eventY < (mFloatDrawable.getBounds().top + mFloatDrawable
? ? ? .getBorderHeight())) {
? ?return EDGE_LT;
? } else if ((mFloatDrawable.getBounds().right - mFloatDrawable
? ? .getBorderWidth()) <= eventX
? ? && eventX < mFloatDrawable.getBounds().right
? ? && mFloatDrawable.getBounds().top <= eventY
? ? && eventY < (mFloatDrawable.getBounds().top + mFloatDrawable
? ? ? .getBorderHeight())) {
? ?return EDGE_RT;
? } else if (mFloatDrawable.getBounds().left <= eventX
? ? && eventX < (mFloatDrawable.getBounds().left + mFloatDrawable
? ? ? .getBorderWidth())
? ? && (mFloatDrawable.getBounds().bottom - mFloatDrawable
? ? ? .getBorderHeight()) <= eventY
? ? && eventY < mFloatDrawable.getBounds().bottom) {
? ?return EDGE_LB;
? } else if ((mFloatDrawable.getBounds().right - mFloatDrawable
? ? .getBorderWidth()) <= eventX
? ? && eventX < mFloatDrawable.getBounds().right
? ? && (mFloatDrawable.getBounds().bottom - mFloatDrawable
? ? ? .getBorderHeight()) <= eventY
? ? && eventY < mFloatDrawable.getBounds().bottom) {
? ?return EDGE_RB;
? } else if (mFloatDrawable.getBounds().contains(eventX, eventY)) {
? ?return EDGE_MOVE_IN;
? }
? return EDGE_MOVE_OUT;
?}


?@Override
?protected void onDraw(Canvas canvas) {


? if (mDrawable == null) {
? ?return;
? }


? if (mDrawable.getIntrinsicWidth() == 0
? ? || mDrawable.getIntrinsicHeight() == 0) {
? ?return;
? }


? configureBounds();
? // 在画布上花图片
? mDrawable.draw(canvas);
? canvas.save();
? // 在画布上画浮层FloatDrawable,Region.Op.DIFFERENCE是表示Rect交集的补集
? canvas.clipRect(mDrawableFloat, Region.Op.DIFFERENCE);
? // 在交集的补集上画上灰色用来区分
? canvas.drawColor(Color.parseColor("#a0000000"));
? canvas.restore();
? // 画浮层
? mFloatDrawable.draw(canvas);
?}


?protected void configureBounds() {
? // configureBounds在onDraw方法中调用
? // isFirst的目的是下面对mDrawableSrc和mDrawableFloat只初始化一次,
? // 之后的变化是根据touch事件来变化的,而不是每次执行重新对mDrawableSrc和mDrawableFloat进行设置
? if (isFrist) {
? ?oriRationWH = ((float) mDrawable.getIntrinsicWidth())
? ? ?/ ((float) mDrawable.getIntrinsicHeight());


? ?final float scale = mContext.getResources().getDisplayMetrics().density;
? ?int w = Math.min(getWidth(), (int) (mDrawable.getIntrinsicWidth()
? ? ?* scale + 0.5f));
? ?int h = (int) (w / oriRationWH);


? ?int left = (getWidth() - w) / 2;
? ?int top = (getHeight() - h) / 2;
? ?int right = left + w;
? ?int bottom = top + h;


? ?mDrawableSrc.set(left, top, right, bottom);
? ?mDrawableDst.set(mDrawableSrc);


? ?int floatWidth = dipTopx(mContext, cropWidth);
? ?int floatHeight = dipTopx(mContext, cropHeight);


? ?if (floatWidth > getWidth()) {
? ? floatWidth = getWidth();
? ? floatHeight = cropHeight * floatWidth / cropWidth;
? ?}


? ?if (floatHeight > getHeight()) {
? ? floatHeight = getHeight();
? ? floatWidth = cropWidth * floatHeight / cropHeight;
? ?}


? ?int floatLeft = (getWidth() - floatWidth) / 2;
? ?int floatTop = (getHeight() - floatHeight) / 2;
? ?mDrawableFloat.set(floatLeft, floatTop, floatLeft + floatWidth,
? ? ?floatTop + floatHeight);


? ?isFrist = false;
? }


? mDrawable.se

首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 图片拖拽、放大缩小的自.. 下一篇Android ContentObserver使用实现..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: