首页 > 解决方案 > 单击时使 ImageView 全屏

问题描述

我有两个图像的视图

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/top_section"
        android:orientation="vertical"
        android:layout_marginTop="120dp">

    <ImageView
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:id="@+id/top_image"/>

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/top_text"/>
</LinearLayout>

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottom_section"
        android:orientation="vertical"
        android:layout_alignParentBottom="true">

    <ImageView
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:id="@+id/bottom_image"/>

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bottom_text"/>
</LinearLayout>

我想这样做,如果用户单击图像视图,它会变成全屏并正确旋转以占据全屏。

到目前为止,我已经尝试过(例如,如果单击顶部)

topText.setVisibility(View.GONE)
bottomSection.setVisibility(View.GONE)
getSupportActionBar().hide();
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

但图像并不是真正的全屏。我将如何使图像全屏显示?我一直在考虑可能有三分之一的图像视图,并使其与宽度和高度的父级匹配,并使其在点击时可见。

标签: androidandroid-layoutimageviewandroid-imageviewfullscreen

解决方案


创建另一个名为 的活动ImageZoomedActivity,通过意图(可能是 url、本地文件位置等)和OnCreate方法将图像数据传递给它:

requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.popup_photo_full);
    supportPostponeEnterTransition();
    if(getIntent().hasExtra("file")) {
        imageFile = new File(getIntent().getStringExtra("file"));
        GlideApp.with(this).asBitmap()
                .load(imageFile)
                .into(image);
        supportStartPostponedEnterTransition();
    }

上面的代码适用于通过意图传递的本地文件 URI。

这两行

requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

让您全屏进行活动。也可以在带有缩略图的第一个活动和带有全屏图像的第二个活动之间进行转换。如果您想了解更多信息,请给我发邮件,我可以将详细代码发送给您。


推荐阅读