Android給圖片加文字和圖片水印實(shí)例代碼
我們?cè)谧鲰?xiàng)目的時(shí)候有時(shí)候需要給圖片添加水印,水寒今天就遇到了這樣的問題,所以搞了一個(gè)工具類,貼出來大家直接調(diào)用就行。
/**
* 圖片工具類
* @author 水寒
*
*/
public class ImageUtil {
/**
* 設(shè)置水印圖片在左上角
* @param Context
* @param src
* @param watermark
* @param paddingLeft
* @param paddingTop
* @return
*/
public static Bitmap createWaterMaskLeftTop(
Context context, Bitmap src, Bitmap watermark,
int paddingLeft, int paddingTop) {
return createWaterMaskBitmap(src, watermark,
dp2px(context, paddingLeft), dp2px(context, paddingTop));
}
private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark,
int paddingLeft, int paddingTop) {
if (src == null) {
return null;
}
int width = src.getWidth();
int height = src.getHeight();
//創(chuàng)建一個(gè)bitmap
Bitmap newb = Bitmap.createBitmap(width, height, Config.ARGB_8888);// 創(chuàng)建一個(gè)新的和SRC長(zhǎng)度寬度一樣的位圖
//將該圖片作為畫布
Canvas canvas = new Canvas(newb);
//在畫布 0,0坐標(biāo)上開始繪制原始圖片
canvas.drawBitmap(src, 0, 0, null);
//在畫布上繪制水印圖片
canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
// 保存
canvas.save(Canvas.ALL_SAVE_FLAG);
// 存儲(chǔ)
canvas.restore();
return newb;
}
/**
* 設(shè)置水印圖片在右下角
* @param Context
* @param src
* @param watermark
* @param paddingRight
* @param paddingBottom
* @return
*/
public static Bitmap createWaterMaskRightBottom(
Context context, Bitmap src, Bitmap watermark,
int paddingRight, int paddingBottom) {
return createWaterMaskBitmap(src, watermark,
src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
}
/**
* 設(shè)置水印圖片到右上角
* @param Context
* @param src
* @param watermark
* @param paddingRight
* @param paddingTop
* @return
*/
public static Bitmap createWaterMaskRightTop(
Context context, Bitmap src, Bitmap watermark,
int paddingRight, int paddingTop) {
return createWaterMaskBitmap( src, watermark,
src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
dp2px(context, paddingTop));
}
/**
* 設(shè)置水印圖片到左下角
* @param Context
* @param src
* @param watermark
* @param paddingLeft
* @param paddingBottom
* @return
*/
public static Bitmap createWaterMaskLeftBottom(
Context context, Bitmap src, Bitmap watermark,
int paddingLeft, int paddingBottom) {
return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft),
src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
}
/**
* 設(shè)置水印圖片到中間
* @param Context
* @param src
* @param watermark
* @return
*/
public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
return createWaterMaskBitmap(src, watermark,
(src.getWidth() - watermark.getWidth()) / 2,
(src.getHeight() - watermark.getHeight()) / 2);
}
/**
* 給圖片添加文字到左上角
* @param context
* @param bitmap
* @param text
* @return
*/
public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap, String text,
int size, int color, int paddingLeft, int paddingTop) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setTextSize(dp2px(context, size));
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
return drawTextToBitmap(context, bitmap, text, paint, bounds,
dp2px(context, paddingLeft),
dp2px(context, paddingTop) + bounds.height());
}
/**
* 繪制文字到右下角
* @param context
* @param bitmap
* @param text
* @param size
* @param color
* @param paddingLeft
* @param paddingTop
* @return
*/
public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap, String text,
int size, int color, int paddingRight, int paddingBottom) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setTextSize(dp2px(context, size));
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
return drawTextToBitmap(context, bitmap, text, paint, bounds,
bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
bitmap.getHeight() - dp2px(context, paddingBottom));
}
/**
* 繪制文字到右上方
* @param context
* @param bitmap
* @param text
* @param size
* @param color
* @param paddingRight
* @param paddingTop
* @return
*/
public static Bitmap drawTextToRightTop(Context context, Bitmap bitmap, String text,
int size, int color, int paddingRight, int paddingTop) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setTextSize(dp2px(context, size));
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
return drawTextToBitmap(context, bitmap, text, paint, bounds,
bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
dp2px(context, paddingTop) + bounds.height());
}
/**
* 繪制文字到左下方
* @param context
* @param bitmap
* @param text
* @param size
* @param color
* @param paddingLeft
* @param paddingBottom
* @return
*/
public static Bitmap drawTextToLeftBottom(Context context, Bitmap bitmap, String text,
int size, int color, int paddingLeft, int paddingBottom) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setTextSize(dp2px(context, size));
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
return drawTextToBitmap(context, bitmap, text, paint, bounds,
dp2px(context, paddingLeft),
bitmap.getHeight() - dp2px(context, paddingBottom));
}
/**
* 繪制文字到中間
* @param context
* @param bitmap
* @param text
* @param size
* @param color
* @return
*/
public static Bitmap drawTextToCenter(Context context, Bitmap bitmap, String text,
int size, int color) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setTextSize(dp2px(context, size));
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
return drawTextToBitmap(context, bitmap, text, paint, bounds,
(bitmap.getWidth() - bounds.width()) / 2,
(bitmap.getHeight() + bounds.height()) / 2);
}
//圖片上繪制文字
private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text,
Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
paint.setDither(true); // 獲取跟清晰的圖像采樣
paint.setFilterBitmap(true);// 過濾一些
if (bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, paddingLeft, paddingTop, paint);
return bitmap;
}
/**
* 縮放圖片
* @param src
* @param w
* @param h
* @return
*/
public static Bitmap scaleWithWH(Bitmap src, double w, double h) {
if (w == 0 || h == 0 || src == null) {
return src;
} else {
// 記錄src的寬高
int width = src.getWidth();
int height = src.getHeight();
// 創(chuàng)建一個(gè)matrix容器
Matrix matrix = new Matrix();
// 計(jì)算縮放比例
float scaleWidth = (float) (w / width);
float scaleHeight = (float) (h / height);
// 開始縮放
matrix.postScale(scaleWidth, scaleHeight);
// 創(chuàng)建縮放后的圖片
return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);
}
}
/**
* dip轉(zhuǎn)pix
* @param context
* @param dp
* @return
*/
public static int dp2px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
}
使用方法如下:
添加一個(gè)布局,上面是原始圖片,下面是添加水印后的圖片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/sour_pic_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="原圖" />
<ImageView
android:id="@+id/sour_pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside"/>
<TextView
android:id="@+id/watermark_pic_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水印" />
<ImageView
android:id="@+id/wartermark_pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside"/>
</LinearLayout>
在Activity中獲取到ImageView對(duì)象,并且獲取Bitmap對(duì)象,對(duì)Bitmap進(jìn)行canva繪圖,添加水?。?/p>
/**
* 圖片工具類
* @author 水寒
*
*/
public class MainActivity extends Activity {
private ImageView mSourImage;
private ImageView mWartermarkImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
mSourImage = (ImageView) findViewById(R.id.sour_pic);
mWartermarkImage = (ImageView) findViewById(R.id.wartermark_pic);
Bitmap sourBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sour_pic);
mSourImage.setImageBitmap(sourBitmap);
Bitmap waterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.weixin);
Bitmap watermarkBitmap = ImageUtil.createWaterMaskCenter(sourBitmap, waterBitmap);
watermarkBitmap = ImageUtil.createWaterMaskLeftBottom(this, watermarkBitmap, waterBitmap, 0, 0);
watermarkBitmap = ImageUtil.createWaterMaskRightBottom(this, watermarkBitmap, waterBitmap, 0, 0);
watermarkBitmap = ImageUtil.createWaterMaskLeftTop(this, watermarkBitmap, waterBitmap, 0, 0);
watermarkBitmap = ImageUtil.createWaterMaskRightTop(this, watermarkBitmap, waterBitmap, 0, 0);
Bitmap textBitmap = ImageUtil.drawTextToLeftTop(this, watermarkBitmap, "左上角", 16, Color.RED, 0, 0);
textBitmap = ImageUtil.drawTextToRightBottom(this, textBitmap, "右下角", 16, Color.RED, 0, 0);
textBitmap = ImageUtil.drawTextToRightTop(this, textBitmap, "右上角", 16, Color.RED, 0, 0);
textBitmap = ImageUtil.drawTextToLeftBottom(this, textBitmap, "左下角", 16, Color.RED, 0, 0);
textBitmap = ImageUtil.drawTextToCenter(this, textBitmap, "中間", 16, Color.RED);
mWartermarkImage.setImageBitmap(textBitmap);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android視頻處理之動(dòng)態(tài)時(shí)間水印效果
- Android添加水印的正確方法 只要三步!
- Android實(shí)現(xiàn)為圖片添加水印
- Android 給圖片加上水印的示例代碼(支持logo+文字)
- Android給任何view添加全屏傾斜水印
- Android 圖片添加水印的實(shí)現(xiàn)方法
- android實(shí)現(xiàn)文字水印效果 支持多行水印
- Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)分享長(zhǎng)圖并且添加全圖水印
- Android可配置透明度的水印
相關(guān)文章
Android使用Scroll+Fragment仿京東分類效果
這篇文章主要為大家詳細(xì)介紹了Android使用Scroll+Fragment仿京東分類效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
關(guān)于ADB的Android Debug Bridge(安卓調(diào)試橋)那些事
這篇文章主要介紹了關(guān)于ADB的Android Debug Bridge(安卓調(diào)試橋)那些事,需要的朋友可以參考下2019-10-10
Android通過wifi連接手機(jī)(不需要root)
這篇文章主要為大家詳細(xì)介紹了Android通過wifi連接手機(jī),不需要root,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android實(shí)現(xiàn)圖片循環(huán)播放的實(shí)例方法
2013-05-05
Android中EditText的drawableRight屬性設(shè)置點(diǎn)擊事件
這篇文章主要介紹了Android中EditText的drawableRight屬性的圖片設(shè)置點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android使用分類型RecyclerView仿各大商城首頁(yè)
這篇文章主要為大家詳細(xì)介紹了Android使用分類型的RecyclerView仿各大商城首頁(yè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android Studio修改Log信息顏色的實(shí)現(xiàn)
這篇文章主要介紹了Android Studio修改Log信息顏色的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Android多國(guó)語言轉(zhuǎn)換Excel及Excel轉(zhuǎn)換為string詳解
這篇文章主要給大家介紹了關(guān)于Android多國(guó)語言轉(zhuǎn)換Excel以及Excel轉(zhuǎn)換為string的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2019-01-01
Android利用LitePal操作數(shù)據(jù)庫(kù)存取圖片
這篇文章主要為大家詳細(xì)介紹了Android利用LitePal操作數(shù)據(jù)庫(kù)存取圖片的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Compose自定義View實(shí)現(xiàn)宇智波斑寫輪眼
這篇文章主要為大家介紹了Compose自定義View實(shí)現(xiàn)宇智波斑寫輪眼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

