android 图片加文字
2013-11-22 17:08
211 查看
两种方法:
1.直接在图片上写文字
String str = "PICC要写的文字";
ImageView image = (ImageView) this.findViewById(R.id.ImageView);
Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
int width = photo.getWidth(), hight = photo.getHeight();
System.out.println("宽"+width+"高"+hight);
icon = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); //建立一个空的BItMap
Canvas canvas = new Canvas(icon);//初始化画布绘制的图像到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
Paint textPaint = new Paint(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方法里面{
Bitmap mark = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
Bitmap a = createBitmap(photo,mark);
image.setImageBitmap(a);
saveMyBitmap(a);
}
private Bitmap createBitmap( Bitmap src, Bitmap watermark )
{
String tag = "createBitmap";
// Log.d( tag, "create a new bitmap" );
if( src == null )
{
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//create the new blank bitmap
Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
//创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印
//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );//保存
//store
cv.restore();//存储
return newb;
}
//保存图片到data下面
public void saveMyBitmap(Bitmap bmp){
FileOutputStream fos = null;
try {
fos = openFileOutput("image1.jpg", Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
}
}
}
}
1.直接在图片上写文字
String str = "PICC要写的文字";
ImageView image = (ImageView) this.findViewById(R.id.ImageView);
Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
int width = photo.getWidth(), hight = photo.getHeight();
System.out.println("宽"+width+"高"+hight);
icon = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); //建立一个空的BItMap
Canvas canvas = new Canvas(icon);//初始化画布绘制的图像到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
Paint textPaint = new Paint(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方法里面{
Bitmap mark = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
Bitmap a = createBitmap(photo,mark);
image.setImageBitmap(a);
saveMyBitmap(a);
}
private Bitmap createBitmap( Bitmap src, Bitmap watermark )
{
String tag = "createBitmap";
// Log.d( tag, "create a new bitmap" );
if( src == null )
{
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//create the new blank bitmap
Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
//创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印
//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );//保存
//store
cv.restore();//存储
return newb;
}
//保存图片到data下面
public void saveMyBitmap(Bitmap bmp){
FileOutputStream fos = null;
try {
fos = openFileOutput("image1.jpg", Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
}
}
}
}
相关文章推荐
- Android自定义实现图片加文字功能
- android图片加文字轮播效果,RollViewPager使用,简单修改源代码
- android发布图片加文字描述
- Android Apps GridView的图片加文字效果
- Android基础 - 对话框和浮动Activity【转】
- (转帖)Android重力感应Demo
- android的PowerManager和PowerManager.WakeLock
- Android 避免内存泄漏(译)
- Android RelativeLayout 属性
- 新建基于Rokon框架的Android项目
- android 入门学习笔记 LayoutInflater的使用
- Android手机摄像头编程入门
- android的测试工具CTS
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- Android平台的模块编译加速
- [Android]实现静默安装APK的两种方法
- Android学习系列(22)--App主界面比较
- Android视频教学下载大全(VeryCD上)
- Android开发视频教程大总结—23部视频教程