Android 通过用户事件触摸(在此点上)绘制具有4个点和拉角的形状
我想制作一个具有4个点的形状,然后通过触摸事件为用户绘制并提供拉动角度。Android 通过用户事件触摸(在此点上)绘制具有4个点和拉角的形状,android,xamarin,touch,shape,user-interaction,Android,Xamarin,Touch,Shape,User Interaction,我想制作一个具有4个点的形状,然后通过触摸事件为用户绘制并提供拉动角度。 例如,我有一个ImageView,其中我将填充从画廊/相机(nevermind)拍摄的图像,之后必须显示带有4个点的形状(我认为每个点将是一个小圆圈的图像),用户可以通过触摸到该点来改变角度。 这个点就像坐标,我想得到这个值,然后做一些透视。 换句话说: ImageView填充图像->显示带4个点的抽屉形状 用户把这几点作为他想要的 当工作完成时,用这个坐标来做透视图 那么我如何才能做到这一点呢?谢谢 最后,这篇文章给我
例如,我有一个ImageView,其中我将填充从画廊/相机(nevermind)拍摄的图像,之后必须显示带有4个点的形状(我认为每个点将是一个小圆圈的图像),用户可以通过触摸到该点来改变角度。
这个点就像坐标,我想得到这个值,然后做一些透视。 换句话说:
公共类DrawView扩展视图{
点1,点3;
点2,点4;
/**
*点1和点3属于同一组,与点2和点4相同
*/
int-groupId=-1;
private ArrayList colorbolls=new ArrayList();
//容纳球的阵列
私有整数balID=0;
//变量以了解正在拖动的球
油漆;
帆布;
公共绘图视图(上下文){
超级(上下文);
油漆=新油漆();
setFocusable(true);//获取触摸事件所必需的
画布=新画布();
//设置球的起点
点1=新点();
点1.x=50;
点1.y=20;
点2=新点();
点2.x=150;
点2.y=20;
点3=新点();
点3.x=150;
点3.y=120;
点4=新点();
点4.x=50;
点4.y=120;
//使用ColorBall类声明每个球
添加(新的颜色球(上下文,R.drawable.gray_圆,点1));
添加(新的颜色球(上下文,R.drawable.gray_圆,点2));
添加(新的颜色球(上下文,R.drawable.gray_圆,第3点));
添加(新的颜色球(上下文,R.drawable.gray_圆,第4点));
}
公共绘图视图(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
公共绘图视图(上下文、属性集属性){
超级(上下文,attrs);
油漆=新油漆();
setFocusable(true);//获取触摸事件所必需的
画布=新画布();
//设置球的起点
点1=新点();
点1.x=50;
点1.y=20;
点2=新点();
点2.x=150;
点2.y=20;
点3=新点();
点3.x=150;
点3.y=120;
点4=新点();
点4.x=50;
点4.y=120;
//使用ColorBall类声明每个球
添加(新的颜色球(上下文,R.drawable.gray_圆,点1));
添加(新的颜色球(上下文,R.drawable.gray_圆,点2));
添加(新的颜色球(上下文,R.drawable.gray_圆,第3点));
添加(新的颜色球(上下文,R.drawable.gray_圆,第4点));
}
//画球的方法
@凌驾
受保护的void onDraw(画布){
//canvas.drawColor(0xFFCCCC);//如果需要其他背景色
paint.setAntiAlias(真);
绘制。设置抖动(真);
paint.setColor(Color.parseColor(#55000000”);
绘制.设置样式(绘制.样式.填充);
绘制.设置行程连接(绘制.连接.圆形);
//mPaint.setStrokeCap(油漆盖圆形);
油漆。设置行程宽度(5);
帆布.拉丝漆(油漆);
paint.setColor(Color.parseColor(#55FFFFFF”);
if(groupId==1){
canvas.drawRect(point1.x+colorbolls.get(0).getWidthOfBall()/2,
point3.y+colorbolls.get(2.getWidthOfBall()/2,point3.x
+colorbolls.get(2.getWidthOfBall()/2,point1.y
+colorbolls.get(0.getWidthOfBall()/2,paint);
}否则{
canvas.drawRect(point2.x+colorbolls.get(1).getWidthOfBall()/2,
point4.y+colorbolls.get(3.getWidthOfBall()/2,point4.x
+colorbolls.get(3.getWidthOfBall()/2,point2.y
+colorbolls.get(1.getWidthOfBall()/2,paint);
}
位图可绘制mBitmap;
mBitmap=新的BitmapDrawable();
//在画布上画球
用于(彩色球:彩色球){
canvas.drawBitmap(ball.getBitmap(),ball.getX(),ball.getY(),
新油漆());
}
}
//触摸屏幕时发生的事件
公共布尔onTouchEvent(运动事件){
int eventaction=event.getAction();
int X=(int)event.getX();
int Y=(int)event.getY();
开关(事件操作){
case MotionEvent.ACTION_DOWN://touch DOWN,检查手指是否已打开
//球
balID=-1;
groupId=-1;
用于(彩色球:彩色球){
//检查是否在球的边界内(圆)
//抓住球的中心
Utils.logd(“Id:+ball.getID());
logd(“getX:+ball.getX()+”getY():“+ball.getY());
int centerX=ball.getX()+ball.getWidthOfBall();
int centerY=ball.getY()+ball.getHeightOfBall();
油漆.setColor(颜色为青色);
//计算从触球到球中心的半径
双圆=数学
.sqrt((双)((centerX-X)*(centerX-X))+(centerY-Y)
*(Y-Y);;
public class DrawView extends View {
Point point1, point3;
Point point2, point4;
/**
* point1 and point 3 are of same group and same as point 2 and point4
*/
int groupId = -1;
private ArrayList<ColorBall> colorballs = new ArrayList<ColorBall>();
// array that holds the balls
private int balID = 0;
// variable to know what ball is being dragged
Paint paint;
Canvas canvas;
public DrawView(Context context) {
super(context);
paint = new Paint();
setFocusable(true); // necessary for getting the touch events
canvas = new Canvas();
// setting the start point for the balls
point1 = new Point();
point1.x = 50;
point1.y = 20;
point2 = new Point();
point2.x = 150;
point2.y = 20;
point3 = new Point();
point3.x = 150;
point3.y = 120;
point4 = new Point();
point4.x = 50;
point4.y = 120;
// declare each ball with the ColorBall class
colorballs.add(new ColorBall(context, R.drawable.gray_circle, point1));
colorballs.add(new ColorBall(context, R.drawable.gray_circle, point2));
colorballs.add(new ColorBall(context, R.drawable.gray_circle, point3));
colorballs.add(new ColorBall(context, R.drawable.gray_circle, point4));
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
setFocusable(true); // necessary for getting the touch events
canvas = new Canvas();
// setting the start point for the balls
point1 = new Point();
point1.x = 50;
point1.y = 20;
point2 = new Point();
point2.x = 150;
point2.y = 20;
point3 = new Point();
point3.x = 150;
point3.y = 120;
point4 = new Point();
point4.x = 50;
point4.y = 120;
// declare each ball with the ColorBall class
colorballs.add(new ColorBall(context, R.drawable.gray_circle, point1));
colorballs.add(new ColorBall(context, R.drawable.gray_circle, point2));
colorballs.add(new ColorBall(context, R.drawable.gray_circle, point3));
colorballs.add(new ColorBall(context, R.drawable.gray_circle, point4));
}
// the method that draws the balls
@Override
protected void onDraw(Canvas canvas) {
// canvas.drawColor(0xFFCCCCCC); //if you want another background color
paint.setAntiAlias(true);
paint.setDither(true);
paint.setColor(Color.parseColor("#55000000"));
paint.setStyle(Paint.Style.FILL);
paint.setStrokeJoin(Paint.Join.ROUND);
// mPaint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(5);
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#55FFFFFF"));
if (groupId == 1) {
canvas.drawRect(point1.x + colorballs.get(0).getWidthOfBall() / 2,
point3.y + colorballs.get(2).getWidthOfBall() / 2, point3.x
+ colorballs.get(2).getWidthOfBall() / 2, point1.y
+ colorballs.get(0).getWidthOfBall() / 2, paint);
} else {
canvas.drawRect(point2.x + colorballs.get(1).getWidthOfBall() / 2,
point4.y + colorballs.get(3).getWidthOfBall() / 2, point4.x
+ colorballs.get(3).getWidthOfBall() / 2, point2.y
+ colorballs.get(1).getWidthOfBall() / 2, paint);
}
BitmapDrawable mBitmap;
mBitmap = new BitmapDrawable();
// draw the balls on the canvas
for (ColorBall ball : colorballs) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(),
new Paint());
}
}
// events when touching the screen
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int) event.getX();
int Y = (int) event.getY();
switch (eventaction) {
case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on
// a ball
balID = -1;
groupId = -1;
for (ColorBall ball : colorballs) {
// check if inside the bounds of the ball (circle)
// get the center for the ball
Utils.logd("Id : " + ball.getID());
Utils.logd("getX : " + ball.getX() + " getY() : " + ball.getY());
int centerX = ball.getX() + ball.getWidthOfBall();
int centerY = ball.getY() + ball.getHeightOfBall();
paint.setColor(Color.CYAN);
// calculate the radius from the touch to the center of the ball
double radCircle = Math
.sqrt((double) (((centerX - X) * (centerX - X)) + (centerY - Y)
* (centerY - Y)));
Utils.logd("X : " + X + " Y : " + Y + " centerX : " + centerX
+ " CenterY : " + centerY + " radCircle : " + radCircle);
if (radCircle < ball.getWidthOfBall()) {
balID = ball.getID();
Utils.logd("Selected ball : " + balID);
if (balID == 1 || balID == 3) {
groupId = 2;
canvas.drawRect(point1.x, point3.y, point3.x, point1.y,
paint);
} else {
groupId = 1;
canvas.drawRect(point2.x, point4.y, point4.x, point2.y,
paint);
}
invalidate();
break;
}
invalidate();
}
break;
case MotionEvent.ACTION_MOVE: // touch drag with the ball
// move the balls the same as the finger
if (balID > -1) {
Utils.logd("Moving Ball : " + balID);
colorballs.get(balID).setX(X);
colorballs.get(balID).setY(Y);
paint.setColor(Color.CYAN);
if (groupId == 1) {
colorballs.get(1).setX(colorballs.get(0).getX());
colorballs.get(1).setY(colorballs.get(2).getY());
colorballs.get(3).setX(colorballs.get(2).getX());
colorballs.get(3).setY(colorballs.get(0).getY());
canvas.drawRect(point1.x, point3.y, point3.x, point1.y,
paint);
} else {
colorballs.get(0).setX(colorballs.get(1).getX());
colorballs.get(0).setY(colorballs.get(3).getY());
colorballs.get(2).setX(colorballs.get(3).getX());
colorballs.get(2).setY(colorballs.get(1).getY());
canvas.drawRect(point2.x, point4.y, point4.x, point2.y,
paint);
}
invalidate();
}
break;
case MotionEvent.ACTION_UP:
// touch drop - just do things here after dropping
break;
}
// redraw the canvas
invalidate();
return true;
}
public void shade_region_between_points() {
canvas.drawRect(point1.x, point3.y, point3.x, point1.y, paint);
}
}