简体   繁体   English

如何在ImageView中单击全屏显示图像?

[英]How to display an image in full screen on click in the ImageView?

I need to open an image in full screen upon click in the ImageView, like a gallery with one image! 我需要在ImageView中单击时全屏打开图像,就像一个带有一个图像的图库! How would I do that? 我该怎么办?

this is a basic example: 这是一个基本的例子:

the layout activity_main.xml 布局activity_main.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher"/>

    </LinearLayout>

onCreate() method: onCreate()方法:

private boolean zoomOut =  false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ImageView imageView  = (ImageView)findViewById(R.id.imageView1);
    imageView .setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(zoomOut) {
                Toast.makeText(getApplicationContext(), "NORMAL SIZE!", Toast.LENGTH_LONG).show();
                imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                imageView.setAdjustViewBounds(true);
                zoomOut =false;
            }else{
                Toast.makeText(getApplicationContext(), "FULLSCREEN!", Toast.LENGTH_LONG).show();
                imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                zoomOut = true;
            }
        }                   
    });
}

Jorgesys's answer is good, but to make the image really full screen, a better way would be to make a new dialog/activity with .NoActionBar.Fullscreen theme. Jorgesys的答案很好,但为了使图像真正全屏,更好的方法是使用.NoActionBar.Fullscreen主题制作新的对话/活动。 For example, 例如,

<style name="FullScreenDialogTheme" parent="android:Theme.Material.NoActionBar.Fullscreen"/>

This new dialog would contain just an image view for your image. 此新对话框仅包含图像的图像视图。

Then you would pass the drawable, or a reference to drawable from your activity to this new one via an intent. 然后你可以通过意图将drawable或者drawable的引用从你的活动传递给这个新的。 Check this question for more details on that. 有关详细信息,请查看问题。 (I would suggest passing via some reference, for efficiency concerns). (出于效率考虑,我建议通过一些参考)。

Try this code.. 试试这个代码..

imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        imageView.setVisibility(View.GONE);
        imageView2.setVisibility(View.VISIBLE);
        Glide.with(this).load(imagePath).into(imageView2);

    }
});

xml code.. xml代码..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher"
        />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher"
        android:visibility="gone"
        />
</LinearLayout>

and also used glide then add below dependecy into app level gradle file. 并使用glide然后将下面的依赖添加到app level gradle文件中。

implementation 'com.github.bumptech.glide:glide:4.7.1'

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

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