转载请著名出处:王亟亟的大牛之路
这两天不知道给Soyi加些什么东西,那就慢慢的往CodeActivity里加东西吧。所以就写了这么个简单的自己定义控件LabelImageView。
HOW to do?
0,获取一大堆參数,没有传就用默认的。
1,设置传来的image_src作为底版,在onDraw方法中 canvas.drawBitmap(bitmap, 0, 0, paint);
2,依据textLocation參数推断位置,默认右下
switch (textLocation) {
case RightBottom:
//右下
canvas.drawText(contentStr, (int) bitmapWidth - paint.measureText(contentStr), (int) bitmapHeight-fontMetrics.bottom, paint);
break;
case RightTop:
//右上
canvas.drawText(contentStr, (int) bitmapWidth - paint.measureText(contentStr), 0+textSize, paint);
break;
case LeftTop:
//左上
canvas.drawText(contentStr, 0, 0+textSize, paint);
break;
case LeftBottom:
//左下
canvas.drawText(contentStr, 0, (int) bitmapHeight-fontMetrics.bottom, paint);
break;
}
3..结束!
!
效果图:
或是在这里:
又在这里:
反正就是能够给你的图片打上你的水印。加上你想要加的内容即可(闲着蛋疼的时候不知道写什么,就写了)
包结构:
就这么一个类,非常简便,所以也就不做Gradle了,可是记得把一些须要的素材文件一起Copy走哦!
怎么用?
在你的主布局里面引入:
<labelimageview.pro.wjj.labelimageview.LabelImageViewPro.LabelImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
labelImageView:image_src="@drawable/bg"
labelImageView:text_color="@color/White"
labelImageView:text_content="Hellow World"
labelImageView:text_location="RightBottom"
labelImageView:text_size="50" />
就能够了,假设要用一系列set的方法。那就赋予一个ID各种Set吧。大致须要的几个方法都谢了,假设有别的需求就自己加咯。
说一下一些配置參数:
image_src:图片资源(假设不设置请给实现类里面加一个备用的图片)
text_color:字体颜色
text_content:详细的文字内容
text_location:文字的位置。现有的是:左上,左下,右上。右下,假设须要特殊的位置就设置内部的bitmapWidth。 bitmapHeight即可。
final static String LeftTop = "LeftTop";
final static String LeftBottom = "LeftBottom";
final static String RightTop = "RightTop";
final static String RightBottom = "RightBottom";
text_size:字体的大小,默认是30.
主要说一下image_width和image_height,假设你想要设置的图片有多大就显示多大那么就使用
android:layout_width="wrap_conten"
android:layout_height="wrap_content"
假设你须要附加设置图片大小,请设置
labelImageView:image_height="100dp"
labelImageView:image_width="100dp"
像这种数值。而
android:layout_width="wrap_conten"
android:layout_height="wrap_content"
也不用删除,就这么放着吧,不影响使用,假设你的图片大于屏幕。会默认设置为最大值。所以能够不用在意要不要设置成match_parent。
当然你也能够用Java代码去实现,像这样
LabelImageView image=(LabelImageView )findViewById(R.id.labelImageView );
image.setTextColor(Color.Black);
image.setTextLocation(RightBottom);
image.setContentStr("你好啊");
image.setTextSize(45);
也能够实现,看你个人喜好了。
源代码地址:https://github.com/ddwhan0123/GitLabelImageView