Помещение одного изображения под другое в android
Не знаю на сколько я адекватно выразился в заголовке, но проблема такая: mImageSight должен быть всегда поверх других изображений, но получается так, что изображение в анимации mAnimImg накладывается поверх mImageSight. При добавлении картинки из галереи, она накрывает все остальные изображения. Как можно отрегулировать все так, чтобы mImageSight был всегда в самом верхнем слое, посередине mAnimImg и нижним слоем была картинка из галереи. И заодно еще один вопрос задам, как сделать неограниченное добавление изображений с удалением старого? Получается так, что у меня вылетает приложение при попытке загрузить новое изображение. Заранее спасибо за ответ!
код:
public class GameActivity extends Activity implements View.OnTouchListener {
private ImageView mImageSight;
private ViewGroup mMoveSight;
private ImageView mAnimImg;
private TranslateAnimation transfer;
private float mX;
private float mY;
private float xX;
private float yY;
static final int GALLERY_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
}
});
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
xX = metrics.widthPixels;
yY = metrics.heightPixels;
mMoveSight = (ViewGroup) findViewById(R.id.move_sight);
mImageSight = (ImageView) findViewById(R.id.sight);
RelativeLayout.LayoutParams lParams = new RelativeLayout.LayoutParams(400, 400);
mImageSight.setLayoutParams(lParams);
mImageSight.setOnTouchListener(this);
mAnimImg = (ImageView) findViewById(R.id.thing);
Button animator = (Button) findViewById(R.id.anima);
animator.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("checkk", "Animation started");
mAnimImg.startAnimation(transfer);
}
});
}
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
mX = X - lParams.leftMargin;
mY = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = (int) (X - mX);
layoutParams.topMargin = (int) (Y - mY);
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
transfer = new TranslateAnimation(0, (X - mX - xX + 285), 0, (Y - mY - yY + 345));
transfer.setDuration(2000);
transfer.setFillAfter(true);
break;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
Bitmap bitmap = null;
ImageView imageView = (ImageView) findViewById(R.id.imageView);
switch(requestCode) {
case GALLERY_REQUEST:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
Log.d("checkk", "Image set");
}
}
}
}
xml:
<androidx.constraintlayout.widget.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=".GameActivity">
<RelativeLayout
android:id="@+id/move_sight"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/sight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sight_ready_w"
tools:ignore="MissingConstraints" />
</RelativeLayout>
<ImageView
android:id="@+id/thing"
android:
android:layout_width="150px"
android:layout_height="150px"
android:layout_alignParentEnd="true"
android:src="@drawable/thing_ready"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside"/>
<Button
android:id="@+id/anima"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="132sp"
android:layout_marginBottom="16sp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28sp"
android:layout_marginBottom="16sp"
android:text="GetImg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>