如何在Android Studio中绘制多个矩形并移动它们(带触摸),而无需单独编程每个矩形

时间:2018-01-12 09:02:52

标签: java android

我目前正在尝试编写一个应用程序,允许您将(红色)矩形从左下角移动到您想要的任何位置。释放第一个矩形后,您应该可以根据需要随时将另一个从左下角移动到任何您想要的位置。使用我的以下代码,您最多只能有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>

1 个答案:

答案 0 :(得分:0)

当您想要处理变量时,需要将变量放入数组并形成循环。所以而不是:

func configureLocationServices(){
    if authorizationStatus == .notDetermined{
        locationManager.requestAlwaysAuthorization()
    }else{
        return}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

    centerMapOnUserLocation()
}

您可以创建一个点数组:

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;

您也可以将xpos,ypos初始化为Point:

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

然后在你的onDraw方法中,你可以遍历数组:

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

我会让你休息一下......!