android按钮平移,android双指平移、旋转、缩放控件完美版

package com.example.zhouqiong.richeditotandroid.edit;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Matrix;

import android.graphics.Paint;

import android.graphics.Point;

import android.graphics.PointF;

import android.graphics.Rect;

import android.graphics.RectF;

import android.support.annotation.Nullable;

import android.util.AttributeSet;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;

import android.widget.Scroller;

import com.blankj.utilcode.util.ToastUtils;

import com.example.zhouqiong.richeditotandroid.view.CustomsEditText;

import com.example.zhouqiong.richeditotandroid.xzhucourseware.R;

import com.example.zhouqiong.richeditotandroid.xzhucourseware.model.bean.BubbleInfo;

import com.example.zhouqiong.richeditotandroid.xzhucourseware.util.LogUtil;

/**

* Created by Robert on 2017/6/20.

*/

public class MoveLayout extends RelativeLayout {

private boolean isBorder = true;

private int dragDirection = 0;

private static final int TOP = 0x15;

private static final int LEFT = 0x16;

private static final int BOTTOM = 0x17;

private static final int RIGHT = 0x18;

private static final int LEFT_TOP = 0x11;

private static final int RIGHT_TOP = 0x12;

private static final int LEFT_BOTTOM = 0x13;

private static final int RIGHT_BOTTOM = 0x14;

private static final int CENTER = 0x19;

private int lastX;

private int lastY;

private int screenWidth;

private int screenHeight;

private int oriLeft;

private int oriRight;

private int oriTop;

private int oriBottom;

/**

* 标示此类的每个实例的id

*/

private int identity = 0;

/**

* 保存此布局所显示的Image的本地路径

*/

private String imgLocalPath;

/**

* 保存此布局所显示的Image的服务器地址

*/

private String imgUrlPath;

/**

* 保存此布局锁展示的图标id

*/

private int imgLocalId;

private String imgLogcalBitmap;

/**

* 图片类型(图片或图标)

*/

private int imgType;

/**

* 保存气泡内边距 及背景图片本地路径

*/

private BubbleInfo bubbleInfo;

private String editBg;

/**

* 触控区域设定

*/

private int touchAreaLength = 60;

private int minHeight = 120;

private int minWidth = 180;

private static final String TAG = "MoveLinearLayout";

private Paint mBorderPaint;

private float ratio = DEFAULT_RATIO;

public final static float DEFAULT_RATIO = 1f;

private boolean isBubble = false;

private int oriWidth, oriHeight;

private CustomsEditText customsEditText;

// private boolean isTopLimit = false;

// private boolean isBottomLimit = false;

public MoveLayout(Context context, float ratio) {

super(context);

this.ratio = ratio;

init(context);

}

public MoveLayout(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

init(context);

}

public MoveLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init(context);

}

private void init(Context context) {

mBorderPaint = new Paint();

mBorderPaint.setColor(Color.RED);

mBorderPaint.setStrokeWidth(5.0f);

mBorderPaint.setAntiAlias(true);

mBorderPaint.setStyle(Paint.Style.STROKE);

screenHeight = 500;//getResources().getDisplayMetrics().heightPixels - 40;

screenWidth = 500;// getResources().getDisplayMetrics().widthPixels;

}

public void setViewWidthHeight(int width, int height) {

screenWidth = width;

screenHeight = height;

}

public void setMinHeight(int height) {

minHeight = height;

if (minHeight < touchAreaLength * 2) {

minHeight = touchAreaLength * 2;

}

}

public void setMinWidth(int width) {

minWidth = width;

if (minWidth < touchAreaLength * 3) {

minWidth = touchAreaLength * 3;

}

}

public String getImgUrlPath() {

return imgUrlPath;

}

public void setImgUrlPath(String imgUrlPath) {

this.imgUrlPath = imgUrlPath;

}

public BubbleInfo getBubbleInfo() {

return bubbleInfo;

}

public void setBubbleInfo(BubbleInfo bubbleInfo) {

this.bubbleInfo = bubbleInfo;

}

public String getEditBg() {

return editBg;

}

public void setEditBg(String editBg) {

this.editBg = editBg;

}

