введите описание изображения здесь Я пытаюсь создать приложение, в котором круг перемещается поверх случайного места на ImageView
. У меня есть заполнитель TextView
на карте, который должен указать кругу, куда идти. Проблема возникает, когда я собираю приложение на свой телефон и проверяю его. Круг и заполнитель textView находятся далеко не там, где я хотел, чтобы они были. и круг даже не поверх textView. Итак, вот вопрос: как я могу сохранить заполнитель в одном и том же месте на карте с каждым соотношением сторон и размером экрана И как я могу переместить круг поверх заполнителя?
Я искал ответ почти два дня и начинаю чувствовать себя немного разочарованным.
Вот мой код Java:
public class MainActivity extends AppCompatActivity {
Random random;
ImageView theMap;
ImageView pointerCircle;
int randomNum;
//PLACEHOLDERS
TextView place1;
TextView place2;
TextView place3;
TextView place4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
theMap = (ImageView) findViewById(R.id.theMap);
pointer = (ImageView) findViewById(R.id.pointer);
//DEFINING THE PLACEHOLDERS
place1 = (TextView) findViewById(R.id.place1);
place2 = (TextView) findViewById(R.id.place2);
place3 = (TextView) findViewById(R.id.place3);
place4 = (TextView) findViewById(R.id.place4);
random = new Random();
}
public void findSpot(View view) {
randomNum = random.nextInt(4) + 1;
shuffle(randomNum);
}
public void shuffle(final int theNum) {
switch (theNum) {
case 1:
pointer.setY(place1.getY());
pointer.setX(place1.getX());
case 2:
pointer.setY(place2.getY());
pointer.setX(place2.getX());
case 3:
pointer.setY(place3.getY());
pointer.setX(place3.getX());
case 4:
pointer.setY(place4.getY());
pointer.setX(place4.getX());
}
}
}
А вот и 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"
android:background="@android:color/black"
app:layout_constraintWidth_default="wrap"
app:layout_constraintHeight_default="wrap"
tools:context="com.example.aapotti.randomplaceonamap.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/button_border"
android:fontFamily="serif-monospace"
android:onClick="findSpot"
android:text="Find random spot!"
android:textAlignment="center"
android:textColor="@android:color/background_light"
android:textSize="20sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="493dp" />
<ImageView
android:id="@+id/pointer"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="184dp"
android:layout_marginTop="204dp"
android:adjustViewBounds="true"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/pointer" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/theMap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="w,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/normalmap">
</ImageView>
<TextView
android:id="@+id/placeholder"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="80dp"
android:layout_marginTop="90dp"
android:background="@android:color/white"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
</android.support.constraint.ConstraintLayout>