android 文字图片合成
16lz
2021-01-24
引用:http://blog.csdn.net/cq361106306/article/details/8142526
- 两种方法:
- 1.直接在图片上写文字
- Stringstr="PICC要写的文字";
- ImageViewimage=(ImageView)this.findViewById(R.id.ImageView);
- Bitmapphoto=BitmapFactory.decodeResource(this.getResources(),R.drawable.text);
- intwidth=photo.getWidth(),hight=photo.getHeight();
- System.out.println("宽"+width+"高"+hight);
- icon=Bitmap.createBitmap(width,hight,Bitmap.Config.ARGB_8888);//建立一个空的BItMap
- Canvascanvas=newCanvas(icon);//初始化画布绘制的图像到icon上
- PaintphotoPaint=newPaint();//建立画笔
- photoPaint.setDither(true);//获取跟清晰的图像采样
- photoPaint.setFilterBitmap(true);//过滤一些
- Rectsrc=newRect(0,0,photo.getWidth(),photo.getHeight());//创建一个指定的新矩形的坐标
- Rectdst=newRect(0,0,width,hight);//创建一个指定的新矩形的坐标
- canvas.drawBitmap(photo,src,dst,photoPaint);//将photo缩放或则扩大到dst使用的填充区photoPaint
- PainttextPaint=newPaint(Paint.ANTI_ALIAS_FLAG|Paint.DEV_KERN_TEXT_FLAG);//设置画笔
- textPaint.setTextSize(20.0f);//字体大小
- textPaint.setTypeface(Typeface.DEFAULT_BOLD);//采用默认的宽度
- textPaint.setColor(Color.RED);//采用的颜色
- //textPaint.setShadowLayer(3f,1,1,this.getResources().getColor(android.R.color.background_dark));//影音的设置
- canvas.drawText(str,20,26,textPaint);//绘制上去字,开始未知x,y采用那只笔绘制
- canvas.save(Canvas.ALL_SAVE_FLAG);
- canvas.restore();
- image.setImageBitmap(icon);
- saveMyBitmap(icon);
- 2.将两个图片合成
- onCreat方法里面{
- Bitmapmark=BitmapFactory.decodeResource(this.getResources(),R.drawable.icon);
- Bitmapphoto=BitmapFactory.decodeResource(this.getResources(),R.drawable.text);
- Bitmapa=createBitmap(photo,mark);
- image.setImageBitmap(a);
- saveMyBitmap(a);
- }
- privateBitmapcreateBitmap(Bitmapsrc,Bitmapwatermark)
- {
- Stringtag="createBitmap";
- //Log.d(tag,"createanewbitmap");
- if(src==null)
- {
- returnnull;
- }
- intw=src.getWidth();
- inth=src.getHeight();
- intww=watermark.getWidth();
- intwh=watermark.getHeight();
- //createthenewblankbitmap
- Bitmapnewb=Bitmap.createBitmap(w,h,Config.ARGB_8888);
- //创建一个新的和SRC长度宽度一样的位图
- Canvascv=newCanvas(newb);
- //drawsrcinto
- cv.drawBitmap(src,0,0,null);//在0,0坐标开始画入src
- //drawwatermarkinto
- cv.drawBitmap(watermark,w-ww+5,h-wh+5,null);//在src的右下角画入水印
- //saveallclip
- cv.save(Canvas.ALL_SAVE_FLAG);//保存
- //store
- cv.restore();//存储
- returnnewb;
- }
- //保存图片到data下面
- publicvoidsaveMyBitmap(Bitmapbmp){
- FileOutputStreamfos=null;
- try{
- fos=openFileOutput("image1.jpg",Context.MODE_PRIVATE);
- bmp.compress(Bitmap.CompressFormat.JPEG,100,fos);
- }catch(FileNotFoundExceptione){
- }finally{
- if(fos!=null){
- try{
- fos.flush();
- fos.close();
- }catch(IOExceptione){
- }
- }
- }
- }
更多相关文章
- android整合两个bitmap
- android drawText
- android 获取字体宽高
- Android实现圆形图片
- 自定义view-制作一个加载中的圆形
- Android(安卓)Shader应用开发之雷达扫描效果
- android的图片加载和伸缩处理
- Android(安卓)MPAndroidChart:动态添加统计数据线【8】
- Bitmap and DrawPoint