├── WaterMaskDemo
├── app
│ ├── .gitignore
│ ├── src
│ │ ├── main
│ │ │ ├── res
│ │ │ │ ├── values
│ │ │ │ │ ├── strings.xml
│ │ │ │ │ ├── colors.xml
│ │ │ │ │ ├── styles.xml
│ │ │ │ │ └── attrs.xml
│ │ │ │ ├── mipmap-xhdpi
│ │ │ │ │ ├── pic_03.png
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-hdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-mdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-xxhdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-xxxhdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ └── layout
│ │ │ │ │ ├── activity_main.xml
│ │ │ │ │ └── layout_watermask.xml
│ │ │ ├── AndroidManifest.xml
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── echo
│ │ │ │ └── watermaskdemo
│ │ │ │ ├── MainActivity.java
│ │ │ │ ├── WaterMaskView.java
│ │ │ │ └── WaterMaskUtil.java
│ │ ├── test
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── echo
│ │ │ │ └── watermaskdemo
│ │ │ │ └── ExampleUnitTest.java
│ │ └── androidTest
│ │ │ └── java
│ │ │ └── com
│ │ │ └── echo
│ │ │ └── watermaskdemo
│ │ │ └── ExampleInstrumentedTest.java
│ ├── proguard-rules.pro
│ └── build.gradle
├── settings.gradle
├── .idea
│ ├── copyright
│ │ └── profiles_settings.xml
│ ├── modules.xml
│ ├── runConfigurations.xml
│ ├── gradle.xml
│ ├── compiler.xml
│ └── misc.xml
├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
├── .gitignore
├── build.gradle
├── gradle.properties
├── gradlew.bat
└── gradlew
├── .gitignore
└── README.md
/WaterMaskDemo/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/WaterMaskDemo/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/WaterMaskDemo/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | WaterMaskDemo
3 |
4 |
--------------------------------------------------------------------------------
/WaterMaskDemo/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/echoMu/WaterMaskDemo/HEAD/WaterMaskDemo/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/mipmap-xhdpi/pic_03.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/echoMu/WaterMaskDemo/HEAD/WaterMaskDemo/app/src/main/res/mipmap-xhdpi/pic_03.png
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/echoMu/WaterMaskDemo/HEAD/WaterMaskDemo/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/echoMu/WaterMaskDemo/HEAD/WaterMaskDemo/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/echoMu/WaterMaskDemo/HEAD/WaterMaskDemo/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/echoMu/WaterMaskDemo/HEAD/WaterMaskDemo/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/echoMu/WaterMaskDemo/HEAD/WaterMaskDemo/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/WaterMaskDemo/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/echoMu/WaterMaskDemo/HEAD/WaterMaskDemo/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/echoMu/WaterMaskDemo/HEAD/WaterMaskDemo/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/echoMu/WaterMaskDemo/HEAD/WaterMaskDemo/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/echoMu/WaterMaskDemo/HEAD/WaterMaskDemo/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/echoMu/WaterMaskDemo/HEAD/WaterMaskDemo/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/WaterMaskDemo/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Jul 28 10:04:45 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/WaterMaskDemo/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/test/java/com/echo/watermaskdemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.echo.watermaskdemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/WaterMaskDemo/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.3.0'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/WaterMaskDemo/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/WaterMaskDemo/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/WaterMaskDemo/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/WaterMaskDemo/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/androidTest/java/com/echo/watermaskdemo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.echo.watermaskdemo;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.echo.watermaskdemo", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # Intellij
36 | *.iml
37 | .idea/workspace.xml
38 | .idea/tasks.xml
39 | .idea/gradle.xml
40 | .idea/dictionaries
41 | .idea/libraries
42 |
43 | # Keystore files
44 | *.jks
45 |
46 | # External native build folder generated in Android Studio 2.2 and later
47 | .externalNativeBuild
48 |
49 | # Google Services (e.g. APIs or Firebase)
50 | google-services.json
51 |
52 | # Freeline
53 | freeline.py
54 | freeline/
55 | freeline_project_description.json
56 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\caizyyz\AppData\Local\Android\sdk5/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "com.echo.watermaskdemo"
8 | minSdkVersion 17
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:25.3.1'
28 | compile 'com.android.support.constraint:constraint-layout:1.0.2'
29 | testCompile 'junit:junit:4.12'
30 | }
31 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
24 |
25 |
30 |
31 |
36 |
37 |
42 |
43 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/res/layout/layout_watermask.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
12 |
23 |
24 |
34 |
35 |
36 |
50 |
51 |
--------------------------------------------------------------------------------
/WaterMaskDemo/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/WaterMaskDemo/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 | 1.7
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/java/com/echo/watermaskdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.echo.watermaskdemo;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.BitmapFactory;
5 | import android.os.Bundle;
6 | import android.support.annotation.IdRes;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.widget.ImageView;
9 | import android.widget.RadioGroup;
10 | import android.widget.RelativeLayout;
11 |
12 | public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
13 | private ImageView ivWaterMark;
14 | private RadioGroup rgWaterMark;
15 |
16 | private WaterMaskView waterMaskView;
17 | private Bitmap sourBitmap;
18 | private Bitmap waterBitmap;
19 | private Bitmap watermarkBitmap;
20 |
21 | private final static int LEFT_TOP=0;
22 | private final static int RIGHT_TOP=1;
23 | private final static int RIGHT_BOTTOM=2;
24 | private final static int LEFT_BOTTOM=3;
25 |
26 | @Override
27 | protected void onCreate(Bundle savedInstanceState) {
28 | super.onCreate(savedInstanceState);
29 | setContentView(R.layout.activity_main);
30 |
31 | ivWaterMark= (ImageView) findViewById(R.id.wartermark_pic);
32 | rgWaterMark= (RadioGroup) findViewById(R.id.rg_wartermark);
33 |
34 | rgWaterMark.setOnCheckedChangeListener(this);
35 |
36 | sourBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic_03);
37 |
38 | waterMaskView = new WaterMaskView(this);
39 | RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
40 | waterMaskView.setLayoutParams(params);
41 | waterMaskView.setCompanyText("XXXXXX公司");
42 | waterMaskView.setInfoText("这是相关信息1这是相关信息2这是相关信息3这是相关信息4这是相关信息5");
43 |
44 | //默认设置水印位置在左下
45 | saveWaterMask(LEFT_BOTTOM);
46 | }
47 |
48 | /**
49 | * @param position 左上为0,顺时针算起
50 | */
51 | private void saveWaterMask(int position) {
52 | waterBitmap = WaterMaskUtil.convertViewToBitmap(waterMaskView);
53 |
54 | //根据原图处理要生成的水印的宽高
55 | float width = sourBitmap.getWidth();
56 | float height = sourBitmap.getHeight();
57 | float be = width / height;
58 |
59 | if ((float) 16 / 9 >= be && be >= (float) 4 / 3) {
60 | //在图片比例区间内16;9~4:3内,将生成的水印bitmap设置为原图宽高各自的1/5
61 | waterBitmap = WaterMaskUtil.zoomBitmap(waterBitmap, (int) width / 5, (int) height / 5);
62 | } else if (be > (float) 16 / 9) {
63 | //生成4:3的水印
64 | waterBitmap = WaterMaskUtil.zoomBitmap(waterBitmap, (int) width / 5, (int) width*3 / 20);
65 | } else if (be < (float) 4 / 3) {
66 | //生成4:3的水印
67 | waterBitmap = WaterMaskUtil.zoomBitmap(waterBitmap, (int) height*4 / 15, (int) height / 5);
68 | }
69 |
70 | switch (position) {
71 | case LEFT_TOP:
72 | watermarkBitmap = WaterMaskUtil.createWaterMaskLeftTop(this, sourBitmap, waterBitmap, 0, 0);
73 | break;
74 | case RIGHT_TOP:
75 | watermarkBitmap = WaterMaskUtil.createWaterMaskRightTop(this, sourBitmap, waterBitmap, 0, 0);
76 | break;
77 | case RIGHT_BOTTOM:
78 | watermarkBitmap = WaterMaskUtil.createWaterMaskRightBottom(this, sourBitmap, waterBitmap, 0, 0);
79 | break;
80 | case LEFT_BOTTOM:
81 | watermarkBitmap = WaterMaskUtil.createWaterMaskLeftBottom(this, sourBitmap, waterBitmap, 0, 0);
82 | break;
83 | }
84 | ivWaterMark.setImageBitmap(watermarkBitmap);
85 | }
86 |
87 | @Override
88 | public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
89 | switch (i){
90 | case R.id.rb_wartermark_lefttop:
91 | saveWaterMask(LEFT_TOP);
92 | break;
93 | case R.id.rb_wartermark_righttop:
94 | saveWaterMask(RIGHT_TOP);
95 | break;
96 | case R.id.rb_wartermark_rightbottom:
97 | saveWaterMask(RIGHT_BOTTOM);
98 | break;
99 | case R.id.rb_wartermark_leftbottom:
100 | saveWaterMask(LEFT_BOTTOM);
101 | break;
102 | }
103 | }
104 |
105 | @Override
106 | protected void onDestroy() {
107 | super.onDestroy();
108 |
109 | if (sourBitmap != null) {
110 | sourBitmap.recycle();
111 | sourBitmap = null;
112 | }
113 | if (waterBitmap != null) {
114 | waterBitmap.recycle();
115 | waterBitmap = null;
116 | }
117 | }
118 |
119 | }
120 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/java/com/echo/watermaskdemo/WaterMaskView.java:
--------------------------------------------------------------------------------
1 | package com.echo.watermaskdemo;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Color;
6 | import android.text.TextUtils;
7 | import android.util.AttributeSet;
8 | import android.view.LayoutInflater;
9 | import android.widget.Button;
10 | import android.widget.RelativeLayout;
11 | import android.widget.TextView;
12 |
13 | /**
14 | * 自定义水印view
15 | * 支持设置logo、公司名称、相关信息
16 | */
17 | public class WaterMaskView extends RelativeLayout {
18 | private Button btLogoImg;
19 | private TextView tvCompanyText;
20 | private TextView tvInfoText;
21 |
22 | /**
23 | * logo的图片资源ID
24 | */
25 | private int logoButtonDrawable;
26 | /**
27 | * 公司名称字符串
28 | */
29 | private String companyText;
30 | /**
31 | * 相关信息字符串
32 | */
33 | private String infoText;
34 |
35 | public WaterMaskView(Context context) {
36 | this(context, null);
37 | }
38 |
39 | public WaterMaskView(Context context, AttributeSet attrs) {
40 | super(context, attrs);
41 |
42 | LayoutInflater.from(context).inflate(R.layout.layout_watermask, this, true);
43 | btLogoImg = (Button) findViewById(R.id.bt_logo_img);
44 | tvCompanyText = (TextView) findViewById(R.id.tv_company_text);
45 | tvInfoText = (TextView) findViewById(R.id.tv_info_text);
46 |
47 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WaterMaskView);
48 | if (typedArray != null) {
49 | //logo和公司名称这一栏的背景色
50 | int titleBarBackGround = typedArray.getColor(R.styleable.WaterMaskView_title_background_color, Color.WHITE);
51 | setBackgroundColor(titleBarBackGround);
52 |
53 | //设置logo的文字
54 | String logoButtonText = typedArray.getString(R.styleable.WaterMaskView_logo_button_text);
55 | if (!TextUtils.isEmpty(logoButtonText)) {
56 | btLogoImg.setText(logoButtonText);
57 | //设置logo文字颜色
58 | int logoButtonTextColor = typedArray.getColor(R.styleable.WaterMaskView_logo_button_text_color, Color.WHITE);
59 | btLogoImg.setTextColor(logoButtonTextColor);
60 | } else {
61 | //设置logo的icon 这里是二选一 要么只能是文字 要么只能是图片
62 | logoButtonDrawable = typedArray.getResourceId(R.styleable.WaterMaskView_logo_button_drawable, R.mipmap.ic_launcher_round);
63 | if (logoButtonDrawable != -1) {
64 | btLogoImg.setBackgroundResource(logoButtonDrawable);
65 | }
66 | }
67 |
68 | //获取公司名称
69 | companyText = typedArray.getString(R.styleable.WaterMaskView_company_text);
70 | if (!TextUtils.isEmpty(companyText)) {
71 | tvCompanyText.setText(companyText);
72 | }
73 | //获取公司名称显示颜色
74 | int titleTextColor = typedArray.getColor(R.styleable.WaterMaskView_title_text_color, Color.WHITE);
75 | tvCompanyText.setTextColor(titleTextColor);
76 |
77 | //设置相关信息文字
78 | if (!TextUtils.isEmpty(infoText)) {
79 | tvInfoText.setText(infoText);
80 | //设置相关信息文字颜色
81 | int infoTextColor = typedArray.getColor(R.styleable.WaterMaskView_info_text_color, Color.WHITE);
82 | tvInfoText.setTextColor(infoTextColor);
83 | } else {
84 | infoText = typedArray.getString(R.styleable.WaterMaskView_info_text);
85 |
86 | if (!TextUtils.isEmpty(infoText)) {
87 | tvInfoText.setText(infoText);
88 | //设置相关信息文字颜色
89 | int infoTextColor = typedArray.getColor(R.styleable.WaterMaskView_info_text_color, Color.WHITE);
90 | tvInfoText.setTextColor(infoTextColor);
91 | }
92 | }
93 |
94 | typedArray.recycle();
95 | }
96 |
97 | }
98 |
99 | /**
100 | * 设置logo的图片资源ID
101 | *
102 | * @param btLogoImg
103 | */
104 | public void setBtLogoImg(Button btLogoImg) {
105 | this.btLogoImg = btLogoImg;
106 | btLogoImg.setBackgroundResource(logoButtonDrawable);
107 | }
108 |
109 | /**
110 | * 设置公司名称文字
111 | *
112 | * @param companyText
113 | */
114 | public void setCompanyText(String companyText) {
115 | this.companyText = companyText;
116 | tvCompanyText.setText(companyText);
117 | }
118 |
119 | /**
120 | * 设置相关信息文字
121 | *
122 | * @param infoText
123 | */
124 | public void setInfoText(String infoText) {
125 | this.infoText = infoText;
126 | tvInfoText.setText(infoText);
127 | }
128 |
129 | }
130 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # WaterMaskDemo
2 | Android 给图片加上水印(支持logo+文字)吧!
3 |
4 | 现在我们想要往图片上打上水印,该水印应符合这样的需求的:
5 | 1. 支持logo+文字;
6 | 2. 文字信息支持多行展示;
7 | 3. 用户可以选择水印在图片上的生成位置(左上、右上、右下和左下)。
8 |
9 | 粗略的结构图低配版大概就长这样...
10 |
11 | 
12 |
13 | 现在提供这样的一种思路去实现这一个需求,我们可以通过自定义一个view,view的布局中包含logo、公司名称和相关信息,这个view就是我们要打上图片的水印。
14 |
15 | 这样的一个view其实是一个自定义组合布局,关于如何实现组合布局的自定义view,可以参考这篇文章:[Android 自定义View实践之组合控件实现布局的复用](http://www.jianshu.com/p/d378523a00d9)
16 |
17 | 有了水印的view之后,我们就可以利用以下这个方法,得到水印的view的Bitmap。
18 |
19 | /**
20 | * 将一个view转换为Bitmap
21 | * @param view
22 | * @return
23 | */
24 | public static Bitmap convertViewToBitmap(View view){
25 | view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
26 | view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
27 | view.buildDrawingCache();
28 | Bitmap bitmap = view.getDrawingCache();
29 | return bitmap;
30 | }
31 |
32 |
33 | 得到水印的view的Bitmap之后,处理一下尺寸,保持水印的尺寸在合理范围内。
34 |
35 | //根据原图处理要生成的水印的宽高
36 | float width = sourBitmap.getWidth();
37 | float height = sourBitmap.getHeight();
38 | float be = width / height;
39 |
40 | if ((float) 16 / 9 >= be && be >= (float) 4 / 3) {
41 | //在图片比例区间内16;9~4:3内,将生成的水印bitmap设置为原图宽高各自的1/5
42 | waterBitmap = WaterMaskUtil.zoomBitmap(waterBitmap, (int) width / 5, (int) height / 5);
43 | } else if (be > (float) 16 / 9) {
44 | //生成4:3的水印
45 | waterBitmap = WaterMaskUtil.zoomBitmap(waterBitmap, (int) width / 5, (int) width*3 / 20);
46 | } else if (be < (float) 4 / 3) {
47 | //生成4:3的水印
48 | waterBitmap = WaterMaskUtil.zoomBitmap(waterBitmap, (int) height*4 / 15, (int) height / 5);
49 | }
50 |
51 | 然后将它按照要求绘制在原图上,提供生成左上、右上、右下和左下四个位置的水印各自的方法,关键代码如下:
52 |
53 | /**
54 | * 设置水印图片在左上角
55 | * @param src
56 | * @param watermark
57 | * @param paddingLeft
58 | * @param paddingTop
59 | * @return
60 | */
61 | public static Bitmap createWaterMaskLeftTop(
62 | Context context, Bitmap src, Bitmap watermark,
63 | int paddingLeft, int paddingTop) {
64 | return createWaterMaskBitmap(src, watermark,
65 | dp2px(context, paddingLeft), dp2px(context, paddingTop));
66 | }
67 |
68 | /**
69 | * 设置水印图片在右下角
70 | * @param src
71 | * @param watermark
72 | * @param paddingRight
73 | * @param paddingBottom
74 | * @return
75 | */
76 | public static Bitmap createWaterMaskRightBottom(
77 | Context context, Bitmap src, Bitmap watermark,
78 | int paddingRight, int paddingBottom) {
79 | return createWaterMaskBitmap(src, watermark,
80 | src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
81 | src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
82 | }
83 |
84 | /**
85 | * 设置水印图片到右上角
86 | * @param src
87 | * @param watermark
88 | * @param paddingRight
89 | * @param paddingTop
90 | * @return
91 | */
92 | public static Bitmap createWaterMaskRightTop(
93 | Context context, Bitmap src, Bitmap watermark,
94 | int paddingRight, int paddingTop) {
95 | return createWaterMaskBitmap( src, watermark,
96 | src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
97 | dp2px(context, paddingTop));
98 | }
99 |
100 | /**
101 | * 设置水印图片到左下角
102 | * @param src
103 | * @param watermark
104 | * @param paddingLeft
105 | * @param paddingBottom
106 | * @return
107 | */
108 | public static Bitmap createWaterMaskLeftBottom(
109 | Context context, Bitmap src, Bitmap watermark,
110 | int paddingLeft, int paddingBottom) {
111 | return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft),
112 | src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
113 | }
114 |
115 | /**
116 | * 绘制水印图片
117 | * @param src 原图
118 | * @param watermark 水印
119 | * @param paddingLeft
120 | * @param paddingTop
121 | * @return
122 | */
123 | private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark,
124 | int paddingLeft, int paddingTop) {
125 | if (src == null) {
126 | return null;
127 | }
128 | int width = src.getWidth();
129 | int height = src.getHeight();
130 | //创建一个bitmap
131 | Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
132 | //将该图片作为画布
133 | Canvas canvas = new Canvas(newb);
134 | //在画布 0,0坐标上开始绘制原始图片
135 | canvas.drawBitmap(src, 0, 0, null);
136 | //在画布上绘制水印图片
137 | canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
138 | // 保存
139 | canvas.save(Canvas.ALL_SAVE_FLAG);
140 | // 存储
141 | canvas.restore();
142 | return newb;
143 | }
144 |
145 | 绘制后的效果应该是这样的,欢迎拍砖~
146 |
147 | 
148 |
149 | 附上源代码:[github传送门](https://github.com/echoMu/WaterMaskDemo)
--------------------------------------------------------------------------------
/WaterMaskDemo/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/WaterMaskDemo/app/src/main/java/com/echo/watermaskdemo/WaterMaskUtil.java:
--------------------------------------------------------------------------------
1 | package com.echo.watermaskdemo;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.Canvas;
6 | import android.graphics.Matrix;
7 | import android.graphics.Paint;
8 | import android.graphics.Rect;
9 | import android.view.View;
10 |
11 | /**
12 | *
13 | * author : echoMu
14 | * e-mail :
15 | * time : 2017/07/12
16 | * desc :
17 | * version:
18 | *
19 | */
20 | public class WaterMaskUtil {
21 | /**
22 | * 设置水印图片在左上角
23 | * @param src
24 | * @param watermark
25 | * @param paddingLeft
26 | * @param paddingTop
27 | * @return
28 | */
29 | public static Bitmap createWaterMaskLeftTop(
30 | Context context, Bitmap src, Bitmap watermark,
31 | int paddingLeft, int paddingTop) {
32 | return createWaterMaskBitmap(src, watermark,
33 | dp2px(context, paddingLeft), dp2px(context, paddingTop));
34 | }
35 |
36 | /**
37 | * 设置水印图片在右下角
38 | * @param src
39 | * @param watermark
40 | * @param paddingRight
41 | * @param paddingBottom
42 | * @return
43 | */
44 | public static Bitmap createWaterMaskRightBottom(
45 | Context context, Bitmap src, Bitmap watermark,
46 | int paddingRight, int paddingBottom) {
47 | return createWaterMaskBitmap(src, watermark,
48 | src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
49 | src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
50 | }
51 |
52 | /**
53 | * 设置水印图片到右上角
54 | * @param src
55 | * @param watermark
56 | * @param paddingRight
57 | * @param paddingTop
58 | * @return
59 | */
60 | public static Bitmap createWaterMaskRightTop(
61 | Context context, Bitmap src, Bitmap watermark,
62 | int paddingRight, int paddingTop) {
63 | return createWaterMaskBitmap( src, watermark,
64 | src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
65 | dp2px(context, paddingTop));
66 | }
67 |
68 | /**
69 | * 设置水印图片到左下角
70 | * @param src
71 | * @param watermark
72 | * @param paddingLeft
73 | * @param paddingBottom
74 | * @return
75 | */
76 | public static Bitmap createWaterMaskLeftBottom(
77 | Context context, Bitmap src, Bitmap watermark,
78 | int paddingLeft, int paddingBottom) {
79 | return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft),
80 | src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
81 | }
82 |
83 | /**
84 | * 设置水印图片到中间
85 | * @param src
86 | * @param watermark
87 | * @return
88 | */
89 | public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
90 | return createWaterMaskBitmap(src, watermark,
91 | (src.getWidth() - watermark.getWidth()) / 2,
92 | (src.getHeight() - watermark.getHeight()) / 2);
93 | }
94 |
95 | /**
96 | * 绘制水印图片
97 | * @param src 原图
98 | * @param watermark 水印
99 | * @param paddingLeft
100 | * @param paddingTop
101 | * @return
102 | */
103 | private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark,
104 | int paddingLeft, int paddingTop) {
105 | if (src == null) {
106 | return null;
107 | }
108 | int width = src.getWidth();
109 | int height = src.getHeight();
110 | //创建一个bitmap
111 | Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
112 | //将该图片作为画布
113 | Canvas canvas = new Canvas(newb);
114 | //在画布 0,0坐标上开始绘制原始图片
115 | canvas.drawBitmap(src, 0, 0, null);
116 | //在画布上绘制水印图片
117 | canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
118 | // 保存
119 | canvas.save(Canvas.ALL_SAVE_FLAG);
120 | // 存储
121 | canvas.restore();
122 | return newb;
123 | }
124 |
125 | /**
126 | * 给图片添加文字到左上角
127 | * @param context
128 | * @param bitmap
129 | * @param text
130 | * @return
131 | */
132 | public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap, String text,
133 | int size, int color, int paddingLeft, int paddingTop) {
134 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
135 | paint.setColor(color);
136 | paint.setTextSize(dp2px(context, size));
137 | Rect bounds = new Rect();
138 | paint.getTextBounds(text, 0, text.length(), bounds);
139 | return drawTextToBitmap(context, bitmap, text, paint, bounds,
140 | dp2px(context, paddingLeft),
141 | dp2px(context, paddingTop) + bounds.height());
142 | }
143 |
144 | /**
145 | * 绘制文字到右下角
146 | * @param context
147 | * @param bitmap
148 | * @param text
149 | * @param size
150 | * @param color
151 | * @param paddingRight
152 | * @param paddingBottom
153 | * @return
154 | */
155 | public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap, String text,
156 | int size, int color, int paddingRight, int paddingBottom) {
157 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
158 | paint.setColor(color);
159 | paint.setTextSize(dp2px(context, size));
160 | Rect bounds = new Rect();
161 | paint.getTextBounds(text, 0, text.length(), bounds);
162 | return drawTextToBitmap(context, bitmap, text, paint, bounds,
163 | bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
164 | bitmap.getHeight() - dp2px(context, paddingBottom));
165 | }
166 |
167 | /**
168 | * 绘制文字到右上方
169 | * @param context
170 | * @param bitmap
171 | * @param text
172 | * @param size
173 | * @param color
174 | * @param paddingRight
175 | * @param paddingTop
176 | * @return
177 | */
178 | public static Bitmap drawTextToRightTop(Context context, Bitmap bitmap, String text,
179 | int size, int color, int paddingRight, int paddingTop) {
180 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
181 | paint.setColor(color);
182 | paint.setTextSize(dp2px(context, size));
183 | Rect bounds = new Rect();
184 | paint.getTextBounds(text, 0, text.length(), bounds);
185 | return drawTextToBitmap(context, bitmap, text, paint, bounds,
186 | bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
187 | dp2px(context, paddingTop) + bounds.height());
188 | }
189 |
190 | /**
191 | * 绘制文字到左下方
192 | * @param context
193 | * @param bitmap
194 | * @param text
195 | * @param size
196 | * @param color
197 | * @param paddingLeft
198 | * @param paddingBottom
199 | * @return
200 | */
201 | public static Bitmap drawTextToLeftBottom(Context context, Bitmap bitmap, String text,
202 | int size, int color, int paddingLeft, int paddingBottom) {
203 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
204 | paint.setColor(color);
205 | paint.setTextSize(dp2px(context, size));
206 | Rect bounds = new Rect();
207 | paint.getTextBounds(text, 0, text.length(), bounds);
208 | return drawTextToBitmap(context, bitmap, text, paint, bounds,
209 | dp2px(context, paddingLeft),
210 | bitmap.getHeight() - dp2px(context, paddingBottom+2));
211 | }
212 |
213 | /**
214 | * 绘制文字到中间
215 | * @param context
216 | * @param bitmap
217 | * @param text
218 | * @param size
219 | * @param color
220 | * @return
221 | */
222 | public static Bitmap drawTextToCenter(Context context, Bitmap bitmap, String text,
223 | int size, int color) {
224 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
225 | paint.setColor(color);
226 | paint.setTextSize(dp2px(context, size));
227 | Rect bounds = new Rect();
228 | paint.getTextBounds(text, 0, text.length(), bounds);
229 | return drawTextToBitmap(context, bitmap, text, paint, bounds,
230 | (bitmap.getWidth() - bounds.width()) / 2,
231 | (bitmap.getHeight() + bounds.height()) / 2);
232 | }
233 |
234 | //图片上绘制文字
235 | private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text,
236 | Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
237 | android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
238 |
239 | paint.setDither(true); // 获取跟清晰的图像采样
240 | paint.setFilterBitmap(true);// 过滤一些
241 | if (bitmapConfig == null) {
242 | bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
243 | }
244 | bitmap = bitmap.copy(bitmapConfig, true);
245 | Canvas canvas = new Canvas(bitmap);
246 |
247 | // canvas.drawText(text, paddingLeft, paddingTop, paint);
248 | drawMultiLineText(text, paddingLeft, paddingTop, paint,canvas);
249 | return bitmap;
250 | }
251 |
252 | private static void drawMultiLineText(String str, float x, float y, Paint paint,
253 | Canvas canvas) {
254 | String[] lines = str.split("\n");
255 | float txtSize = -paint.ascent() + paint.descent();
256 |
257 | if (paint.getStyle() == Paint.Style.FILL_AND_STROKE
258 | || paint.getStyle() == Paint.Style.STROKE) {
259 | txtSize += paint.getStrokeWidth(); // add stroke width to the text
260 | }
261 | float lineSpace = txtSize * 0.1f; // default line spacing
262 | for (int i = 0; i < lines.length; ++i) {
263 | canvas.drawText(lines[i], x, y + (txtSize + lineSpace) * i, paint);
264 | }
265 | }
266 |
267 | /**
268 | * 缩放图片
269 | * @param src
270 | * @param w
271 | * @param h
272 | * @return
273 | */
274 | public static Bitmap scaleWithWH(Bitmap src, double w, double h) {
275 | if (w == 0 || h == 0 || src == null) {
276 | return src;
277 | } else {
278 | // 记录src的宽高
279 | int width = src.getWidth();
280 | int height = src.getHeight();
281 | // 创建一个matrix容器
282 | Matrix matrix = new Matrix();
283 | // 计算缩放比例
284 | float scaleWidth = (float) (w / width);
285 | float scaleHeight = (float) (h / height);
286 | // 开始缩放
287 | matrix.postScale(scaleWidth, scaleHeight);
288 | // 创建缩放后的图片
289 | return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);
290 | }
291 | }
292 |
293 | /**
294 | * dip转pix
295 | * @param context
296 | * @param dp
297 | * @return
298 | */
299 | public static int dp2px(Context context, float dp) {
300 | final float scale = context.getResources().getDisplayMetrics().density;
301 | return (int) (dp * scale + 0.5f);
302 | }
303 |
304 | /**
305 | * 将一个view转换为Bitmap
306 | * @param view
307 | * @return
308 | */
309 | public static Bitmap convertViewToBitmap(View view){
310 | view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
311 | view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
312 | view.buildDrawingCache();
313 | Bitmap bitmap = view.getDrawingCache();
314 | return bitmap;
315 | }
316 |
317 | /**
318 | * 缩放Bitmap图片
319 | *
320 | */
321 | public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
322 |
323 | int w = bitmap.getWidth();
324 | int h = bitmap.getHeight();
325 | Matrix matrix = new Matrix();
326 | float scaleWidth = ((float) width / w);
327 | float scaleHeight = ((float) height / h);
328 | matrix.postScale(scaleWidth, scaleHeight);// 利用矩阵进行缩放不会造成内存溢出
329 | Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
330 |
331 | return newbmp;
332 | }
333 |
334 | }
335 |
--------------------------------------------------------------------------------