Bitmap bmp = Bitmap.createBitmap(dm.widthPixels, 200, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); Paint paint = new Paint(); Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/CONSOLA.TTF"); paint.setTypeface(typeface); paint.setColor(Color.GREEN); paint.setTextSize(40); canvas.drawText("Hello World", 100,100, paint); srcImageView.setImageBitmap(bmp);
和繪製drawable相似,這裏再也不贅述。canvas
Drawable drwable = new Drawable() { @Override public void setColorFilter(ColorFilter cf) { // TODO Auto-generated method stub } @Override public void setAlpha(int alpha) { // TODO Auto-generated method stub } @Override public int getOpacity() { // TODO Auto-generated method stub return 0; } @Override public void draw(Canvas canvas) { // TODO Auto-generated method stub } };
/** * bitmap轉爲base64 * @param bitmap * @return */ public static String bitmapToBase64(Bitmap bitmap) { String result = null; ByteArrayOutputStream baos = null; try { if (bitmap != null) { baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); baos.flush(); baos.close(); byte[] bitmapBytes = baos.toByteArray(); result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (baos != null) { baos.flush(); baos.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; } /** * base64轉爲bitmap * @param base64Data * @return */ public static Bitmap base64ToBitmap(String base64Data) { byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT); return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); }