Android bitmap上面写字

方法1;

Bitmap bitmap;
   public Bitmap drawTextToBitmap(Context mContext, Bitmap bit, String mText, String str3) {

      try {
         Resources resources = mContext.getResources();
         float scale = resources.getDisplayMetrics().density;
         // Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);
         
bitmap = bit;
         Bitmap.Config bitmapConfig = bitmap.getConfig();
         if (bitmapConfig == null) {
            bitmapConfig = Bitmap.Config.ARGB_8888;
         }
         bitmap bitmap.copy(bitmapConfig, true);
         Canvas canvas = new Canvas(bitmap);
         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);//消除锯齿
         
paint.setDither(true); //获取跟清晰的图像采样
         
paint.setFilterBitmap(true);//过滤一些
         
paint.setColor(Color.RED);
         paint.setTextSize((int) (2*scale));
         paint.setShadowLayer(1f0f1f, Color.DKGRAY);//阴影制作半径,x偏移量,y偏移量,阴影颜色
         
Rect bounds = new Rect();
         paint.getTextBounds(mText, 0, mText.length(), bounds);
         int y = (bitmap.getHeight() + bounds.height()) / 4;
         int x = 0;
         Toast.makeText(MyCaremaActivity.this,"x" +bitmap.getHeight() +"y"+y + "y*sace" + y * scale+"sace"+scale,Toast.LENGTH_LONG).show();
         Log.v("===位置""x" +bitmap.getHeight() +"y"+y + "y*sace" + y * scale+"sace"+scale);
         canvas.drawText(mText, x * scale, y * scale, paint);
         canvas.drawText(str3, x * scale, y * scale + 25, paint);
//       canvas.drawText(mText, x * scale, 210, paint);
//       canvas.drawText(str3, x * scale,210 + 25 , paint);
         
Log.v("===合成图片""====ok" + mText);
         return bitmap;
      } catch (Exception e) {
         // TODO: handle exception
         return null
;
      }

  

方法二:

Bitmap bitmap;
   public Bitmap addWaterMark(Bitmap src, String water,String  dtr, Context context){
       bitmap  = src.copy(Bitmap.Config.ARGB_8888true);
      int w = bitmap.getWidth();
      int h = bitmap.getHeight();
      Log.v("bitmap","bw"+w+"bh"+h);
      Canvas canvas = new Canvas(bitmap);
      //启用抗锯齿和使用设备的文本字距
      
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
      //字体的相关设置
      
textPaint.setTextSize(14.0f);//字体大小
      //textPaint.setTypeface(Typeface.DEFAULT);
      
textPaint.setColor(Color.RED);
      textPaint.setFakeBoldText(false);
      //自定义宋体
      
String familyName = "宋体";
      Typeface font = Typeface.create(familyName, Typeface.NORMAL);
      textPaint.setTypeface(font);
      textPaint.setShadowLayer(3f00, context.getResources().getColor(android.R.color.background_dark));
      //图片上添加水印的位置,这里设置的是中下部3/4处
      
canvas.drawText(water, (float)(w*0.01), (float) (h * 0.9), textPaint);
      canvas.drawText(dtr, (float)(w*0.01), (float) (h * 0.9 20), textPaint);
      canvas.save(Canvas.ALL_SAVE_FLAG);
      canvas.restore();
      return bitmap;
   }

      private  String mYear;
      private  String mMonth;
      private  String mDay;
      private  String mWay;
         String s;
      public   String StringData(){
         final Calendar c = Calendar.getInstance();
         c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
         mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
         
mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
         
mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
         
mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
         SimpleDateFormat formatter = new SimpleDateFormat ("HH:mm:ss ");
         Date curDate = new Date(System.currentTimeMillis());//获取当前时间
         
String str = formatter.format(curDate);
         if("1".equals(mWay)){
            mWay ="天";
         }else if("2".equals(mWay)){
            mWay ="一";
         }else if("3".equals(mWay)){
            mWay ="二";
         }else if("4".equals(mWay)){
            mWay ="三";
         }else if("5".equals(mWay)){
            mWay ="四";
         }else if("6".equals(mWay)){
            mWay ="五";
         }else if("7".equals(mWay)){
            mWay ="六";
         }
         s"时间:"+mYear "年" mMonth "月" mDay+"日"+"周"+mWay+str;
         Log.v("==获取时间",mYear "年" mMonth "月" mDay+"日"+"/星期"+mWay+str);
         Log.v("==获取时间s",s);
         return s;
      }

}

你可能感兴趣的:(Android bitmap上面写字)