如何使视图在触摸事件中可调整大小

9

enter image description here

看下面的图片。

我想通过拖动它的角来改变高亮视图的形状。不一定要将视图拖动到矩形,可以是任意形状。

怎样实现这个目标呢?

现在我已经在触摸事件上做到了四个角可移动,但形状没有改变。

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);
    }
}

我从互联网上获取了这段代码。

它可以在触摸事件中移动角落,但形状并没有移动......


如果答案能帮助您解决问题,您会考虑接受它吗? - Tofeeq Ahmad
2个回答

1

以下代码用于基于触摸绘制矩形。

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.common.Utils;
import com.example.rectangleoverlay.R;

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);
    }
}

以下类用于存储对象。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;

public class ColorBall {

    Bitmap bitmap;
    Context mContext;
    Point point;
    int id;
    static int count = 0;

    public ColorBall(Context context, int resourceId, Point point) {
        this.id = count++;
        bitmap = BitmapFactory.decodeResource(context.getResources(),
                resourceId);
        mContext = context;
        this.point = point;
    }

    public int getWidthOfBall() {
        return bitmap.getWidth();
    }

    public int getHeightOfBall() {
        return bitmap.getHeight();
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    public int getX() {
        return point.x;
    }

    public int getY() {
        return point.y;
    }

    public int getID() {
        return id;
    }

    public void setX(int x) {
        point.x = x;
    }

    public void setY(int y) {
        point.y = y;
    }
}

0

就像我们裁剪图像时会增加矩形的大小一样。然后,我们可以将视图保存为新位图。因此,您可以从this question获得帮助。

这解释了几乎与您所需的相同的事情。

或者

如果是布局,则当我们触摸任何视图时,我们可以设置新的高度和宽度的新布局参数。


我不想裁剪图像,我想通过拖动角落来改变步骤。 - Dipali
一旦看到问题和答案,它支持拖动角落。 - Tofeeq Ahmad
请在这里解释一下,我无法理解。 - Dipali
请查看此教程:http://blogs.sonymobile.com/wp/2010/05/18/android-one-finger-zoom-tutorial-part-1/ - Tofeeq Ahmad
我测试了代码,但它正在调整图像视图的大小,我想要的是,如果我拖动一个角落,图像视图应该只从那个角落伸展,其他三个角落不应该移动,结果将是任意形状。如果您有任何想法,请告诉我。 - Dipali
你需要根据具体情况进行定制。我在此之后没有任何想法。 - Tofeeq Ahmad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接