Skip to content

Commit

Permalink
Merge pull request #4 from beigirad/dev
Browse files Browse the repository at this point in the history
add TopZigzag
  • Loading branch information
beigirad committed May 15, 2018
2 parents 1b86872 + 71fe278 commit 9e7d31e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 28 deletions.
93 changes: 65 additions & 28 deletions library/src/main/java/ir/beigirad/zigzagview/ZigzagView.java
Expand Up @@ -27,6 +27,9 @@
import static android.graphics.PorterDuff.Mode.SRC_IN;

public class ZigzagView extends FrameLayout {
private static final int ZIGZAG_TOP = 1;
private static final int ZIGZAG_BOTTOM = 2; // default to be backward compatible.Like google ;)

private final String TAG = this.getClass().getSimpleName();
private int zigzagHeight;
private int zigzagElevation;
Expand All @@ -37,6 +40,7 @@ public class ZigzagView extends FrameLayout {
private int zigzagPaddingRight;
private int zigzagPaddingTop;
private int zigzagPaddingBottom;
private int zigzagSides;

private Path pathZigzag = new Path();
private Paint paintZigzag;
Expand Down Expand Up @@ -82,6 +86,7 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr, int def
this.zigzagPaddingRight = (int) a.getDimension(R.styleable.ZigzagView_zigzagPaddingRight, zigzagPadding);
this.zigzagPaddingTop = (int) a.getDimension(R.styleable.ZigzagView_zigzagPaddingTop, zigzagPadding);
this.zigzagPaddingBottom = (int) a.getDimension(R.styleable.ZigzagView_zigzagPaddingBottom, zigzagPadding);
this.zigzagSides = a.getInt(R.styleable.ZigzagView_zigzagSides, ZIGZAG_BOTTOM);
a.recycle();

this.paintZigzag = new Paint();
Expand All @@ -105,7 +110,10 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

rectMain.set(0, 0, getMeasuredWidth(), getMeasuredHeight());
rectZigzag.set(rectMain.left + zigzagPaddingLeft, rectMain.top + zigzagPaddingTop, rectMain.right - zigzagPaddingRight, rectMain.bottom - zigzagPaddingBottom);
rectContent.set(rectZigzag.left + zigzagPaddingContent, rectZigzag.top + zigzagPaddingContent, rectZigzag.right - zigzagPaddingContent, rectZigzag.bottom - zigzagPaddingContent - zigzagHeight);
rectContent.set(rectZigzag.left + zigzagPaddingContent,
rectZigzag.top + zigzagPaddingContent + (containsSide(zigzagSides, ZIGZAG_TOP) ? zigzagHeight : 0),
rectZigzag.right - zigzagPaddingContent,
rectZigzag.bottom - zigzagPaddingContent - (containsSide(zigzagSides, ZIGZAG_BOTTOM) ? zigzagHeight : 0));

super.setPadding(rectContent.left, rectContent.top, rectMain.right - rectContent.right, rectMain.bottom - rectContent.bottom);
}
Expand All @@ -115,7 +123,7 @@ protected void onDraw(Canvas canvas) {

drawZigzag();

if (zigzagElevation > 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (zigzagElevation > 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
drawShadow();
canvas.drawBitmap(shadow, 0, 0, null);
}
Expand All @@ -128,40 +136,24 @@ private void drawZigzag() {
float right = rectZigzag.right;
float top = rectZigzag.top;
float bottom = rectZigzag.bottom;
int width = (int) (right - left);

pathZigzag.moveTo(right, bottom);
pathZigzag.lineTo(right, top);
pathZigzag.lineTo(left, top);
pathZigzag.lineTo(left, bottom);

int h = zigzagHeight;
int seed = 2 * h;
int count = width / seed;
int diff = width - (seed * count);
int sideDiff = diff / 2;


float x = (float) (seed / 2);
float upHeight = bottom - h;
float downHeight = bottom;
if (containsSide(zigzagSides, ZIGZAG_TOP))
drawHorizontalSide(pathZigzag, left, top, right, true);
else
pathZigzag.lineTo(top, left);

for (int i = 0; i < count; i++) {
int startSeed = (i * seed) + sideDiff + (int) left;
int endSeed = startSeed + seed;

if (i == 0) {
startSeed = (int) left + sideDiff;
} else if (i == count - 1) {
endSeed = endSeed + sideDiff;
}
pathZigzag.lineTo(left, bottom);

this.pathZigzag.lineTo(startSeed + x, upHeight);
this.pathZigzag.lineTo(endSeed, downHeight);
}
if (containsSide(zigzagSides, ZIGZAG_BOTTOM))
drawHorizontalSide(pathZigzag, left, bottom, right, false);
else
pathZigzag.lineTo(right, bottom);
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void drawShadow() {
shadow = Bitmap.createBitmap(getWidth(), getHeight(), ALPHA_8);
shadow.eraseColor(TRANSPARENT);
Expand All @@ -181,4 +173,49 @@ private void drawShadow() {

}

private void drawHorizontalSide(Path path, float left, float y, float right, boolean isTop) {
int h = zigzagHeight;
int seed = 2 * h;
int width = (int) (right - left);
int count = width / seed;
int diff = width - (seed * count);
int sideDiff = diff / 2;

float halfSeed = (float) (seed / 2);
float innerHeight = isTop ? y + h : y - h;

if (isTop) {
for (int i = count; i > 0; i--) {
int startSeed = (i * seed) + sideDiff + (int) left;
int endSeed = startSeed - seed;

if (i == 1) {
endSeed = endSeed - sideDiff;
}

path.lineTo(startSeed - halfSeed, innerHeight);
path.lineTo(endSeed, y);
}
} else {
for (int i = 0; i < count; i++) {
int startSeed = (i * seed) + sideDiff + (int) left;
int endSeed = startSeed + seed;

if (i == 0) {
startSeed = (int) left + sideDiff;
} else if (i == count - 1) {
endSeed = endSeed + sideDiff;
}

path.lineTo(startSeed + halfSeed, innerHeight);
path.lineTo(endSeed, y);
}
}


}

private boolean containsSide(int flagSet, int flag) {
return (flagSet | flag) == flagSet;
}
}
4 changes: 4 additions & 0 deletions library/src/main/res/values/attrs.xml
Expand Up @@ -10,5 +10,9 @@
<attr name="zigzagPaddingRight" format="dimension" />
<attr name="zigzagPaddingBottom" format="dimension" />
<attr name="zigzagPaddingTop" format="dimension" />
<attr name="zigzagSides">
<flag name="top" value="1" />
<flag name="bottom" value="2" />
</attr>
</declare-styleable>
</resources>
1 change: 1 addition & 0 deletions sample/src/main/res/layout/activity_main.xml
Expand Up @@ -13,6 +13,7 @@
app:zigzagBackgroundColor="#8bc34a"
app:zigzagElevation="8dp"
app:zigzagHeight="10dp"
app:zigzagSides="top|bottom"
app:zigzagPaddingContent="16dp">

<TextView
Expand Down

0 comments on commit 9e7d31e

Please sign in to comment.