Android 图片拖拽、放大缩小的自定义控件

Android 图片拖拽、放大缩小的自定义控件

需求:像相册中的图片跟随手指拖动,双指的放大和缩小,相册中拖出范围之后有弹回的动画,感觉上很圆润,很舒服,我写的例子中并没有加动画

思路:1.自定义DragImageView.java

2.自定义中先画图片,图片大于屏幕就把图片缩小后显示,图片小于屏幕就直接显示,显示之前要计算显示图片的Rect,Rect是其实就是四个坐标,用来控制显示图片的范                  围,这个Rect是根据图片的长宽比例计算而来,显示在屏幕中间。

3.跟随手指移动:在touchEvent事件中处理好单双指的各种事件之后,跟随手指就是不断的改变Rect的坐标,然后不断的invalidate()(该方法是重新强制绘制View);

4.双指缩放:双指缩放的时候从图片Rect的中心缩放的,没有实现相册那种从手指中心缩放的那种。

5.单指和双指的事件在touc事件中已近处理好了

event.getPointerCount()==2的情况就是双指部分

float    X0,Y0,X1,Y1双指的四个坐标

在双指移动的时候计算一个双指的距离出来m1,在MOVE执行的时候重新计算一个新的距离m2,去判断m1和m2大小来判断双指是张开还是捏合,并且执行相应事件,判断之后把m2复制给m1,这时的MOVE事件会有新的m2出来,所以就一直判断就ok了。放大和缩小就是按照一定的比例去改变Rect的值。

DragImageView.java

public class DragImageView extends View {

private Paint mPaint;
 private Drawable mDrawable;
 private Rect mDrawableRect = new Rect();
 // private Rect mDrawableOffsetRect = new Rect();
 private Context mContext;
 private float mRation_WH = 0;
 private float mOldX = 0;
 private float mOldY = 0;
 private float mOldX0, mOldY0, mOldX1, mOldY1, mOldK, mOldB, mOldHandsX,
   mOldHandsY;
 private double mD1;
 private boolean isFirst = true;
 private int SINGALDOWN = 1;// 单点按下
 private int MUTILDOWM = 2;// 双点按下
 private int MUTILMOVE = 3;// 双点拖拽
 private int mStatus = 0;

enum STATUS {
  SINGAL, MUTILDOWN, MUTILMOVE;
 }

public DragImageView(Context context) {
  super(context);
  this.mContext = context;
  mPaint = new Paint();
  mPaint.setAntiAlias(true);
  mPaint.setColor(Color.BLACK);
  mPaint.setStyle(Style.FILL);
  mPaint.setTextSize(35.0f);
 }

@SuppressLint("DrawAllocation")
 @Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  super.onDraw(canvas);
  if (mDrawable == null || mDrawable.getIntrinsicHeight() == 0
    || mDrawable.getIntrinsicWidth() == 0) {
   return;
  }
  setBounds();
  mDrawable.draw(canvas);
  // Log.i("draw", "draw+++++++++++++++++++++++++++++++++++++++");

}

@SuppressLint("ClickableViewAccessibility")
 @Override
 public boolean onTouchEvent(MotionEvent event) {
  // TODO Auto-generated method stub
  switch (event.getPointerCount()) {
  case 1:

switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN:
    mStatus = SINGALDOWN;
    mOldX = event.getX();
    mOldY = event.getY();
    // Log.i("x_y_down", event.getX() + "__" + event.getY());
    break;
   case MotionEvent.ACTION_UP:
    checkBounds();
    // Log.i("x_y_up", event.getX() + "__" + event.getY());
    break;
   case MotionEvent.ACTION_MOVE:
    // Log.i("x_y_move", event.getX() + "__" + event.getY());
    if (mStatus == SINGALDOWN) {
     int offsetWidth = (int) (event.getX() - mOldX);
     int offsetHeight = (int) (event.getY() - mOldY);
     // Log.i("x_y_offset", offsetWidth + "__" + offsetHeight);
     mOldX = event.getX();
     mOldY = event.getY();
     mDrawableRect.offset(offsetWidth, offsetHeight);
     invalidate();
    }

break;
   default:
    break;
   }
   break;
  default:
   /*
    * mStatus = MUTILDOWM; if (mStatus == MUTILDOWM) { mOldX0 =
    * event.getX(0); mOldY0 = event.getY(0); mOldX1 = event.getX(1);
    * mOldY1 = event.getY(1); mOldK = (mOldY1 - mOldY0) / (mOldX1 -
    * mOldX0); mOldB = (mOldY0 * mOldX1 - mOldY1 * mOldX0) / (mOldX1 -
    * mOldX0); mOldHandsX = (mOldX0 + mOldX1) / 2; mOldHandsY =
    * mOldHandsX * mOldK + mOldB; mD1 = Math.sqrt(Math.pow(mOldX0 -
    * mOldX1, 2) + Math.pow(mOldY0 - mOldY1, 2)); Log.i("mD1", mD1 +
    * "________________"); }
    */
   switch (event.getAction()) {
   case MotionEvent.ACTION_POINTER_DOWN:
    Log.i("DOUBLETOWDOWN", "true");
    break;
   case MotionEvent.ACTION_MOVE:
    // Log.i("x_y_move", event.getX(0) + "__" + event.getY(0) +
    // "___"
    // + event.getX(1) + "__" + event.getY(1));
    mStatus = MUTILMOVE;
    float X0 = event.getX(0);
    float Y0 = event.getY(0);
    float X1 = event.getX(1);
    float Y1 = event.getY(1);
    float k = (Y1 - Y0) / (X1 - X0);
    float b = (Y0 * X1 - Y1 * X0) / (X1 - X0);
    int RectCenterX = mDrawableRect.centerX();
    int RectCenterY = mDrawableRect.centerY();
    float mHandsX = (X0 + X1) / 2;
    float mHandsY = mHandsX * k + b;
    double mD2 = Math.sqrt(Math.pow(X0 - X1, 2)
      + Math.pow(Y0 - Y1, 2));

Log.i("GCM", mD2 + "________________X:" + mHandsX + "___Y:"
      + mHandsY);
    if (mD1 < mD2) {

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/6e66e80307b3de27fc7bba39c427f836.html