安卓简洁地实现点击ImageView查看大图功能

我是在fragment里面实现查看大图功能,不过放到activity里面也一样
查看大图地功能是由dialog实现

先看看效果
安卓简洁地实现点击ImageView查看大图功能_第1张图片
安卓简洁地实现点击ImageView查看大图功能_第2张图片
实现方式很简单,可以直接复制粘贴

//方法里直接实例化一个imageView不用xml文件,传入bitmap设置图片
    private void bigImageLoader(Bitmap bitmap){
        final Dialog dialog = new Dialog(getActivity());
        ImageView image = new ImageView(getContext());
        image.setImageBitmap(bitmap);
        dialog.setContentView(image);
		//将dialog周围的白块设置为透明
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        //显示
        dialog.show();
        //点击图片取消
        image.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                dialog.cancel();
            }
        });
    }

完整版代码

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.io.IOException;

import okhttp3.Request;


public class page2 extends Fragment {
    private ImageView imageSmall;
    private View view;

    public page2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_page2, container, false);
        imageSmall = (ImageView)view.findViewById(R.id.image_small);
        //下面一段代码是自己封装的OkHttp的代码,不是重点,重点是设置小图的bitmap
        String url = "https://img9.51tietu.net/pic/2019-091402/cw0pcs4zbcjcw0pcs4zbcj.jpg";
        OkHttpUtil.loadAsync(url, new OkHttpUtil.DataCallback() {
            @Override public void requestFailure(Request request, Exception e) {}
            @Override public void requestStringSuccess(String result) throws IOException {}
            @Override public void requestBitmapSuccess(Bitmap bitmap) throws IOException {
                imageSmall.setImageBitmap(bitmap);
            }
        });
        imageSmall.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
            //获取ImageView的Bitmap
                bitmap0 = ((BitmapDrawable)imageSmall.getDrawable()).getBitmap();
                bigImageLoader(bitmap0);
            }
        });
        return view;
    }



    private void bigImageLoader(Bitmap bitmap){
        final Dialog dialog = new Dialog(getActivity());
        ImageView image = new ImageView(getContext());
        image.setImageBitmap(bitmap);
        dialog.setContentView(image);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.show();
        image.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                dialog.cancel();
            }
        });
    }

}

你可能感兴趣的:(安卓,android)