Skip to content

Commit

Permalink
Added comments and other nits
Browse files Browse the repository at this point in the history
  • Loading branch information
vinaygaba committed Jul 4, 2017
1 parent 642fdcd commit 1869a7f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
/**
* Copyright (C) 2017 Vinay Gaba
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/*
Copyright (C) 2017 Vinay Gaba
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.vinaygaba.rubberstamp;

import android.util.Pair;

public class PositionCalculator {

/**
* Utility method to calculate the coordinates of the rubberstamp based on the params passed to
* it.
* @param location The RubberStampPosition which denotes the position of the watermark
* @param bitmapWidth The width of the bitmap where the rubberstamp will be drawn
* @param bitmapHeight The height of the bitmap where the rubberstamp will be drawn
* @param rubberstampWidth The width of the rubberstamp
* @param rubberstampHeight The height of the rubberstamp
* @return Returns a Pair<Integer, Integer> object which has the x-coordinate and the y-coordinate
*/
public static Pair<Integer, Integer> getCoordinates(RubberStampPosition location,
int bitmapWidth, int bitmapHeight,
int rubberstampWidth, int rubberstampHeight) {
switch(location){

case TOP_LEFT:
return new Pair<>(0, rubberstampHeight);

Expand Down
73 changes: 48 additions & 25 deletions library/src/main/java/com/vinaygaba/rubberstamp/RubberStamp.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Copyright (C) 2017 Vinay Gaba
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/*
Copyright (C) 2017 Vinay Gaba
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.vinaygaba.rubberstamp;
Expand All @@ -26,6 +26,7 @@
import android.graphics.Shader;
import android.graphics.Typeface;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Pair;
Expand All @@ -36,14 +37,18 @@

public class RubberStamp {

static Context mContext;
public static final int BACKGROUND_MARGIN = 10;
private Context mContext;
private static final int BACKGROUND_MARGIN = 10;

public RubberStamp(Context context){
public RubberStamp(@NonNull Context context){
mContext = context;
}

public Bitmap addStamp(RubberStampConfig config) {
public Bitmap addStamp(@NonNull RubberStampConfig config) {
if (config == null) {
throw new IllegalArgumentException("The config passed to this method should never" +
"be null");
}
Bitmap baseBitmap = getBaseBitmap(config);
if (baseBitmap == null) {
return baseBitmap;
Expand All @@ -58,17 +63,15 @@ public Bitmap addStamp(RubberStampConfig config) {

if (!TextUtils.isEmpty(config.getRubberStampString())) {
addTextToBitmap(config, canvas, baseBitmapWidth, baseBitmapHeight);
}

if (config.getRubberStampBitmap() != null) {
} else if (config.getRubberStampBitmap() != null) {
addBitmapToBitmap(config.getRubberStampBitmap(), config, canvas,
baseBitmapWidth, baseBitmapHeight);
}
return result;
}

@Nullable
private Bitmap getBaseBitmap(RubberStampConfig config) {
private Bitmap getBaseBitmap(@NonNull RubberStampConfig config) {
Bitmap baseBitmap = config.getBaseBitmap();
@DrawableRes int drawable = config.getBaseDrawable();

Expand All @@ -79,7 +82,16 @@ private Bitmap getBaseBitmap(RubberStampConfig config) {
return baseBitmap;
}

private void addTextToBitmap(RubberStampConfig config, Canvas canvas, int baseBitmapWidth,
/**
* Method to add text RubberStamp to a canvas based on the provided configuration
* @param config The RubberStampConfig that specifies how the RubberStamp should look
* @param canvas The canvas on top of which the RubberStamp needs to be drawn
* @param baseBitmapWidth The width of the base bitmap
* @param baseBitmapHeight The height of the base bitmap
*/
private void addTextToBitmap(@NonNull RubberStampConfig config,
@NonNull Canvas canvas,
int baseBitmapWidth,
int baseBitmapHeight) {
Rect bounds = new Rect();

Expand Down Expand Up @@ -168,8 +180,19 @@ private void addTextToBitmap(RubberStampConfig config, Canvas canvas, int baseBi
}
}

private void addBitmapToBitmap(Bitmap rubberStampBitmap, RubberStampConfig config, Canvas canvas,
int baseBitmapWidth, int baseBitmapHeight) {
/**
* Method to add a bitmap RubberStamp to a canvas based on the provided configuration
* @param rubberStampBitmap The bitmap which will be used as the RubberStamp
* @param config The RubberStampConfig that specifies how the RubberStamp should look
* @param canvas The canvas on top of which the RubberStamp needs to be drawn
* @param baseBitmapWidth The width of the base bitmap
* @param baseBitmapHeight The height of the base bitmap
*/
private void addBitmapToBitmap(@NonNull Bitmap rubberStampBitmap,
@NonNull RubberStampConfig config,
@NonNull Canvas canvas,
int baseBitmapWidth,
int baseBitmapHeight) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setUnderlineText(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Copyright (C) 2017 Vinay Gaba
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/*
Copyright (C) 2017 Vinay Gaba
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.vinaygaba.rubberstamp;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
/**
* Copyright (C) 2017 Vinay Gaba
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/*
Copyright (C) 2017 Vinay Gaba
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.vinaygaba.rubberstamp;

/**
* Enums to represent the position configurations of the rubberstamp
*/
public enum RubberStampPosition {
TOP_LEFT, TOP_RIGHT, TOP_CENTER, CENTER_LEFT, CENTER_RIGHT, CENTER, BOTTOM_LEFT, BOTTOM_RIGHT,
BOTTOM_CENTER, CUSTOM, TILE
Expand Down

0 comments on commit 1869a7f

Please sign in to comment.