Android:使用“onClick”事件的标题全屏显示图像

时间:2017-08-16 05:18:17

标签: java android android-layout android-imageview picasso

我是android开发的初学者。我有一个Android图像视图,使用Picasso 2.5.2库导入Web图像。它正在工作。单击该imageview后,我想创建一个带有该图像的全屏Android对话框。我&#39;使用以下代码。但是在单击图像视图后,将显示一个没有全屏的对话框。最后,我想放大并缩小全屏对话框。我想在imageview onClickListener上使用这种全屏。另请参阅下面的图像如何有标题,我还需要图像标题(如在Facebook中)<​​/ p>

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>

3 个答案:

答案 0 :(得分:1)

您可以使用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);
    }
}

详细活动布局,

<?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>
  

在我的示例中,我使用Glide而非Picaso,因为Android   官方推荐是Glide,如果您愿意,可以改变它。

答案 1 :(得分:0)

将图像设置为居中裁剪并使用您的屏幕尺寸调整大小,如

Picasso.with(MainActivity.this)
            .load(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"))
            .noFade()
            .resize(800, 800)
            .centerCrop()
            .into(imageView);

答案 2 :(得分:0)

使用下面的xml进行布局,我再添加了一个ImageView,只需使用点击小图片来查看可见性

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />

</RelativeLayout>