01 Nov 2015
public final class genQrCode {
private static final int BLACK = 0xff000000;
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
//在相应的位置填充黑色
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
}
30 Oct 2015
static class LongTextView extends View {
private static final float TEXT_SIZE = 60;
private static final CharSequence TEXT = "This is a long long long " +
"long long long long long long long long long long " +
"long long long string";
TextPaint textPaint;
public LongTextView(Context context) {
super(context);
textPaint = new TextPaint();
textPaint.setColor(Color.BLUE);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextSize(TEXT_SIZE);
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
StaticLayout staticLayout = new StaticLayout(
TEXT, textPaint,
canvas.getWidth(),
Layout.Alignment.ALIGN_NORMAL,
1.0f, 0.0f, false
);
staticLayout.draw(canvas);
}
}