簡體   English   中英

我如何在Android Studio中繪制多個矩形並移動它們(通過觸摸),而無需分別對每個矩形進行編程

[英]How do I draw multiple rectangles in Android Studio and move them (with touch) without individually programming each rectangle

我目前正在嘗試編寫一個應用程序,該應用程序允許您將(紅色)矩形從左下角移動到所需的任何位置。 釋放第一個矩形后,應該可以將左下角的另一個矩形移動到所需的任意位置,並可以進行任意多次。 在下面的代碼中,您最多只能有3個可移動矩形,因為我為每個新矩形手動創建了單獨的變量。 有沒有一種方法可以在需要更多變量時自動執行此操作,以便您可以擁有任意數量的矩形?

是的,我已經在尋找解決此問題的方法,但是它非常具體,所以我沒有找到任何可以利用的方法。 我是一個初學者,所以我的代碼有點混亂。 抱歉

這是我的代碼(重要的應該是DrawView.java)

DrawView.java:

public class DrawView extends View {
    public static float xpos = -1;
    public static float ypos = -1;
    public static float xpos1 = -1;
    public static float ypos1 = -1;
    public static float xpos2 = -1;
    public static float ypos2 = -1;
    public static float xpos3 = -1;
    public static float ypos3 = -1;
    public int clicked = 0;
    public int uses = 0;
    Paint paint = new Paint();

    DisplayMetrics metrics = this.getResources().getDisplayMetrics();
    public int width = metrics.widthPixels;
    public int height = metrics.heightPixels;

    public DrawView(Context c) {
        super(c);
        setFocusable(true);
    }

    @Override
    public void onDraw(Canvas canvas) {
        if (xpos == -1 && ypos == -1) {
            xpos = 70;
            ypos = height - 90;
            xpos1 = xpos;
            ypos1 = ypos;
            xpos2 = xpos;
            ypos2 = ypos;
            xpos3 = xpos;
            ypos3 = ypos;
        }

        paint.setStrokeWidth(0);
        paint.setColor(Color.GREEN);
        canvas.drawRect(50, 50, 70+(uses*20), 70+(clicked*20), paint); //for testing
        paint.setColor(Color.GRAY);
        canvas.drawRect(0, height-150, 800, height, paint);
        paint.setColor(Color.LTGRAY);
        canvas.drawRect(0, height-160, 800, height-150, paint);
        paint.setColor(Color.RED);
        canvas.drawRect(xpos-50, ypos-50, xpos+50, ypos+50, paint);

        if (uses >= 0) {
            canvas.drawRect(xpos1 - 50, ypos1 - 50, xpos1 + 50, ypos1 + 50, paint);
        }

        if (uses >= 1) {
            canvas.drawRect(xpos2-50, ypos2-50, xpos2+50, ypos2+50, paint);
        }

        if (uses >= 2) {
            canvas.drawRect(xpos3-50, ypos3-50, xpos3+50, ypos3+50, paint);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();

        if (action == MotionEvent.ACTION_DOWN && clicked == 0) {
            float cx = event.getX();
            float cy = event.getY();
            if (cx >= (xpos1 - 50) && cx <= (xpos1 + 50) && cy >= (ypos1 - 50) && cy <= (ypos1 + 50) && uses >= 0) {
                clicked = 1;
            }
            if (cx >= (xpos2 - 50) && cx <= (xpos2 + 50) && cy >= (ypos2 - 50) && cy <= (ypos2 + 50) && uses >= 1) {
                clicked = 2;
            }
            if (cx >= (xpos3 - 50) && cx <= (xpos3 + 50) && cy >= (ypos3 - 50) && cy <= (ypos3 + 50) && uses >= 2) {
                clicked = 3;
            }
        }

        if (action == MotionEvent.ACTION_MOVE && clicked == 1) {
            float dx = event.getX() - xpos1;
            float dy = event.getY() - ypos1;
            xpos1 += dx;
            ypos1 += dy;
        }
        if (action == MotionEvent.ACTION_UP && uses == 0 && clicked == 1) {
            uses += 1;
            clicked = 0;
        }
        else if (action == MotionEvent.ACTION_UP && clicked == 1) {
            clicked = 0;
        }

        if (action == MotionEvent.ACTION_MOVE && clicked == 2) {
            float dx = event.getX() - xpos2;
            float dy = event.getY() - ypos2;
            xpos2 += dx;
            ypos2 += dy;
        }
        if (action == MotionEvent.ACTION_UP && uses == 1 && clicked == 2) {
            uses += 1;
            clicked = 0;
        }
        else if (action == MotionEvent.ACTION_UP && clicked == 2) {
            clicked = 0;
        }

        if (action == MotionEvent.ACTION_MOVE && clicked == 3) {
            float dx = event.getX() - xpos3;
            float dy = event.getY() - ypos3;
            xpos3 += dx;
            ypos3 += dy;
        }
        if (action == MotionEvent.ACTION_UP && uses == 2 && clicked == 3) {
            uses += 1;
            clicked = 0;
        }
        else if (action == MotionEvent.ACTION_UP && clicked == 3) {
            clicked = 0;
        }

        if (xpos1 < 50) {
            xpos1 = 50;
        }
        if (xpos1 > width-50) {
            xpos1 = width-50;
        }
        if (ypos1 < 50) {
            ypos1 = 50;
        }
        if (ypos1 > height-75) {
            ypos1 = height-75;
        }

        invalidate();
        return true;
    }
}

MainActivity.java:

public class MainActivity extends Activity {
    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.DKGRAY);
        setContentView(drawView);

    }
}

activity_main.xml(此處未做任何更改):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="wuerthedv.rechteckverschieben2.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

您需要將變量放入數組並在處理它們時形成循環。 所以代替:

public static float xpos1 = -1;
public static float ypos1 = -1;
public static float xpos2 = -1;
public static float ypos2 = -1;
public static float xpos3 = -1;
public static float ypos3 = -1;

您可以創建一個點數組:

ArrayList<Point> arr_pos = new ArrayList<>();
arr_pos.add(new Point(-1,-1));
arr_pos.add(new Point(-1,-1));
arr_pos.add(new Point(-1,-1));

您還可以將xpos,ypos初始化為Point:

Point pos = new Point(-1,-1)

然后在onDraw方法中,可以遍歷數組:

if (pos.x == -1 && pos.y == -1) {
        pos.x = 70;
        pos.y = height - 90;
        for (int i=0; i<arr_pos.size(); i++){
            arr_pos.add(i, new Point(pox.x, pos.y);
        }
}

我會把剩下的事留給你...!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM