Android自定义View实现可拖拽缩放的矩形框

本文实例为大家分享了Android自定义View拖拽缩放矩形框的具体代码,供大家参考,具体内容如下

在开发项目中,需要一个矩形框来实现截屏功能,并且还需要可以任意拖拽和缩放,这就需要自定义View来实现了,具体功能如下:

1.自定义View

package com.xinrui.screenshot.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

public class CropRectView extends View {

 // 绘制 损害框和损害名称
 private Paint mPaint;
 private RectF mRectF;

 // 边缘字体
// private BorderedText mBorderedText;

 // 标题 或 名字
 private String mTitle;
 // 概率
 private float mConfidence;

 // 矩形框 corner 的角度:直角、圆角
 private int mCornerAngle;

 //直角 默认
 public static final int RIGHT_CORNER = 0;
 //圆角
 public static final int ROUND_CORNER = 1;

 // Remove Rect
 private int MODE;
 private static final int MODE_OUTSIDE = 0x000000aa;/*170*/
 private static final int MODE_INSIDE = 0x000000bb;/*187*/
 private static final int MODE_POINT = 0X000000cc;/*204*/
 private static final int MODE_ILLEGAL = 0X000000dd;/*221*/

 private float startX;/*start X location*/
 private float startY;/*start Y location*/
 private float endX;/*end X location*/
 private float endY;/*end Y location*/

 private float currentX;/*X coordinate values while finger press*/
 private float currentY;/*Y coordinate values while finger press*/

 private float memoryX;/*the last time the coordinate values of X*/
 private float memoryY;/*the last time the coordinate values of Y*/

 private float mCoverWidth;/*width of selection box*/
 private float mCoverHeight;/*height of selection box*/

 private static final int ACCURACY = 100;/*touch accuracy*/
 private int pointPosition;/*vertex of a rectangle*/

 private static final float minWidth = 100.0f;/*the minimum width of the rectangle*/
 private static final float minHeight = 200.0f;/*the minimum height of the rectangle*/

 private onLocationListener mLocationListener;/*listen to the Rect */

 private static final float EDGE_WIDTH = 1.8f;

 public MoveAndCropRectView(Context context) {
  this(context, null);
 }

 public MoveAndCropRectView(Context context, @Nullable AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public MoveAndCropRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  initDatas(context);
 }

 private void initDatas(Context context) {
  mPaint = new Paint();
  mRectF = new RectF();

  //画笔设置空心
  mPaint.setStyle(Paint.Style.STROKE);
  mPaint.setColor(Color.WHITE);
  mPaint.setStrokeWidth(2);
  mPaint.setAntiAlias(true);

//  float textSizePx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
//    18.0f, context.getResources().getDisplayMetrics());
//  mBorderedText = new BorderedText(textSizePx);
  currentX = 0;
  currentY = 0;
 }

 private boolean firstDraw = true;

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);

//  switch (mCornerAngle) {
//   case RIGHT_CORNER:// 绘制 损害框(直角矩形框)
//    drawRect(canvas);
//    break;
//   case ROUND_CORNER:// 绘制 损害框(圆角矩形框)
//    drawRoundRect(canvas);
//    break;
//  }

  if (firstDraw) {
   firstDraw = false;
   startX = mRectF.left;
   startY = mRectF.top;
   endX = mRectF.right;
   endY = mRectF.bottom;

   mCoverWidth = mRectF.width();
   mCoverHeight = mRectF.height();
  }

  if (mLocationListener != null) {
   mLocationListener.locationRect(startX, startY, endX, endY);
  }

//  LogUtils.d("onDraw -- startX: " + startX);

  canvas.drawLine(startX - EDGE_WIDTH, startY - EDGE_WIDTH,
    endX + EDGE_WIDTH, startY - EDGE_WIDTH, mPaint);/*top 上边框-*/
  canvas.drawLine(startX - EDGE_WIDTH, endY + EDGE_WIDTH,
    endX + EDGE_WIDTH, endY + EDGE_WIDTH, mPaint);/*bottom -*/
  canvas.drawLine(startX - EDGE_WIDTH, startY - EDGE_WIDTH,
    startX - EDGE_WIDTH, endY + EDGE_WIDTH, mPaint);/*left |*/
  canvas.drawLine(endX + EDGE_WIDTH, startY - EDGE_WIDTH,
    endX + EDGE_WIDTH, endY + EDGE_WIDTH, mPaint);/*right |*/

  // 绘制名称 和 概率
//  final String labelString =
//    !TextUtils.isEmpty(mTitle)
//      ? String.format("%s %.2f", mTitle, (100 * mConfidence))
//      : String.format("%.2f", (100 * mConfidence));
//
//  // 在 直角矩形框 上写字
//  mBorderedText.drawText(canvas,
//    startX,
//    startY, labelString + "%",
//    mPaint);
 }

 @SuppressWarnings("NullableProblems")
 @Override
 public boolean onTouchEvent(MotionEvent event) {
  switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN:
    memoryX = event.getX();
    memoryY = event.getY();
    checkMode(memoryX, memoryY);
    break;
   case MotionEvent.ACTION_MOVE: {
    currentX = event.getX();
    currentY = event.getY();
    switch (MODE) {
     case MODE_ILLEGAL:
      recoverFromIllegal(currentX, currentY);
      postInvalidate();
      break;
     case MODE_OUTSIDE:
      //do nothing;
      break;
     case MODE_INSIDE://拖动
      moveByTouch(currentX, currentY);
      postInvalidate();
      break;
     default:
      /*MODE_POINT*/
      moveByPoint(currentX, currentY);
      postInvalidate();
      break;
    }
   }
   break;
   case MotionEvent.ACTION_UP:
//    mPaint.setColor(getContext().getResources().getColor(R.color.orange));
    postInvalidate();
    break;
   default:
    break;
  }
  return true;
 }

 /*点击顶点附近时的缩放处理*/
 @SuppressWarnings("SuspiciousNameCombination")
 private void moveByPoint(float bx, float by) {
//  LogUtils.d("moveByPoint");
  switch (pointPosition) {
   case 0:/*left-up*/
    mCoverWidth = Math.abs(endX - bx);
    mCoverHeight = Math.abs(endY - by);
    //noinspection SuspiciousNameCombination
    if (!checkLegalRect(mCoverWidth, mCoverHeight)) {
     MODE = MODE_ILLEGAL;
    } else {
     refreshLocation(bx, by, endX, endY);
    }
    break;
   case 1:/*right-up*/
    mCoverWidth = Math.abs(bx - startX);
    mCoverHeight = Math.abs(endY - by);
    if (!checkLegalRect(mCoverWidth, mCoverHeight)) {
     MODE = MODE_ILLEGAL;
    } else {
     refreshLocation(startX, by, bx, endY);
    }
    break;
   case 2:/*left-down*/
    mCoverWidth = Math.abs(endX - bx);
    mCoverHeight = Math.abs(by - startY);
    if (!checkLegalRect(mCoverWidth, mCoverHeight)) {
     MODE = MODE_ILLEGAL;
    } else {
     refreshLocation(bx, startY, endX, by);
    }
    break;
   case 3:/*right-down*/
    mCoverWidth = Math.abs(bx - startX);
    mCoverHeight = Math.abs(by - startY);
    if (!checkLegalRect(mCoverWidth, mCoverHeight)) {
     MODE = MODE_ILLEGAL;
    } else {
     refreshLocation(startX, startY, bx, by);
    }
    break;
   default:
    break;
  }
 }

 /*刷新矩形的坐标*/
 private void refreshLocation(float isx, float isy, float iex, float iey) {
  this.startX = isx;
  this.startY = isy;
  this.endX = iex;
  this.endY = iey;

  mCoverWidth = endX - startX;
  mCoverHeight = endY - startY;

 }

 /*检测矩形是否达到最小值*/
 private boolean checkLegalRect(float cHeight, float cWidth) {
  return (cHeight > minHeight && cWidth > minWidth);
 }

 /*从非法状态恢复,这里处理的是达到最小值后能拉伸放大*/
 private void recoverFromIllegal(float rx, float ry) {
  if ((rx > startX && ry > startY) && (rx < endX && ry < endY)) {
   MODE = MODE_ILLEGAL;
  } else {
   MODE = MODE_POINT;
  }
 }

 /**
  * 判断点在矩形的什么位置
  * @param cx
  * @param cy
  */
 private void checkMode(float cx, float cy) {
  if (cx > startX && cx < endX && cy > startY && cy < endY) {
   MODE = MODE_INSIDE;//矩形内部
  } else if (nearbyPoint(cx, cy) < 4) {
   MODE = MODE_POINT;//矩形点上
  } else {
   MODE = MODE_OUTSIDE;//矩形外部
  }
 }

 /*矩形随手指移动*/
 private void moveByTouch(float mx, float my) {/*move center point*/
  float dX = mx - memoryX;
  float dY = my - memoryY;

  startX += dX;
  startY += dY;
  if(startX<=0){
   startX=0;
  }
  if(startY<=0){
   startY=0;
  }
  endX = startX + mCoverWidth;
  endY = startY + mCoverHeight;
  if(endX>=1920){
   endX=1920;
   startX=endX-mCoverWidth;
  }
  if(endY>=1080){
   endY=1080;
   startY=endY-mCoverHeight;
  }
  memoryX = mx;
  memoryY = my;
 }

 /*判断点(inX,inY)是否靠近矩形的4个顶点*/
 private int nearbyPoint(float floatX, float floatY) {
  if ((Math.abs(startX - floatX) <= ACCURACY && (Math.abs(floatY - startY) <= ACCURACY))) {/*left-up angle*/
   pointPosition = 0;
   return 0;
  }
  if ((Math.abs(endX - floatX) <= ACCURACY && (Math.abs(floatY - startY) <= ACCURACY))) {/*right-up angle*/
   pointPosition = 1;
   return 1;
  }
  if ((Math.abs(startX - floatX) <= ACCURACY && (Math.abs(floatY - endY) <= ACCURACY))) {/*left-down angle*/
   pointPosition = 2;
   return 2;
  }
  if ((Math.abs(endX - floatX) <= ACCURACY && (Math.abs(floatY - endY) <= ACCURACY))) {/*right-down angle*/
   pointPosition = 3;
   return 3;
  }
  pointPosition = 100;
  return 100;
 }

 // 设置矩形框
 public void setRectF(RectF rectf) {
  this.mRectF = rectf;
 }

 public void setTitle(String title) {
  mTitle = title;
 }

 public void setConfidence(float confidence) {
  mConfidence = confidence;
 }

 public void setCornerAngle(int cornerAngle) {
  this.mCornerAngle = cornerAngle;
 }

 // 绘制 损害框(直角矩形框)
 private void drawRect(Canvas canvas) {

  canvas.drawRect(mRectF, mPaint);

  // 绘制名称 和 概率
//  final String labelString =
//    !TextUtils.isEmpty(mTitle)
//      ? String.format("%s %.2f", mTitle, (100 * mConfidence))
//      : String.format("%.2f", (100 * mConfidence));

  // 在 直角矩形框 上写字
//  mBorderedText.drawText(canvas,
//    mRectF.left,
//    mRectF.top, labelString + "%",
//    mPaint);
 }

 // 绘制 损害框(圆角矩形框)
 private void drawRoundRect(Canvas canvas) {
  float cornerSize = Math.min(mRectF.width(), mRectF.height()) / 8.0f;
  canvas.drawRoundRect(mRectF, cornerSize, cornerSize, mPaint);

  // 绘制名称 和 概率
//  final String labelString =
//    !TextUtils.isEmpty(mTitle)
//      ? String.format("%s %.2f", mTitle, (100 * mConfidence))
//      : String.format("%.2f", (100 * mConfidence));

  // 在 圆角矩形框 上写字
//  mBorderedText.drawText(canvas,
//    mRectF.left + cornerSize,
//    mRectF.top, labelString + "%",
//    mPaint);
 }

 public void setLocationListener(onLocationListener mLocationListener) {
  this.mLocationListener = mLocationListener;
 }

 public interface onLocationListener {
  void locationRect(float startX, float startY, float endX, float endY);
 }

}

