简体   繁体   English

单击图像时更改ImageView的大小

[英]Changing the size of an ImageView when image clicked

I am trying to get a picture, within an ImageView, to increase in size when clicked. 我正在尝试在ImageView中获取图片,以在单击时增大尺寸。 I have searched for a number of days, but have not found exactly what I need. 我搜索了几天,但没有确切找到我需要的东西。 At the moment I am using the TouchImageView from MikeOrtiz, and it works in that, when the image is clicked, it zooms in. But I want the image to increase in size, not just to zoom in, and while leaving the rest of the page opaque, the image needs to increase to the size of the screen while retaining its aspect ratio, ie it is a square image, so it should not become a rectangular image. 目前,我正在使用MikeOrtiz的TouchImageView,它的工作原理是:单击图像时,它会放大。但是我希望图像的大小增加,而不仅仅是放大,而剩下的其余部分如果页面不透明,则需要在保持其纵横比的同时将图像增大到屏幕尺寸,即它是正方形图像,因此不应成为矩形图像。

I have the following XML code: 我有以下XML代码:

<LinearLayout
<com.example.TouchImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="14dp"
    android:contentDescription="@string/CompanyHeader" 
    android:src="@drawable/entrancex250"/>

</LinearLayout>

I am using the TouchImageView from: 我从以下位置使用TouchImageView:

https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/ortiz/touch/TouchImageView.java https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/ortiz/touch/TouchImageView.java

As explained, what this does is to zoom in, but what I need is to make the image itself bigger when clicked, and then back to its normal size when clicked again. 如前所述,它的作用是放大图像,但我需要的是使图像本身在单击时变大,然后在再次单击时返回其正常大小。

Thanks very much. 非常感谢。

   public class TheCompany extends Activity implements OnMenuItemClickListener{

        public static final float screen_width;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.the_company);
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            screen_width = metrics.widthPixels;

        }

    }

Then in your page do this : 然后在您的页面中执行以下操作:

        ImageView imageView = (ImageView) findViewById(R.id.imageView2);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                float scale =  TheCompany.screen_width / view.getWidth();
                if(view.getScaleX() == 1) {
                    view.setScaleY(scale);
                    view.setScaleX(scale);
                }else{
                    view.setScaleY(1);
                    view.setScaleX(1);
                }
            }


        });

If all you need is to increase the size of the ImageView, then you should not use TouchImageView at all, but ImageView instead. 如果仅需要增加ImageView的大小,则根本不应该使用TouchImageView,而应使用ImageView。

<ImageView
    android:id="@+id/image_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/picture" 
    android:clickable="true" />

and in your java class setonclicklistener on this imageview and inside onclick change the size of the imageview by setting layout params. 并在此imageview和onclick内的java类setonclicklistener中,通过设置布局参数来更改imageview的大小。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(newWidth, newHeight);
imageView.setLayoutParams(params);

Here newWidth and newHeight are the dimensions to which to which you want set the imageview on click. 在这里,newWidth和newHeight是要在单击时设置imageview的尺寸。

Have a look at this great library : PhotoView 看看这个很棒的图书馆: PhotoView

It supports zooming and have a tap listener, so you just have to zoom on tap 它支持缩放并具有轻按监听器,因此您只需要轻按即可缩放

an alternative can be to use the following code 一种替代方法是使用以下代码

TouchImageView v = (TouchImageView) getActivity().findViewById("imageView2");
v.setHeight(fill_parent);
v.setWidth(fill_parent);

I wanted to put down what I actually ended up doing thanks to @micnubinub. 由于@micnubinub,我想放下我实际上最终要做的事情。 I put his answer as the correct one as they helped me get to where I needed to be. 我把他的答案作为正确的答案,因为它们帮助我到达了需要去的地方。 But the actual code is not in the answer, so I thought to put it here, in case it helps anyone else. 但是实际的代码不在答案中,因此我想将其放在此处,以防它对其他人有所帮助。

First of all @micnubinub suggested I used dialog to show the image when it gets clicked. 首先,@ micnubinub建议我使用对话框在单击图像时显示图像。 So I created a dialog.xml under res/layout as follows: 因此,我在res / layout下创建了一个dialog.xml,如下所示:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="910dp"
android:minWidth="910dp"
android:scaleType="fitCenter"
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

Then the Java class that would show the image when clicked is as follows: 然后,单击时将显示图像的Java类如下:

    final ImageView imageView = (ImageView) findViewById(R.id.imageView2);
    imageView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {

    final Dialog dialog = new Dialog(TheCompany.this, R.style.CustomDialog);
    if ((getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 
                Configuration.SCREENLAYOUT_SIZE_XLARGE) {
            // on a large screen device ...
        dialog.setContentView(R.layout.dialog_xlarge);}

        else dialog.setContentView(R.layout.dialog);


    ImageView im = (ImageView) dialog.findViewById(R.id.imageView);
    im.setImageResource(R.drawable.entrancex250);
    im.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    dialog.dismiss();
    }
    });
    dialog.show();
    Window window = dialog.getWindow();
    window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    }
    }); 
}

I also created a customdialog in Styles as follows: 我还在样式中创建了一个自定义对话框,如下所示:

<style name="CustomDialog" parent="@android:style/Theme.Translucent.NoTitleBar">
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowEnterAnimation">@anim/zoom_in</item>
<item name="android:windowExitAnimation">@anim/zoom_out</item>
</style>

This worked really well, thanks again @micnubinub. 这真的很好,再次感谢@micnubinub。

The dialog_xlarge is the same as dialog, except with larger height and width. 除了具有更大的高度和宽度之外,dialog_xlarge与对话框相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM