【发布时间】:2017-05-03 07:59:24
【问题描述】:
我正在创建一个带有 QR 条码的应用程序。条形码正确加载,但不知何故加载有点慢,在我点击/单击菜单后大约 3-5 秒。
我们可以让它更快吗?或者页面加载那么长是否正常?其他部分加载仅需 1 秒或更短。该应用程序也离线,因此不需要互联网连接。
这里是我生成二维码的代码:
ImageView imageViewBarcode = (ImageView)findViewById(R.id.imageViewBarcode);
try {
bitmap = TextToImageEncode(barcode_user);
imageViewBarcode.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
上面的代码放在 onCreate 里面。所以当页面加载时,它会生成条形码。
这里是创建条形码的功能
Bitmap TextToImageEncode(String Value) throws WriterException {
BitMatrix bitMatrix;
try {
bitMatrix = new MultiFormatWriter().encode(
Value,
BarcodeFormat.DATA_MATRIX.QR_CODE,
QRcodeWidth, QRcodeWidth, null
);
} catch (IllegalArgumentException Illegalargumentexception) {
return null;
}
int bitMatrixWidth = bitMatrix.getWidth();
int bitMatrixHeight = bitMatrix.getHeight();
int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];
for (int y = 0; y < bitMatrixHeight; y++) {
int offset = y * bitMatrixWidth;
for (int x = 0; x < bitMatrixWidth; x++) {
pixels[offset + x] = bitMatrix.get(x, y) ?
getResources().getColor(R.color.colorBlack):getResources().getColor(R.color.colorWhite);
}
}
Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);
bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight);
return bitmap;
}
【问题讨论】: