自定义ImageView点击放大查看

import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.rb.ejex.R;

import java.io.InputStream;

/**
 * Created by zzw on 2017/12/13 0013.
 */

public class AlertImageView extends ImageView
{
    private Context context;
    ImageView imgView = this;
    private String mUrl = "";

    public AlertImageView(Context context) {
        super(context);
        this.context = context;
        seeBigImg();
    }

    public AlertImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        seeBigImg();
    }

    public AlertImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        seeBigImg();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyleAttr, 0);
        this.mUrl = a.getString(R.styleable.AlertImageView_url);
    }


    private void seeBigImg() {
// TODO Auto-generated method stub
        imgView.setOnClickListener(new OnClickListener() { // 点击放大
            public void onClick(View paramView) {
                final AlertDialog dialog = new AlertDialog.Builder(context, R.style.AlertImage).create();
                dialog.show();
                dialog.setContentView(R.layout.layout_dialog_img);

                ImageView img1 = (ImageView)dialog.findViewById(R.id.image);

                //显示图片的配置
                DisplayImageOptions options = new DisplayImageOptions.Builder()
                        .showImageOnLoading(R.drawable.img_loading)
                        .showImageOnFail(R.drawable.img_load_error)
                        .cacheInMemory(false)
                        .cacheOnDisk(false)
                        .bitmapConfig(Bitmap.Config.RGB_565)
                        .build();
                ImageLoader.getInstance().displayImage(getUrl(), img1, options);

                //选择true的话点击其他地方可以使dialog消失,为false的话不会消失
                dialog.setCanceledOnTouchOutside(true); // Sets whether this dialog is

                Window window = dialog.getWindow();
                window.getDecorView().setPadding(0, 0, 0, 0);
                window.setGravity(Gravity.CENTER);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.FILL_PARENT;
                lp.height = WindowManager.LayoutParams.FILL_PARENT;
                window.setAttributes(lp);


                // 点击布局文件(也可以理解为点击大图)后关闭dialog,这里的dialog不需要按钮
                img1.setOnClickListener(new OnClickListener() {
                    public void onClick(View paramView) {
                        dialog.cancel();
                    }
                });
            }
        });
    }

    public String getUrl() {
        return mUrl;
    }

    public void setUrl(String mUrl) {
        this.mUrl = mUrl;
    }
}

styles.xml

<style name="AlertImage">
        <item name="android:windowBackground">@color/transparent_55</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>

attrs.xml

<!-- 弹出查看大图 -->
    <declare-styleable name="AlertImageView">
        <attr name="url" format="string" />
    </declare-styleable>

使用的时候需要设置控件的url属性,或动态设置

猜你喜欢

转载自blog.csdn.net/qq_26075861/article/details/79744728