乾貨:給圖片加水印效果的自定義控制元件LabelImageView
這兩天不知道給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);
也可以實現,看你個人喜好了。