public boolean isBorder() {

return isBorder;

}

public void setBorder(boolean border) {

isBorder = border;

}

private int mDeleteHeight = 0;

private int mDeleteWidth = 0;

private boolean isInDeleteArea = false;

public void setDeleteWidthHeight(int width, int height) {

mDeleteWidth = screenWidth - width;

mDeleteHeight = height;

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

}

private boolean isImage = false;

// 属性变量

private float translationX; // 移动X

private float translationY; // 移动Y

private float scale = 1; // 伸缩比例

private float rotation; // 旋转角度

// 移动过程中临时变量

private float actionX;

private float actionY;

private float spacing;

private float degree;

private int moveType; // 0=未选择,1=拖动,2=缩放

@Override

public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction() & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_DOWN:

mOnTouchListener.onTouch(this);

moveType = 1;

actionX = event.getRawX();

actionY = event.getRawY();

break;

case MotionEvent.ACTION_POINTER_DOWN:

moveType = 2;

spacing = getSpacing(event);

degree = getDegree(event);

break;

case MotionEvent.ACTION_MOVE:

if (moveType == 1) {

translationX = translationX + event.getRawX() - actionX;

translationY = translationY + event.getRawY() - actionY;

setTranslationX(translationX);

setTranslationY(translationY);

actionX = event.getRawX();

actionY = event.getRawY();

} else if (moveType == 2) {

scale = scale * getSpacing(event) / spacing;

setScaleX(scale);

setScaleY(scale);

rotation = rotation + getDegree(event) - degree;

if (rotation > 360) {

rotation = rotation - 360;

}

if (rotation < -360) {

rotation = rotation + 360;

}

setRotation(rotation);

}

break;

case MotionEvent.ACTION_UP:

case MotionEvent.ACTION_POINTER_UP:

moveType = 0;

}

return true;

}

// 触碰两点间距离

private float getSpacing(MotionEvent event) {

//通过三角函数得到两点间的距离

float x = event.getX(0) - event.getX(1);

float y = event.getY(0) - event.getY(1);

return (float) Math.sqrt(x * x + y * y);

}

// 取旋转角度

private float getDegree(MotionEvent event) {

//得到两个手指间的旋转角度

double delta_x = event.getX(0) - event.getX(1);

double delta_y = event.getY(0) - event.getY(1);

double radians = Math.atan2(delta_y, delta_x);

return (float) Math.toDegrees(radians);

}

public int getImgType() {

return imgType;

}

public void setImgType(int imgType) {

this.imgType = imgType;

}

public int getImgLocalId() {

return imgLocalId;

}

public void setImgLocalId(int imgLocalId) {

this.imgLocalId = imgLocalId;

}

public int getIdentity() {

return identity;

}

public void setIdentity(int identity) {

this.identity = identity;

}

public String getImgLocalPath() {

return imgLocalPath;

}

public void setImgLocalPath(String imgLocalPath) {

this.imgLocalPath = imgLocalPath;

}

private OnTouchListener mOnTouchListener;

protected interface OnTouchListener {

void onTouch(View view);

}

public void setOnTouchListener(OnTouchListener mOnTouchListener) {

this.mOnTouchListener = mOnTouchListener;

}

public boolean isBubble() {

return isBubble;

}

public void setBubble(boolean bubble) {

isBubble = bubble;

}

public int getOriWidth() {

return oriWidth;

}

public void setOriWidth(int oriWidth) {

this.oriWidth = oriWidth;

}

public int getOriHeight() {

return oriHeight;

}

public void setOriHeight(int oriHeight) {

this.oriHeight = oriHeight;

}

public CustomsEditText getCustomsEditText() {

return customsEditText;

}

public void setCustomsEditText(CustomsEditText customsEditText) {

this.customsEditText = customsEditText;

}

public String getImgLogcalBitmap() {

return imgLogcalBitmap;

}

public void setImgLogcalBitmap(String imgLogcalBitmap) {

this.imgLogcalBitmap = imgLogcalBitmap;

}

}

转载自:https://www.jianshu.com/p/df34e47bfa2a

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值