2.Activity里的应用

package com.xinrui.screenshot;

import android.app.Activity;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.widget.RelativeLayout;

import com.xinrui.screenshot.view.CropRectView;

public class MainActivity extends Activity {
 private RelativeLayout main_area;
 CropRectView cropRectView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initview();
 }

 private void initview(){
  main_area = (RelativeLayout)findViewById(R.id.main_area);
  cropRectView = (CropRectView)findViewById(R.id.main_img);
  RectF rectF = new RectF(660, 240, 1260, 840);
  cropRectView.setRectF(rectF);

  cropRectView.setLocationListener(new CropRectView.onLocationListener() {
   @Override
   public void locationRect(float startX, float startY, float endX, float endY) {
    Log.e("MainActivity","[ startX:(" + startX + ")--startY:(" + startY + ")--endX:(" + endX + ")--endY:(" + endY + ") ]");
   }
  });
 }
}

3.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/main_area"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <com.xinrui.screenshot.view.CropRectView
  android:id="@+id/main_img"
  android:layout_centerInParent="true"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>
</RelativeLayout>

大功告成。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android自定义view实现拖拽选择按钮

    本文实例为大家分享了Android实现拖拽选择按钮的具体代码,供大家参考,具体内容如下 github地址:https://github.com/xuezj/DragChooseDemo DragChooseDemo 效果图 Attributes属性(布局文件中的自定义属性) 半径.文字大小.按钮个数注意配合使用,以达到最佳效果 方法 使用 布局文件中的使用 <com.xuezj.dragchooselibrary.view.DragChooseView android:id="@+id/m

  • Android实现View的拖拽

    本文实例为大家分享了Android实现View拖拽的具体代码,供大家参考,具体内容如下 前言 实现View的拖拽,其实原理很简单.无非就是获取手指的位移信息,然后view根据手指的位移信息,移动对应的位置. 首先是获取手机的位移信息就可以根据需求不同分为两种 拖拽view本身,view实现移动.则设置view的setOnTouchListener. 在activity中随意滑动,view都会反应出动作.则重写activity onTouchEvent方法. 而移动的方法嘛,也有几种 给view设

  • Android HorizontalScrollView内子控件横向拖拽实例代码

    前言 网上ListView上下拖动的例子有,效果也很好,但是项目要横着拖的,只要硬着头皮自己写(主要是没找到合适的),参考文章1修改而来,分享一下. 正文 截图 实现代码: public class HoDragActivity extends Activity { private LinearLayout main; private GestureDetector mGestureDetector; @Override public void onCreate(Bundle savedInst

  • Android recyclerview实现拖拽排序和侧滑删除

    Recyclerview现在基本已经替代Listview了,RecyclerView也越来越好用了  当我们有实现条目的拖拽排序和侧滑删除时  可以直接时候Recyclerview提供的API就可以直接实现了 先贴上主要代码 private void initveiw() { ArrayList<String> items = new ArrayList<>(Arrays.asList("itme1", "item2", "itme

  • android ListView和GridView拖拽移位实现代码

    关于ListView拖拽移动位置,想必大家并不陌生,比较不错的软件都用到如此功能了.如:搜狐,网易,百度等,但是相比来说还是百度的用户体验较好,不偏心了,下面看几个示例:             首先说一下:拖拽ListView的item就不应该可以任意移动,只应该在ListView所在的范围内,而网易的你看看我都可以移动到状态栏了,虽然你做了处理,但是用户体验我个人感觉不好,在看看百度的,不仅控制了移动范围,更不错的百度的移动起来会时时的换位,看起来相当的形象,所以我认为这样相当的棒.说明一点

  • Android使用RecycleView实现拖拽交换item位置

    本文实例为大家分享了RecycleView实现拖拽交换item位置的具体代码,供大家参考,具体内容如下 老规矩,先来一张效果图: 相比起ListView而言,RecycleView实现拖拽交换位置的效果要简单很多,因为通过SDK中的ItemTouchHelper工具类可以轻松的实现这种效果,并且一套代码支持所有布局方式;而ListView的话则需要通过生成View的缓存镜像设置到ImageView中,然后通过WindowManager来操作该ImageView,具体怎么实现这里就不展开讲解了.回

  • Android自定义View实现拖拽效果

    腾讯QQ有那种红点拖动效果,今天就来实现一个简单的自定义View拖动效果,再回到原处,并非完全仿QQ红点拖动. 先来看一下效果图 简单说一下实现步骤 1.创建一个类继承View 2.绘制出一个小球 3.重写onTouchEvent,来根据手指放下,移动,抬起,来控制小球 4.直接在布局中引用 先贴一张图看下View的坐标系 下面就贴一下代码,最后会给出源码 public class CustomView extends View { private int lastX; private int

  • Android中在GridView网格视图上实现item拖拽交换的方法

    GridView基础 新建一个HelloGridView的工程 修改main.xml代码如下: <?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width=&q

  • Android自定义View实现可以拖拽的GridView

    先看看效果图 主要思想: 1.监听触碰事件 2.用WindowManager添加拖曳的图片 3.用Collections.swap()交换List数据 自定义代码: public class DragGridVeiw extends GridView { private final int PRESS_TIME = 1000;//长按时间 private int mDownX;//触碰时的X坐标 private int mDownY;//触碰时的Y坐标 private int mMoveX;//

  • Android新特性页面之ViewPager拖拽到最后一页再拖拽打开其他Activity(三种方法)

    android新特性页面,ViewPager拖拽到最后一页再拖拽打开其他Activity.实现的方式有很多,效果比较好的就是到了最后一页再拖拽出现禁止蓝色条时再跳转activity 方式一:拿到ViewPager的边界条EdgeEffectCompat,判断是否到了边界(获取EdgeEffectCompat通过反射,方法来自网络最后边会给出原文) /** * 初始化view */ private void initView(){ pager = (ViewPager) findViewById(

随机推荐