There is an ImageView. How to increase this ImageView by clicking on it and returning to the standard size after a second click?

Googling found such a code. It seems everything works, but after repeated pressing it does not return the original size of the ImageView.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="550dp" android:layout_height="350dp" android:layout_centerVertical="true" android:layout_centerHorizontal="true"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/btn3" android:adjustViewBounds="true" android:src="@drawable/history_button" android:layout_marginTop="250dp" /> </LinearLayout> 

MainActivity

 button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(isImageFitToScreen) { isImageFitToScreen=false; button3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); button3.setAdjustViewBounds(true); }else{ isImageFitToScreen=true; button3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); button3.setScaleType(ImageView.ScaleType.FIT_XY); } } }); 
  • You can just show the same image in the dialog, which will be full screen when clicked - Vladyslav Matviienko
  • And tried to do PropertyAnimator? it just changes the scaleFactor of the View element during sclae animation, there are two lines of code and it will look ok. - Shwarz Andrei

1 answer 1

 public class StackRu573629 extends AppCompatActivity { private boolean isImageScaled = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.stack_ru_573629); ImageView img_1 = (ImageView) findViewById(R.id.img_1); img_1.setOnClickListener(v -> { if (!isImageScaled) v.animate().scaleX(1.4f).scaleY(1.4f).setDuration(500); if (isImageScaled) v.animate().scaleX(1f).scaleY(1f).setDuration(500); isImageScaled = !isImageScaled; }); } } 

enter image description here

  • Thank you, but what if I have an image in the corner of the screen, and I want to enlarge it almost to the full screen? - Lev Naumenko