简体   繁体   English

如何使矩形在Android中绘制onTouchEvent?

[英]How to make rectangles draw onTouchEvent in Android?

I have been working on game which simply creates a rectangle when user touches screen, I don't know how to get the X coordinates and Y coordinates of touchEvent to be the "float" of my rects, every time I put a float number in the X,Y coordinates it says "There is no applicable constructor in (float, float, int, int)". 我一直在做的游戏是在用户触摸屏幕时简单地创建一个矩形,我不知道如何在每次我将float数字放入其中时将touchEvent的X坐标和Y坐标设为矩形的“浮动”值。 X,Y坐标表示“(float,float,int,int)中没有适用的构造函数”。 Which I don't know what that means. 我不知道那是什么意思。

    public class GameBoard extends View {

    private ArrayList<Rect> rectangles = new ArrayList<Rect>();

    public GameBoard(Context context) {
        super(context);

    }
    @Override
    public boolean onTouchEvent (MotionEvent event) {
        float xCoor = event.getX();
        float yCoor = event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                rectangles.add(new Rect(xCoor, yCoor, 10, 40));
                break;
        }
        return true;
    }
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        for (Rect rect : rectangles) {
            canvas.drawRect(rect, paint);
        }
    }
}

You are using Rect for int values , 您正在使用Rect作为int值,

Instead you can use RectF for the float values 相反,您可以将RectF用作浮点值

new RectF(xCoor, yCoor, 10, 40)

see the Constructor of Rect 参见Rect的构造函数

 public Rect(int,int,int,int);

is expecting all parameters to be int 期望所有参数都为int

You need to typecast x and y values from float to int 您需要将x和y值从float转换为int

    int xCoor = (int)event.getX();
    int yCoor = (int)event.getY();
try to use this ::->     


 DrawingImage = (ImageView) this.findViewById(R.id.DrawingImageView1);

        Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
            .getDefaultDisplay().getWidth(), (int) getWindowManager()
            .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        DrawingImage.setImageBitmap(bitmap);

        // Draw Rectangle

        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(10);

        float left = 20;
        float top = 20;
        float right = 50;
        float bottom = 100;

        canvas.drawRect(left, top, right, bottom, paint);

You are adding a new Rect to your ArrayList by calling the constructor Rect. 您将通过调用构造函数Rect将新的Rect添加到ArrayList中。 new Rect tells Java to create a new Rect object. new Rect告诉Java创建一个新的Rect对象。

A constructor is like a method, except it has the same name as the Class it's in. It just so happens that the Rect class does not have a constructor with floats as its parameters. 构造函数就像方法一样,只是它的名称与所在类的名称相同。碰巧这种情况是Rect类没有以浮点数作为参数的构造函数。

So you have to convert your floats to integer before passing them to the Rect constructor. 因此,您必须先将浮点数转换为整数,然后再将其传递给Rect构造函数。

int xCoor = (int) event.getX();
int yCoor = (int) event.getY();

Notice also that when you cast (casting is converting from one type to another) that the value in only ever rounded down to the nearest integer. 还要注意,当您进行转换(广播从一种类型转换为另一种类型)时,该值只会四舍五入到最接近的整数。 In fact anything after the decimal point is simply discarded. 实际上,小数点后的所有内容都将被丢弃。

I think if your going to make a success of your Android developments you would be best served getting a good textbook on Object Oriented programming in Java. 我认为,如果您要成功实现Android开发,那么最好获得一本有关Java面向对象编程的好教科书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM