Skip to content

Commit

Permalink
Gradient Added
Browse files Browse the repository at this point in the history
  • Loading branch information
IntruderShanky committed Dec 16, 2016
1 parent 1635085 commit f53c9f3
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 12 deletions.
Binary file added app/src/main/res/drawable/barney.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 15 additions & 12 deletions app/src/main/res/layout/sample_2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
android:id="@+id/wave_head"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="130dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:alphaValue="80"
app:autoTint="true"
app:src="@drawable/taylor"
android:layout_marginBottom="60dp"
app:alphaValue="50"
app:tintColor="#000000"
app:gradientType="radial"
app:gradient_endColor="#cc000000"
app:gradient_startColor="#00000000"
app:src="@drawable/barney"
app:tideCount="3"
app:tideHeight="40dp"
app:tintColor="#000000" />
app:tideHeight="40dp" />

<LinearLayout
android:layout_width="match_parent"
Expand All @@ -32,16 +34,16 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-smallcaps"
android:text="TAYLOR SWIFT"
android:text="NEIL PATTRIC HARRIS"
android:textColor="#fff"
android:textSize="44sp"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-smallcaps"
android:text="SINGER"
android:text="ACTOR"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
Expand All @@ -50,16 +52,17 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:padding="10dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:orientation="horizontal">
android:visibility="gone"
android:orientation="horizontal"
android:padding="10dp">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#212121"
android:gravity="center"
android:orientation="horizontal">

<ImageView
Expand Down
77 changes: 77 additions & 0 deletions library/src/main/java/com/intrusoft/library/FrissonView.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.Shader;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
Expand All @@ -19,7 +20,10 @@
import android.view.View;

import static com.intrusoft.library.Utils.DEFAULT_ALPHA;
import static com.intrusoft.library.Utils.DEFAULT_ANGLE;
import static com.intrusoft.library.Utils.DEFAULT_TINT;
import static com.intrusoft.library.Utils.END_COLOR;
import static com.intrusoft.library.Utils.START_COLOR;
import static com.intrusoft.library.Utils.TAG_IMAGE;
import static com.intrusoft.library.Utils.TIDE_COUNT;
import static com.intrusoft.library.Utils.TIDE_HEIGHT_DP;
Expand Down Expand Up @@ -53,6 +57,12 @@ public class FrissonView extends View {
private int tideCount;
private int tideHeight;
private int alphaValue;
private GradientType gradientType;
private int gradientStartColor;
private int gradientEndColor;
private int gradientAngle;
private boolean showGradient;
private Shader gradientShader;

public enum ScaleType {
CENTRE_CROP(0),
Expand All @@ -64,8 +74,20 @@ public enum ScaleType {
}
}

public enum GradientType {
LINEAR(0),
RADIAL(1);
final int value;

GradientType(int value) {
this.value = value;
}
}

private static final ScaleType[] scaleTypeArray = {ScaleType.CENTRE_CROP, ScaleType.FIT_XY};

private static final GradientType[] gradientTypeArray = {GradientType.LINEAR, GradientType.RADIAL};

public FrissonView(Context context) {
super(context);
init(context, null);
Expand All @@ -88,6 +110,11 @@ public void init(Context context, AttributeSet attrs) {
alphaValue = DEFAULT_ALPHA;
tintColor = DEFAULT_TINT;
scaleType = ScaleType.CENTRE_CROP;
gradientAngle = DEFAULT_ANGLE;
gradientStartColor = START_COLOR;
gradientEndColor = END_COLOR;
gradientType = GradientType.LINEAR;
showGradient = false;
if (attrs != null) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.FrissonView);
if (array.hasValue(R.styleable.FrissonView_src))
Expand All @@ -112,6 +139,19 @@ public void init(Context context, AttributeSet attrs) {
}
if (array.hasValue(R.styleable.FrissonView_scaleType))
scaleType = scaleTypeArray[array.getInt(R.styleable.FrissonView_scaleType, 0)];
if (array.hasValue(R.styleable.FrissonView_gradient_startColor)) {
gradientStartColor = array.getColor(R.styleable.FrissonView_gradient_startColor, START_COLOR);
showGradient = true;
}
if (array.hasValue(R.styleable.FrissonView_gradient_endColor)) {
gradientEndColor = array.getColor(R.styleable.FrissonView_gradient_endColor, END_COLOR);
showGradient = true;
}
if (array.hasValue(R.styleable.FrissonView_gradientType))
gradientType = gradientTypeArray[array.getInt(R.styleable.FrissonView_gradientType, 0)];
if (array.hasValue(R.styleable.FrissonView_gradientAngle))
if (Math.abs(array.getInt(R.styleable.FrissonView_gradientAngle, DEFAULT_ANGLE)) <= 360)
gradientAngle = Math.abs(array.getInt(R.styleable.FrissonView_gradientAngle, DEFAULT_ANGLE));
array.recycle();
}
tideHeight /= 2;
Expand Down Expand Up @@ -155,6 +195,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (x > 0) x = -x;
if (y > 0) y = -y;
}
if (showGradient)
if (gradientType == GradientType.LINEAR)
gradientShader = GradientGenerator.getLinearGradient(gradientAngle, width, height, gradientStartColor, gradientEndColor);
else
gradientShader = GradientGenerator.getRadialGradient(width, height, gradientStartColor, gradientEndColor);

}

@Override
Expand Down Expand Up @@ -182,6 +228,12 @@ protected void onDraw(final Canvas canvas) {
path = Utils.getWavePath(width, height, tideHeight, i * i * 20, 3);
canvas.drawPath(path, paint);
}
if (showGradient) {
Paint p = new Paint();
p.setShader(gradientShader);
path = Utils.getWavePath(width, height, tideHeight, l * 20, 4);
canvas.drawPath(path, p);
}
}

/**
Expand Down Expand Up @@ -269,4 +321,29 @@ public void setAutoTint(boolean autoTint) {
pickColorFromBitmap(bitmap);
invalidate();
}

/**
* Draw {@link android.graphics.LinearGradient} on top of the image with gradientAngle
*
* @param gradientAngle
* @param gradientStartColor
* @param gradientEndColor
*/
public void setLinearGradient(int gradientAngle, int gradientStartColor, int gradientEndColor) {
showGradient = true;
gradientShader = GradientGenerator.getLinearGradient(gradientAngle, width, height, gradientStartColor, gradientEndColor);
invalidate();
}

/**
* Draw {@link android.graphics.RadialGradient} on top of the image
* @param gradientStartColor
* @param gradientEndColor
*/
public void setRadialGradient(int gradientStartColor, int gradientEndColor) {
showGradient = true;
gradientShader = GradientGenerator.getRadialGradient(width, height, gradientStartColor, gradientEndColor);
invalidate();
}

}
33 changes: 33 additions & 0 deletions library/src/main/java/com/intrusoft/library/GradientGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.intrusoft.library;

import android.graphics.LinearGradient;
import android.graphics.RadialGradient;
import android.graphics.Shader;

/**
* Created by Intruder Shanky on 12/16/2016.
*/

public class GradientGenerator {

public static Shader getRadialGradient(float width, float height, int startColor, int endColor) {
float radius = Math.max(width, height) / 2;
return new RadialGradient(width / 2, height / 2, radius, startColor, endColor, Shader.TileMode.CLAMP);
}

public static Shader getLinearGradient(int angle, float width, float height, int startColor, int endColor) {
float x1 = 0, y1 = 0, y2 = 0;
float x2 = (float) (height / Math.tan(angle));
if (angle <= 180) {
y1 = height;
if (angle > 90)
x1 = width;
} else if (angle <= 360) {
y2 = height;
if (angle <= 270)
x1 = width;
}
return new LinearGradient(x1, y1, x2, y2, startColor, endColor, Shader.TileMode.CLAMP);
}

}
3 changes: 3 additions & 0 deletions library/src/main/java/com/intrusoft/library/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Utils {
final static String TAG_IMAGE = "FrissonView";
final static int TIDE_COUNT = 3;
final static int TIDE_HEIGHT_DP = 30;
final static int DEFAULT_ANGLE = 90;
final static int START_COLOR = Color.BLACK;
final static int END_COLOR = Color.WHITE;

static int getPixelForDp(Context context, int displayPixels) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, displayPixels, context.getResources().getDisplayMetrics());
Expand Down
19 changes: 19 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@
<!--Transparency between 0 - 255 -->
<attr name="alphaValue" format="integer" />

<!-- Gradient Type-->
<attr name="gradientType">
<enum name="linear" value="0" />
<enum name="radial" value="1" />
</attr>

<!--Gradient Start Color-->
<attr name="gradient_startColor" format="color" />

<!--Gradient End Color-->
<attr name="gradient_endColor" format="color" />

<!--Gradient Angle-->
<attr name="gradientAngle" format="integer" />

<declare-styleable name="FrissonView">
<attr name="scaleType" />
<attr name="src" />
Expand All @@ -32,6 +47,10 @@
<attr name="autoTint" />
<attr name="tideHeight" />
<attr name="alphaValue" />
<attr name="gradientType" />
<attr name="gradientAngle"/>
<attr name="gradient_startColor" />
<attr name="gradient_endColor" />
</declare-styleable>

</resources>

0 comments on commit f53c9f3

Please sign in to comment.