android使用Canvas在图片上绘制文字
来源:互联网 发布:java 驼峰转下划线 编辑:程序博客网 时间:2024/04/29 19:32
一个小应用,在图片上绘制文字,以下是绘制文字的方法,并且能够实现自动换行,字体自动适配屏幕大小
private void drawNewBitmap(ImageView imageView, String str) { Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.background); int width = photo.getWidth(); int hight = photo.getHeight(); //建立一个空的Bitmap Bitmap icon = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); // 初始化画布绘制的图像到icon上 Canvas canvas = new Canvas(icon); // 建立画笔 Paint photoPaint = new Paint(); // 获取更清晰的图像采样,防抖动 photoPaint.setDither(true); // 过滤一下,抗剧齿 photoPaint.setFilterBitmap(true); Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());// 创建一个指定的新矩形的坐标 Rect dst = new Rect(0, 0, width, hight);// 创建一个指定的新矩形的坐标 canvas.drawBitmap(photo, src, dst, photoPaint);// 将photo 缩放或则扩大到dst使用的填充区photoPaint<span style="white-space:pre"></span> //自定义的画笔 TextPaint textPaint=myTextPaint();
<span style="white-space:pre"></span> //写入文字的位置 drawText(canvas,textPaint,str,45,hight/5,width); canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); imageView.setImageBitmap(icon); saveMyBitmap(this,icon); }
//设置画笔的字体和颜色 public TextPaint myTextPaint(){ TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);// 设置画笔 int TEXT_SIZE = Math.round(25 * getRATIO()); textPaint.setTextSize(TEXT_SIZE);// 字体大小 textPaint.setTypeface(Typeface.DEFAULT_BOLD);// 采用默认的宽度 textPaint.setColor(Color.argb(255,94,38,18));// 采用的颜色 return textPaint;
//写入文字,自动换行的方法 public void drawText(Canvas canvas, TextPaint Paint,String textString,int x,int y,int width) { //int Width=Math.round(width* getRATIO()); int start_x=Math.round(x * getRATIO()); int start_y=Math.round(y * getRATIO()); StaticLayout staticLayout=new StaticLayout(textString, Paint, width-start_x*2, Alignment.ALIGN_NORMAL, 1.5f, 0.0f, false); //绘制的位置 canvas.translate(start_x, start_y); staticLayout.draw(canvas); }
0 0
- android使用Canvas在图片上绘制文字
- Android 使用Canvas在图片上绘制文字
- Android上使用Canvas绘制文字
- Android上使用Canvas绘制文字
- Android上使用Canvas绘制文字
- android开发 之 Canvas绘制文字,图片
- Canvas使用 -- 在canvas上绘制圆角矩形并添加文字
- Android绘图实例(Bitmmap,Canvas,Pain的使用)&动态的在图片上添加文字(canvas.drawText)
- 在图片上准确的绘制文字
- 在QListWidgetItem上绘制文字和图片
- android中Canvas使用drawBitmap绘制图片
- android中Canvas使用drawBitmap绘制图片
- android中Canvas使用drawBitmap绘制图片
- android中Canvas使用drawBitmap绘制图片
- 使用canvas与Paint在View中居中绘制文字
- 使用canvas与Paint在View中居中绘制文字
- 使用canvas与Paint在View中居中绘制文字
- 使用canvas与Paint在View中居中绘制文字
- Android socket 通信
- 几条 ffmpeg 的命令
- 练习二1011
- 不同shell文件中的函数调用
- c++
- android使用Canvas在图片上绘制文字
- innodb_flush_log_at_trx_commit配置
- mysql事务处理
- HDU——2191悼念512汶川大地震遇难同胞(多重背包)
- uva 12545 Bits Equalizer (SWERC 2012) 贪心 分析
- FFMPEG视音频编解码零基础学习方法
- printf函数的基本用法
- js倒计时(附js date操作)
- Hadoop学习8:hdfs文件操作