Android - 保存对象相对于屏幕尺寸的位置

时间:2017-11-06 07:48:21

标签: java android xml screen

我开发了一个简单的应用程序,可以帮助用户在线玩足球游戏并保存足球运动员在体育场内的位置。问题是我从屏幕保存对象的位置他们的位置根据设备屏幕尺寸更改

First image of players

Second image of players

这是我的MainActvitity.class

public class MainActivity extends AppCompatActivity {


private ViewGroup mainLayout;
private View footballer;

private int xDelta;
private int yDelta;

//339 133

private ArrayList<View> Footballer = new ArrayList<>();
private int[] IdOfPlayer = {
        R.id.footballer1,
        R.id.footballer2,
        R.id.footballer3,
        R.id.footballer4,
        R.id.footballer5,
        R.id.footballer6,
        R.id.footballer7,
        R.id.footballer8,
        R.id.footballer9,
        R.id.footballer10,
        R.id.footballer11,
};

private ArrayList<float[]> coordinatesOfPlayers = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


   // mainLayout = (RelativeLayout) findViewById(R.id.station);




    for (int id:IdOfPlayer) Footballer.add(findViewById(id));

    for (View v:Footballer) v.setOnTouchListener(new View.OnTouchListener() {
        PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
        PointF StartPT = new PointF(); // Record Start Position of 'img'

        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(final View v, MotionEvent event) {
            int eid = event.getAction();
            switch (eid) {
                case MotionEvent.ACTION_MOVE:



                    PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);

                    int NewXCoordinate =(int) (StartPT.x + mv.x);

                    int NewYCoordinate = (int)(StartPT.y + mv.y);

                    if (NewXCoordinate<0 ||  NewXCoordinate> dpToPx(300-25)|| NewYCoordinate<0 || NewYCoordinate > dpToPx(400-12)) break;

                    v.setX((int) (StartPT.x + mv.x));
                    v.setY((int) (StartPT.y + mv.y));
                    StartPT = new PointF(v.getX(), v.getY());

                    /*
                    if (v.getY() < -dpToPx(5) || v.getX() < -dpToPx(7) || v.getX() > dpToPx(300-25) || v.getY() > dpToPx(400-12)) {

                        v.setX(0);
                        v.setY(0);
                    }
                    */

                    break;
                case MotionEvent.ACTION_DOWN:

                    DownPT.x = event.getX();
                    DownPT.y = event.getY();
                    StartPT = new PointF(v.getX(), v.getY());

                    break;
                case MotionEvent.ACTION_UP:


                   // coordinatesOfPlayers.set(Footballer.indexOf(v),new float[]{v.getX(),v.getY()});


                    break;
                default:
                    break;
            }
            return true;
        }
    });

    for (int i  = 0;i<11;i++){
        setMargins(Footballer.get(i),i*50,i*70);
    }

}

public static void setMargins(View v, int l, int t) {

    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) v.getLayoutParams();
    lp.setMargins(l, t, 0, 0);
    v.setLayoutParams(lp);

}

public int dpToPx(int dp) {
    DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
    return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

这是我的XML文件。

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tehedligmail.dragdrop.MainActivity">


    <FrameLayout
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:background="@android:color/holo_green_light"
        android:layout_gravity="center"
        android:id="@+id/station">

        <TextView
            android:id="@+id/footballer1"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_gravity="top|start"
            android:background="@android:color/holo_red_dark"
            android:text="1"
            />

        <TextView
            android:id="@+id/footballer2"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_gravity="top|start"
            android:background="@android:color/holo_red_dark"
            android:text="2"
            />

        <TextView
            android:id="@+id/footballer3"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_gravity="top|start"
            android:background="@android:color/holo_red_dark"
            android:text="3"
            />

        <TextView
            android:id="@+id/footballer4"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_gravity="top|start"
            android:background="@android:color/holo_red_dark"
            android:text="4"
            />

        <TextView
            android:id="@+id/footballer5"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_gravity="top|start"
            android:background="@android:color/holo_red_dark"
            android:text="5"
            />

        <TextView
            android:id="@+id/footballer6"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_gravity="top|start"
            android:background="@android:color/holo_red_dark"
            android:text="6"
            />

        <TextView
            android:id="@+id/footballer7"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_gravity="top|start"
            android:background="@android:color/holo_red_dark"
            android:text="7"
            />

        <TextView
            android:id="@+id/footballer8"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_gravity="top|start"
            android:background="@android:color/holo_red_dark"
            android:text="8"
            />

        <TextView
            android:id="@+id/footballer9"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_gravity="top|start"
            android:background="@android:color/holo_red_dark"
            android:text="9"
            />

        <TextView
            android:id="@+id/footballer10"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_gravity="top|start"
            android:background="@android:color/holo_red_dark"
            android:text="10"
            />

        <TextView
            android:id="@+id/footballer11"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_gravity="top|start"
            android:background="@android:color/holo_red_dark"
            android:text="11"
            />

    </FrameLayout>


</FrameLayout>
&#13;
&#13;
&#13;

欢迎所有建议,提前致谢...

0 个答案:

没有答案