添加水印的图像就像在Flipboard的Android版 [英] Add Watermark to an image just like flipboard in Android
问题描述
我要添加水印的图像就像Flipboard的一样。
正如你所看到的文字是在用黑色透明背景图像的底部增加。我想这样做同样的事情。到目前为止我已经成功地写上图片文字,但我不能让它的背景上的黑色透明的,就像上面的图片。
下面是我的code到目前为止,这是我从发现这里。
公共标志位图(位图SRC,弦乐水印){
INT W = src.getWidth();
INT H = src.getHeight(); 着色着色=新的LinearGradient(0,0,100,0,Color.TRANSPARENT,Color.BLACK,TileMode.CLAMP); 位图结果= Bitmap.createBitmap(W,H,src.getConfig());
帆布帆布=新的Canvas(结果);
canvas.drawBitmap(源,0,0,NULL);
涂料粉刷=新的油漆();
paint.setColor(Color.WHITE);
paint.setTextSize(50);
paint.setAntiAlias(真);
paint.setShader(着色器);
paint.setUnderlineText(假);
canvas.drawText(水印,10,H-15,油漆); 返回结果;
}
我觉得它不需要渐变,您可以用简单的颜色使用的drawRect()方法失败中汲取。
样code是下面,我愿意去做的黑色背景大小的整幅图像的25%。
公共标志位图(位图SRC,弦乐水印){
INT W = src.getWidth();
INT H = src.getHeight(); 涂料bgPaint =新的油漆();
bgPaint.setColor(Color.parse(AA000000)); //透明的黑色,通过改变十六进制值AA,00和FF之间改变不透明度 位图结果= Bitmap.createBitmap(W,H,src.getConfig());
帆布帆布=新的Canvas(结果);
canvas.drawBitmap(源,0,0,NULL);
涂料粉刷=新的油漆();
paint.setColor(Color.WHITE);
paint.setTextSize(50);
paint.setAntiAlias(真);
paint.setUnderlineText(假); //应该先画背景,顺序很重要
INT左= 0;
诠释权= W;
INT底= H;
INT顶部=自下而上(H * 0.25);
canvas.drawRect(左,上,右,下,bgPaint); canvas.drawText(水印,10,H-15,油漆);返回结果;
}
I want to add watermark to images just like flipboard does.
As you can see text is added at the bottom of the images with black transparent background. I want to do the exact same thing. Till now I've managed to write text on image but I am not able to make it's background black transparent just like the above picture.
Here's my code so far which I found from here.
public Bitmap mark(Bitmap src, String watermark) {
int w = src.getWidth();
int h = src.getHeight();
Shader shader = new LinearGradient(0, 0, 100, 0, Color.TRANSPARENT, Color.BLACK, TileMode.CLAMP);
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(50);
paint.setAntiAlias(true);
paint.setShader(shader);
paint.setUnderlineText(false);
canvas.drawText(watermark, 10 , h-15, paint);
return result;
}
I think it doesn't require gradient, you can draw it using simple color and using drawRect() method.
Sample code is below, i gonna take the black background size as 25% of whole image.
public Bitmap mark(Bitmap src, String watermark) {
int w = src.getWidth();
int h = src.getHeight();
Paint bgPaint=new Paint();
bgPaint.setColor(Color.parse("AA000000")); //transparent black,change opacity by changing hex value "AA" between "00" and "FF"
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(50);
paint.setAntiAlias(true);
paint.setUnderlineText(false);
//should draw background first,order is important
int left=0;
int right=w;
int bottom=h;
int top=bottom-(h*.25);
canvas.drawRect(left,top,right,bottom,bgPaint);
canvas.drawText(watermark, 10 , h-15, paint);
return result;
}
这篇关于添加水印的图像就像在Flipboard的Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!