Android ViewBitmap截图加水印依赖

ViewBitmap截图加水印依赖

前言: 在开发APP过程中如何遇到了打印小票的那种功能,需要将订单详情以图片的形式保存到相册并且要加上水印。这个时候只能自己动手去撸了,相信对于有一定经验的人来说将View以图片的形式保存到相册应该不是问题,问题是怎么自己得去设计水印,大部分的时间将会花在了计算水印上了。我之前特意查了下截图加水印的依赖,却没有发现(可能是搜索引擎的问题,不想搜索出来,哈哈),所以我自己写了一个截图加水印的依赖,下面我将介绍这个依赖(这个依赖还有很多不完善的地方,后续会完善并开发更多的水印样式提供给大家使用)。

ViewBitmap的基本使用

导入库

在 allprojects 下的 repositories 里添加 maven { url ‘https://jitpack.io’ }

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

在 dependencies 里添加依赖

dependencies {
		implementation 'com.github.Gyswen:ViewBitmap:v1.1.5'
	}

别忘记了在AndroidManifest.xml里添加获取外部存储权限哦,不然图片无法保存下来

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

ViewBitmap使用的简单例子

XML布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true">

        <Button
            android:id="@+id/butt0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="默认样式"
            android:layout_centerInParent="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/butt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="样式1"
            android:layout_centerInParent="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/butt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="样式2"
            android:layout_centerInParent="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/butt3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="样式3"
            android:layout_centerInParent="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/butt4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="样式4"
            android:layout_centerInParent="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/butt5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="样式5"
            android:layout_centerInParent="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/butt6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="样式6"
            android:layout_centerInParent="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/butt7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="样式7"
            android:layout_centerInParent="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </LinearLayout>

</RelativeLayout>

java代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private RelativeLayout layout;
    private Button button0,button1,button2,button3,button4,button5,button6,button7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layout = findViewById(R.id.layout);
        button0 = findViewById(R.id.butt0);
        button1 = findViewById(R.id.butt1);
        button2 = findViewById(R.id.butt2);
        button3 = findViewById(R.id.butt3);
        button4 = findViewById(R.id.butt4);
        button5 = findViewById(R.id.butt5);
        button6 = findViewById(R.id.butt6);
        button7 = findViewById(R.id.butt7);

        button0.setOnClickListener(this);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //新建画笔,默认style为实心
        final Paint paint = new Paint();
        //设置颜色,颜色可用Color.parseColor("#6b99b9")代替
        paint.setColor(Color.parseColor("#006b99b9"));
        //设置透明度
        paint.setAlpha(80);
        //抗锯齿
        paint.setAntiAlias(true);
        //画笔粗细大小
        paint.setTextSize(ViewBitmap.sp2px(getApplicationContext(),30));
        switch (view.getId()){
            case R.id.butt0:
                if ( ViewBitmap.getInstance(getApplicationContext(),"文休","daragtag/hugh",paint).getViewBitmap(layout)){
                    Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(),"保存失败",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.butt1:
                if ( ViewBitmap.getInstance(getApplicationContext(),"文休","daragtag/hugh",paint).getViewBitmap(layout,-30,ViewBitmapStyle.CENTER)){
                    Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(),"保存失败",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.butt2:
                if ( ViewBitmap.getInstance(getApplicationContext(),"文休","daragtag/hugh",paint).getViewBitmap(layout,-30,ViewBitmapStyle.TOP_LEFT)){
                    Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(),"保存失败",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.butt3:
                if ( ViewBitmap.getInstance(getApplicationContext(),"文休","daragtag/hugh",paint).getViewBitmap(layout,-30,ViewBitmapStyle.LOWER_LEFT)){
                    Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(),"保存失败",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.butt4:
                if ( ViewBitmap.getInstance(getApplicationContext(),"文休","daragtag/hugh",paint).getViewBitmap(layout,-30,ViewBitmapStyle.TOP_RIGHT)){
                    Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(),"保存失败",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.butt5:
                if ( ViewBitmap.getInstance(getApplicationContext(),"文休","daragtag/hugh",paint).getViewBitmap(layout,-30,ViewBitmapStyle.LOWER_RIGET)){
                    Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(),"保存失败",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.butt6:
                if ( ViewBitmap.getInstance(getApplicationContext(),"文休","daragtag/hugh",paint).getViewBitmap(layout,-30,ViewBitmapStyle.TOP_CENTER)){
                    Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(),"保存失败",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.butt7:
                if ( ViewBitmap.getInstance(getApplicationContext(),"文休","daragtag/hugh",paint).getViewBitmap(layout,-30,ViewBitmapStyle.LOWER_CENTER)){
                    Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(),"保存失败",Toast.LENGTH_LONG).show();
                }
                break;
        }
    }
}

ViewBitmap使用详解

如何使用

方式一:
ViewBitmap.getInstance(getApplicationContext(),"文休","daragtag/hugh",paint).getViewBitmap(layout)

方拾二:
ViewBitmap.getInstance(getApplicationContext(),"文休","daragtag/hugh",paint).getViewBitmap(layout,-30,ViewBitmapStyle.LOWER_CENTER)

getInstance()参数解释:context 上下文,loge 水印内容, path 保存路径, paint画笔工具

getViewBitmap()参数解释:view 要截图的布局(控件),rotate 水印角度,style 选择水印样式

样式解释

居中

ViewBitmapStyle.CENTER;

左上角

ViewBitmapStyle.TOP_LEFT;

左下角

ViewBitmapStyle.LOWER_LEFT;

右上角

ViewBitmapStyle.TOP_RIGHT;

右下角

ViewBitmapStyle.LOWER_RIGET;

居中顶部

ViewBitmapStyle.TOP_CENTER;

居中底部

ViewBitmapStyle.LOWER_CENTER;

大家如果觉得有用还希望您能动动鼠标点个赞哦!

猜你喜欢

转载自blog.csdn.net/weixin_42237913/article/details/107873749