|
例子:
从别的页面点击按钮弹出以Dialog样式出现的二维码生成页面,背景半透明,点击屏幕其他区域该页面可以消失。
public class PayNowActivity extends Activity {
// @BindView(R2.id.iv_QRcode)
ImageView ivQRcode;
// @BindView(R2.id.iv_close)
ImageView ivClose;
private String content;
private static final String TAG = PayNowActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_now);
// ButterKnife.bind(this);
ivQRcode = (ImageView) findViewById(R.id.iv_QRcode);
ivClose = (ImageView) findViewById(R.id.iv_close);
content = getIntent().getStringExtra("content");
Bitmap bitmap = createQRCode(content);
ivQRcode.setImageBitmap(bitmap);
ivClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
/**
* 将指定的内容生成成二维码
*
* @param content 将要生成二维码的内容
* @return 返回生成好的二维码事件
* @throws WriterException WriterException异常
*/
public Bitmap createQRCode(String content) {
int qrwidth = getResources().getDimensionPixelOffset(R.dimen.qrcode_width);
//生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
BitMatrix matrix = null;
try {
matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrwidth, qrwidth);
} catch (WriterException e) {
e.printStackTrace();
}
int width = matrix.getWidth();
int height = matrix.getHeight();
//二维矩阵转为一维像素数组,也就是一直横着排了。
int[] pixels = new int[width * height];
//两个for循环是图片横列扫描的结果
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Logger.t(TAG).d("width=" + width + ",height=" + height);
//通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
// @OnClick(R2.id.iv_close)
// public void onClick() {
// finish();
// }
}
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<RelativeLayout
android:layout_width="@dimen/pay_width"
android:layout_height="@dimen/pay_height">
<LinearLayout
android:background="@drawable/corner_border_style"
android:layout_marginTop="@dimen/px_15"
android:layout_marginRight="@dimen/px_15"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="扫描二维码进行支付"
android:textSize="@dimen/px_24"
android:background="@drawable/dash_border_style"
android:textColor="@color/color_black"
android:paddingTop="@dimen/px_50"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/head_height" />
<ImageView
android:id="@+id/iv_QRcode"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="请您在支付成功后关注"
android:textSize="@dimen/px_16"
android:textColor="@color/color_black"
android:layout_marginBottom="@dimen/px_20"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="“馨e品”微信公众号查看订单"
android:textColor="@color/color_black"
android:textSize="@dimen/px_16"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:src="@drawable/gallery_dele"
android:id="@+id/iv_close"
android:layout_alignParentRight="true"
android:layout_width="@dimen/px_40"
android:layout_height="@dimen/px_40" />
</RelativeLayout>
</RelativeLayout>
AndroidManifest.xml中的配置:
<activity
android:name=".activity.PayNowActivity"
android:theme="@style/CustomTheme.Dialog" />
|