java - Android:在 `onClick` 事件上全屏显示带有标题的图像

标签 java android android-layout android-imageview picasso

我是android开发的初学者。我有一个 android ImageView ,它使用 Picasso 2.5.2 库导入 web 图像。它正在发挥作用。单击该 ImageView 后,我想使用该图像创建一个全屏 Android 对话框。我使用了以下代码。但是单击 ImageView 后,会显示一个对话框,但没有全屏。最后,我想放大和缩小全屏对话框。我想要在 imageview onClickListener 上实现这种全屏。另请参阅下面的图片如何具有标题,我还需要图片说明(就像 Facebook 中的一样)

enter image description here

这是我的代码

Mainactivity.java

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {

    ImageView imageView1;
    Context context;

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

        initUi();
    }

    public void initUi(){
        imageView1 = (ImageView)findViewById(R.id.image);
        Picasso.with(this)
                .load(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"))
                .into(imageView1);
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showImage();
            }
        });
    }

    public void showImage() {
        Dialog builder = new Dialog(this, android.R.style.Theme_Light);
        builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
        builder.getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                //nothing;
            }
        });

        ImageView imageView = new ImageView(this);
        Picasso.with(this)
                .load(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"))
                .into(imageView);
        builder.addContentView(imageView, new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        builder.show();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_add_info_other"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:text="Image buttons"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/image"/>


        </LinearLayout>

    </ScrollView>

</LinearLayout>

最佳答案

您可以使用Chrisbane's PhotoView lib用于缩放图像。

通过发送路径打开详情页,

public void showImage() {
    Intent intent = new Intent(this, ImageDeatilActvity.class);

    intent.putExtra("path", "http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640");

    startActivity(intent);
}

完整的ImageDeatilActvity.java

public class ImageDeatilActvity extends AppCompatActivity implements View.OnSystemUiVisibilityChangeListener {

    private boolean mIsFullScreen;

    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_image_deatil_actvity);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);

        upArrow.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
        getSupportActionBar().setHomeAsUpIndicator(upArrow);

        mIsFullScreen = true;

        String path = getIntent().getStringExtra("path");

        PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);

        photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
                updateView();
            }

            @Override
            public void onOutsidePhotoTap() {
                updateView();
            }
        });

        Glide.with(this).load(path).into(photoView);
    }

    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
            mIsFullScreen = false;
        }

        updateView();
    }

    public void updateView() {
        mIsFullScreen = !mIsFullScreen;

        if (mIsFullScreen) {
            hideSystemUI();
        } else {
            showSystemUI();
        }
    }

    private void hideSystemUI() {
        mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2)).start();
    }

    private void showSystemUI() {
        mToolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

详细 Activity 布局,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="in.muthu.stackoverflow.ImageDeatilActvity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true"
        android:background="#33000000"
        android:fitsSystemWindows="true"
        app:contentScrim="@android:color/transparent"
        app:popupTheme="@style/AppTheme.AppBarOverlay"/>

    <uk.co.senab.photoview.PhotoView
        android:id="@+id/photo_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

In My example, I am using Glide not Picaso, because Android official recommendation is Glide, If you want you can change that.

关于java - Android:在 `onClick` 事件上全屏显示带有标题的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45705592/

相关文章:

java - 使用反射读取类的字段时出错

android - 将文本(或字符)与用户在 edittext for android 中的输入相结合

android - 在 GridView 中的项目之间添加 View

android - 我的recyclerview不显示数据,仅加载进度条?

android - 可以在 RelativeLayout 内部的表面上使用 setFixedSize 吗?

Android 线性布局 - 如何将元素保持在 View 底部?

java - EasyMock API 中的 WTF 时刻?这个逻辑可能吗?

java - For循环从自定义ArrayList获取数据

android - 两个 Pane 布局 + DrawerLayout : replace main content

java - 对于 Java,不应该依赖于初始化。 Python 也是这样吗?