见下图
我想通过拖动它的角来更改突出显示视图的形状.. 拖动角后的视图不需要是矩形它可以是任何任意形状
如何做到这一点?
现在我已经完成了触摸事件中可移动的 4 个角,但形状没有改变
package com.assignment.DragDrop;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class DrawView extends View {
Point point1,point2,point3,point4;
private ColorBall[] colorballs = new ColorBall[4]; // 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[0] = new ColorBall(context,R.drawable.bol_blauw, point1);
colorballs[1] = new ColorBall(context,R.drawable.bol_blauw, point2);
colorballs[2] = new ColorBall(context,R.drawable.bol_groen, point3);
colorballs[3] = new ColorBall(context,R.drawable.bol_geel, 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.BLACK);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeJoin(Paint.Join.ROUND);
// mPaint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(5);
canvas.drawPaint(paint);
paint.setColor(Color.WHITE);
canvas.drawRect(point1.x+25, point2.x, point3.x+25, point4.x, paint);
BitmapDrawable mBitmap;
mBitmap= new BitmapDrawable();
// canvas.drawBitmap(bitmap, left, top, paint)
// shade_region_between_points();
// canvas.drawLine(point1.x, point1.y, point2.x, point2.y, paint);
// canvas.drawLine(point1.x, point1.y, point4.x, point4.y, paint);
// canvas.drawLine(point4.x, point4.y, point3.x, point3.y, paint);
// canvas.drawLine(point2.x, point2.y, point3.x, point3.y, paint);
//draw the balls on the canvas
for (ColorBall ball : colorballs) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), paint);
// canvas.drawLine(point1.x, point1.y, point2.x, point2.y, paint);
// canvas.drawRect(point1.x, point2.x, point3.x, point4.x, paint);
}
/*Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
drawable.setBounds( point1.x+25, point2.x, point3.x+25, point4.x);
drawable.draw(canvas);*/
}
// 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 = 0;
for (ColorBall ball : colorballs) {
// check if inside the bounds of the ball (circle)
// get the center for the ball
int centerX = ball.getX() + 25;
int centerY = ball.getY() + 25;
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)));
// canvas.drawLine(point1.x, point1.y, point2.x, point2.y, paint);
// if the radius is smaller then 23 (radius of a ball is 22), then it must be on the ball
if (radCircle < 23){
balID = ball.getID();
canvas.drawRect(point1.x, point2.x, point3.x, point4.x, paint);
invalidate();
break;
}
invalidate();
//canvas.drawRect(point1.x, point2.x, point3.x, point4.x, paint);
// check all the bounds of the ball (square)
//if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
// balID = ball.getID();
// break;
//}
}
break;
case MotionEvent.ACTION_MOVE: // touch drag with the ball
// move the balls the same as the finger
if (balID > 0) {
try {
colorballs[balID-1].setX(X-25);
colorballs[balID-1].setY(Y-25);
// canvas.drawLine(point1.x, point1.y, point2.x, point2.y, paint);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
paint.setColor(Color.CYAN);
canvas.drawRect(point1.x, point2.x, point3.x, point4.x, 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, point2.x, point3.x, point4.x, paint);
}
}
我从互联网上得到了这个代码。
这将能够在触摸事件上移动角落,但形状不会移动......