//布局怡然是自定义布局
MainActivity中还是什么都没有
下面是自定义类的代码
/**
* date:2018/11/2
* author:王加辉(家辉辉辉)
* function: 继承式自定义控件
*/
@SuppressLint("AppCompatCustomView")
public class RectangleImageView extends ImageView {
private int lastX;
private int lastY;
public RectangleImageView(Context context) {
this(context,null);
}
public RectangleImageView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public RectangleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//初始化控件
}
/**
* 触摸事件
* */
@Override
public boolean onTouchEvent(MotionEvent event) {
//获取到手指处的x,y轴
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
lastX = x;
lastY = y;
break;
case MotionEvent.ACTION_MOVE:
int offX = x - lastX ;
int offY = y - lastY ;
movingXY(offX,offY);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
private void movingXY(int offX, int offY) {
//移动View
layout(getLeft() + offX , getTop() + offY , getRight() + offX , getBottom()+offY);
}
//测量
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
//布局
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
//绘制,继承的图片所以不用
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
---------------------
作者:家辉,喂
来源:CSDN
原文:https://blog.csdn.net/jiahui6666/article/details/83719343
版权声明:本文为博主原创文章,转载请附上博文链接!
|
|