0

I'm trying to make the image display full screen when imageView is clicked.

          imageView.setOnClickListener(new View.OnClickListener() {
               @Override
                    public void onClick (View v) {
                    if (isImageFitToScreen) {
                        isImageFitToScreen = false;
                        imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                        imageView.setAdjustViewBounds(true);
                        imageView.setScaleType(ImageView.ScaleType.CENTER);
                    } else {
                        isImageFitToScreen = true;
                        imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    }
                }
            });

xml

                <ImageView
                android:layout_gravity="center"
                android:layout_width="214dp"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="fitXY"
                android:src="@mipmap/no_image"
                android:id="@+id/imageView"
                android:layout_weight="0.19" />

Before the imageView is clicked, the imageView is in center. when it clicked and then back to normal size, the size of imageView become smaller and it moves to left-hand side. How do I make the size remains the same as normal (before zoom in) and fix imageView in center ?

Tony
  • 2,515
  • 14
  • 38
  • 71

2 Answers2

1

Use ViewGroup.LayoutParams variable to store the default LayoutParams of your imageview and use it with Imageview clickListener like this.

 isImageFitToScreen=false;
    final ViewGroup.LayoutParams layoutParams=imageView.getLayoutParams();

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isImageFitToScreen) {
                        isImageFitToScreen = false;
                        imageView.setLayoutParams(layoutParams);
                      } else {
                        isImageFitToScreen = true;
                        imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                         imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                       }
                }
            });
HourGlass
  • 1,805
  • 1
  • 15
  • 29
1

Well, first you need to save the width and height of the imageview, and then you need to get the width and height of the layout or relative layout or the size of the screen that containing the imageview

*LinearLayout,relativeLayout(container), you need to put in xml,

center the content

android:layout_gravity="center"

*to get width and height of imageview or layout check this link

1.- get size of imageview, and container

2.- expand and contrac the imageview, if(validate==true) imageview=sizebig else imageview=sizeshort

Note: if image don't center, check this link to center the imageview if the imageview don't center, you try to cententer the content of the linearlayout or relativelayout(contariner)

Community
  • 1
  • 1
DarckBlezzer
  • 4,578
  • 1
  • 41
  • 51