一、创建CustomPopDialog2.java类
package com.example.explorer1;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
/**
* 该自定义Dialog应用在:弹出框居中显示图片,点击其他区域自动关闭Dialog
*
*/
public class CustomPopDialog2 extends Dialog {
public CustomPopDialog2(Context context) {
super(context);
}
public CustomPopDialog2(Context context, int theme) {
super(context, theme);
}
public static class Builder {
private Context context;
private Bitmap image;
private String serverAddr;
public Builder(Context context,String serverAddr) {
this.context = context;
this.serverAddr = serverAddr;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public CustomPopDialog2 create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final CustomPopDialog2 dialog = new CustomPopDialog2(context,R.style.Dialog);
View layout = inflater.inflate(R.layout.dialog_share_qrcode, null);
dialog.addContentView(layout, new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT
, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
/* LayoutParams lp = new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT
, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
lp.height = 10;
lp.width = 10;
dialog.addContentView(layout, lp);*/
dialog.setContentView(layout);
ImageView img = (ImageView)layout.findViewById(R.id.img_qrcode);//设置ImageView
TextView textView = (TextView)layout.findViewById(R.id.img_qrcode_content);//获取dialog_share_qrcode的TextView对象
textView.setText(serverAddr);//设置textView内容
img.setImageBitmap(getImage());
return dialog;
}
}
}
们自定义Dialog需要准备一个自己的View布局文件,主要关注create()方法即可,本例中就是直接显示一个图片。
二、自定义View的布局文件dialog_share_qrcode.xml、并在style.xml中添加theme
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:gravity="center"
android:id="@+id/rootLayout">
<ImageView
android:id="@+id/img_qrcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="二维码" />
</LinearLayout>
style.xml增加主题
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:background">#00000000</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
在mainActivity调用二维码显示
//FileInputStream fis = new FileInputStream("/storage/emulated/0/Pictures/qww.jpg");//显示本地图片
//Bitmap bitmap = BitmapFactory.decodeStream(fis);// 这里是获取图片Bitmap,也可以传入其他参数到Dialog中
String serverPort = null;
Object obj = new Utils().getConfig().get("serverPort");
if(obj!=null){
serverPort = obj.toString();
}
JSONArray array = new JSONArray();
String ip = "";
for (Map.Entry<String,String> entry : ipMap.entrySet()) {
if(!entry.getValue().equals("127.0.0.1")){
ip = entry.getValue();
array.add("http://"+ip+":"+serverPort);
}
}
//String serverAddr = "http://"+ip+":"+serverPort;
log.i("服务访问地址: "+JSON.toJSONString(array));
Bitmap bitmap = QRCodeUtil.createQRCodeBitmap( "http://"+ip+":"+serverPort , 350, 350 );//动态创建二维码位图
Context context = MainActivity.this;
CustomPopDialog2.Builder dialogBuild = new CustomPopDialog2.Builder(context,JSON.toJSONString(array));
dialogBuild.setImage(bitmap);
CustomPopDialog2 dialog = dialogBuild.create();
dialog.setCanceledOnTouchOutside(true);// 点击外部区域关闭
dialog.show();
最终效果图:
QRCodeUtil.java主要是创建bitmap位图
package com.example.explorer.utils;
import java.util.Hashtable;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
public class QRCodeUtil {
/**
* 创建二维码位图
*
* @param content 字符串内容(支持中文)
* @param width 位图宽度(单位:px)
* @param height 位图高度(单位:px)
* @return
*/
@Nullable
public static Bitmap createQRCodeBitmap(String content, int width, int height) {
return createQRCodeBitmap( content, width, height, "UTF-8", "H", "2", Color.BLACK, Color.WHITE );
}
/**
* 创建二维码位图 (支持自定义配置和自定义样式)
*
* @param content 字符串内容
* @param width 位图宽度,要求>=0(单位:px)
* @param height 位图高度,要求>=0(单位:px)
* @param margin 空白边距 (可修改,要求:整型且>=0), 传null时,zxing源码默认使用"4"。
* @param color_black 黑色色块的自定义颜色值
* @param color_white 白色色块的自定义颜色值
* @return
*/
@Nullable
public static Bitmap createQRCodeBitmap(String content, int width, int height,
@Nullable String character_set, @Nullable String error_correction, @Nullable String margin,
int color_black,int color_white) {
/** 1.参数合法性判断 */
if (TextUtils.isEmpty( content )) { // 字符串内容判空
return null;
}
if (width < 0 || height < 0) { // 宽和高都需要>=0
return null;
}
try {
/** 2.设置二维码相关配置,生成BitMatrix(位矩阵)对象 */
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
if (!TextUtils.isEmpty( character_set )) {
hints.put( EncodeHintType.CHARACTER_SET, character_set ); // 字符转码格式设置
}
if (!TextUtils.isEmpty( error_correction )) {
hints.put( EncodeHintType.ERROR_CORRECTION, error_correction ); // 容错级别设置
}
if (!TextUtils.isEmpty( margin )) {
hints.put( EncodeHintType.MARGIN, margin ); // 空白边距设置
}
BitMatrix bitMatrix = new QRCodeWriter().encode( content, BarcodeFormat.QR_CODE, width, height, hints );
/** 3.创建像素数组,并根据BitMatrix(位矩阵)对象为数组元素赋颜色值 */
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (bitMatrix.get( x, y )) {
pixels[y * width + x] = color_black; // 黑色色块像素设置
} else {
pixels[y * width + x] = color_white; // 白色色块像素设置
}
}
}
/** 4.创建Bitmap对象,根据像素数组设置Bitmap每个像素点的颜色值,之后返回Bitmap对象 */
Bitmap bitmap = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 );
bitmap.setPixels( pixels, 0, width, 0, 0, width, height );
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
}