From ce95bae80b2af2184a364f8fc806be10fcd52305 Mon Sep 17 00:00:00 2001 From: kalai Date: Tue, 23 Feb 2021 19:23:00 +0800 Subject: [PATCH] feat: Wave background customisable feat: Wave background customisable - Users can change wave color and the background, - Made default above and below wave color to be that of the actual wave color. - Users can make the wave color to be their gradient color by giving appropriate drawable to setWaveBackground. --- app/build.gradle | 6 +- .../java/com/waveview/demo/MainActivity.java | 14 +- .../main/res/drawable/gradient_background.xml | 12 ++ app/src/main/res/layout/main.xml | 13 +- app/src/main/res/values/color.xml | 5 + build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 2 +- library/build.gradle | 2 +- .../main/java/com/john/waveview/Solid.java | 59 ++++++-- .../com/john/waveview/SolidAttributes.java | 25 +++ .../src/main/java/com/john/waveview/Wave.java | 143 +++++++++++------- .../com/john/waveview/WaveAttributes.java | 46 ++++++ .../main/java/com/john/waveview/WaveView.java | 125 ++++++++++----- library/src/main/res/values/attr.xml | 9 ++ 14 files changed, 356 insertions(+), 109 deletions(-) create mode 100644 app/src/main/res/drawable/gradient_background.xml create mode 100644 library/src/main/java/com/john/waveview/SolidAttributes.java create mode 100644 library/src/main/java/com/john/waveview/WaveAttributes.java diff --git a/app/build.gradle b/app/build.gradle index 7a9ac8d..bb12b1b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,7 +5,7 @@ android { buildToolsVersion '20.0.0' defaultConfig { - minSdkVersion 8 + minSdkVersion 11 targetSdkVersion 20 versionCode 1 versionName "1.0" @@ -20,6 +20,6 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - compile project(':library') + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation project(':library') } diff --git a/app/src/main/java/com/waveview/demo/MainActivity.java b/app/src/main/java/com/waveview/demo/MainActivity.java index 594e1a2..d3c7ffd 100644 --- a/app/src/main/java/com/waveview/demo/MainActivity.java +++ b/app/src/main/java/com/waveview/demo/MainActivity.java @@ -1,6 +1,8 @@ package com.waveview.demo; +import android.annotation.TargetApi; import android.app.Activity; +import android.os.Build; import android.os.Bundle; import android.widget.SeekBar; @@ -14,17 +16,27 @@ public class MainActivity extends Activity { private SeekBar seekBar; private WaveView waveView; + @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); seekBar = (SeekBar) findViewById(R.id.seek_bar); waveView = (WaveView) findViewById(R.id.wave_view); +/* final ArgbEvaluator evaluator = new ArgbEvaluator(); + final int initColor = getResources().getColor(R.color.holo_red); + final int endColor = getResources().getColor(R.color.holo_green);*/ - seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + + + seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { waveView.setProgress(progress); + /* int color = (Integer) evaluator.evaluate(progress/100f,initColor,endColor); + waveView.setAboveWaveColor(color); + waveView.setBlowWaveColor(color); + waveView.setWaveColor(color);*/ } @Override diff --git a/app/src/main/res/drawable/gradient_background.xml b/app/src/main/res/drawable/gradient_background.xml new file mode 100644 index 0000000..ae5aa3e --- /dev/null +++ b/app/src/main/res/drawable/gradient_background.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/main.xml b/app/src/main/res/layout/main.xml index ed439bd..f830a2d 100644 --- a/app/src/main/res/layout/main.xml +++ b/app/src/main/res/layout/main.xml @@ -8,13 +8,16 @@ android:id="@+id/wave_view" android:layout_width="match_parent" android:layout_height="match_parent" - android:background="#ff702e8c" - wave:above_wave_color="@android:color/white" - wave:blow_wave_color="@android:color/white" + android:background="#FFFFFF" + wave:above_wave_color="@color/light_blue" + wave:blow_wave_color="@color/light_blue" + wave:above_wave_color_alpha="0.5" + wave:blow_wave_color_alpha="0.3" wave:progress="80" - wave:wave_height="little" + wave:wave_height="large" wave:wave_hz="normal" - wave:wave_length="middle" /> + wave:wave_length="middle" + wave:background="@drawable/gradient_background"/> #ffaa66cc + #7CB342 + #E53935 + + #DA00FFE1 + #007BFF \ No newline at end of file diff --git a/build.gradle b/build.gradle index 3a4c082..955d73b 100644 --- a/build.gradle +++ b/build.gradle @@ -2,9 +2,10 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:1.0.1' + classpath 'com.android.tools.build:gradle:4.1.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -13,5 +14,6 @@ buildscript { allprojects { repositories { jcenter() + google() } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 58b1e16..33d123c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip diff --git a/library/build.gradle b/library/build.gradle index c20db03..7508546 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -19,5 +19,5 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) + implementation fileTree(dir: 'libs', include: ['*.jar']) } diff --git a/library/src/main/java/com/john/waveview/Solid.java b/library/src/main/java/com/john/waveview/Solid.java index 35a2976..e8fbbdf 100644 --- a/library/src/main/java/com/john/waveview/Solid.java +++ b/library/src/main/java/com/john/waveview/Solid.java @@ -3,41 +3,78 @@ import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; +import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.View; import android.widget.LinearLayout; +import static com.john.waveview.WaveView.alphaPercentToInt; + /** * Created by John on 2014/10/15. */ class Solid extends View { - private Paint aboveWavePaint; - private Paint blowWavePaint; + private final Paint mWavePaint = new Paint(); + private int mWaveColor; + private float mWaveAlpha; + private Drawable mBackgroundDrawable; + + private final Paint.Style STYLE = Paint.Style.FILL; - public Solid(Context context, AttributeSet attrs) { - this(context, attrs, 0); + public Solid(Context context, SolidAttributes solidAttributes) { + this(context,null, 0, solidAttributes); } - public Solid(Context context, AttributeSet attrs, int defStyleAttr) { + /*For Android Studio Tools*/ + private Solid(Context context){ + super(context); } + + private Solid(Context context, AttributeSet attrs, int defStyleAttr, SolidAttributes solidAttributes) { super(context, attrs, defStyleAttr); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.weight = 1; setLayoutParams(params); + initialize(solidAttributes); + setWaveColor(); } - public void setAboveWavePaint(Paint aboveWavePaint) { - this.aboveWavePaint = aboveWavePaint; + private void initialize(SolidAttributes solidAttributes) { + mBackgroundDrawable = solidAttributes.getBackgroundDrawable(); + + mWaveColor = solidAttributes.getWaveColor(); + mWaveAlpha = solidAttributes.getWaveAlpha(); + setWaveAlpha(mWaveAlpha); + mWavePaint.setStyle(STYLE); + if(mBackgroundDrawable!=null) { + this.setBackgroundDrawable(mBackgroundDrawable); + } } - public void setBlowWavePaint(Paint blowWavePaint) { - this.blowWavePaint = blowWavePaint; + public void setWaveColor() { + mWavePaint.setColor(mWaveColor); + setWaveAlpha(mWaveAlpha); } + public void setWaveAlpha(float alpha){ mWavePaint.setAlpha(alphaPercentToInt(alpha)); } + + public float getWaveAlpha() { return mWavePaint.getAlpha(); } + + public void setWaveColor(int color) { mWavePaint.setColor(color); } + + public int getWaveColor() { return mWavePaint.getColor(); } + + @Override + public Drawable getBackground() { return mBackgroundDrawable; } + + public void setBackground(Drawable backgroundDrawable){ mBackgroundDrawable = backgroundDrawable; } + @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); - canvas.drawRect(getLeft(), 0, getRight(), getBottom(), blowWavePaint); - canvas.drawRect(getLeft(), 0, getRight(), getBottom(), aboveWavePaint); + if(mBackgroundDrawable==null) { + canvas.drawRect(getLeft(), 0, getRight(), getBottom(), mWavePaint); + canvas.drawRect(getLeft(), 0, getRight(), getBottom(), mWavePaint); + } } } diff --git a/library/src/main/java/com/john/waveview/SolidAttributes.java b/library/src/main/java/com/john/waveview/SolidAttributes.java new file mode 100644 index 0000000..0719804 --- /dev/null +++ b/library/src/main/java/com/john/waveview/SolidAttributes.java @@ -0,0 +1,25 @@ +package com.john.waveview; + + +import android.graphics.drawable.Drawable; + +/** + * Data class to store solid attributes*/ +public class SolidAttributes { + + private int mWaveColor; + private float mWaveAlpha; + private Drawable mBackgroundDrawable; + + SolidAttributes(int waveColor, float waveAlpha, Drawable drawable){ + mWaveColor = waveColor; + mWaveAlpha = waveAlpha; + mBackgroundDrawable = drawable; + } + + public int getWaveColor() { return mWaveColor; } + + public float getWaveAlpha() { return mWaveAlpha; } + + public Drawable getBackgroundDrawable() { return mBackgroundDrawable; } +} diff --git a/library/src/main/java/com/john/waveview/Wave.java b/library/src/main/java/com/john/waveview/Wave.java index 020211b..b97d047 100644 --- a/library/src/main/java/com/john/waveview/Wave.java +++ b/library/src/main/java/com/john/waveview/Wave.java @@ -8,6 +8,10 @@ import android.view.View; import android.view.ViewGroup; +import static com.john.waveview.WaveView.LARGE; +import static com.john.waveview.WaveView.MIDDLE; +import static com.john.waveview.WaveView.alphaPercentToInt; + // y=Asin(ωx+φ)+k class Wave extends View { private final int WAVE_HEIGHT_LARGE = 16; @@ -22,9 +26,6 @@ class Wave extends View { private final float WAVE_HZ_NORMAL = 0.09f; private final float WAVE_HZ_SLOW = 0.05f; - public final int DEFAULT_ABOVE_WAVE_ALPHA = 50; - public final int DEFAULT_BLOW_WAVE_ALPHA = 30; - private final float X_SPACE = 20; private final double PI2 = 2 * Math.PI; @@ -34,14 +35,11 @@ class Wave extends View { private Paint mAboveWavePaint = new Paint(); private Paint mBlowWavePaint = new Paint(); - private int mAboveWaveColor; - private int mBlowWaveColor; - - private float mWaveMultiple; private float mWaveLength; - private int mWaveHeight; private float mMaxRight; private float mWaveHz; + private int mWaveHeight; + private float mWaveMultiple; // wave animation private float mAboveOffset = 0.0f; @@ -53,65 +51,56 @@ class Wave extends View { // ω private double omega; - public Wave(Context context, AttributeSet attrs) { - this(context, attrs, R.attr.waveViewStyle); - } + private int mAboveWaveColor; + private int mBlowWaveColor; + private float mBlowWaveAlpha; + private float mAboveWaveAlpha; - public Wave(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); + + public Wave(Context context, WaveAttributes waveAttributes) { + this(context, null, R.attr.waveViewStyle, waveAttributes); } - @Override - protected void onDraw(Canvas canvas) { - super.onDraw(canvas); + /*For Android Studio Tools*/ + private Wave(Context context){ + super(context); } - canvas.drawPath(mBlowWavePath, mBlowWavePaint); - canvas.drawPath(mAboveWavePath, mAboveWavePaint); + private Wave(Context context, AttributeSet attrs, int defStyle, WaveAttributes waveAttributes) { + super(context, attrs, defStyle); + initializeWave(waveAttributes); } + public int getAboveWaveColor() { return mAboveWaveColor; } + public void setAboveWaveColor(int aboveWaveColor) { - this.mAboveWaveColor = aboveWaveColor; + mAboveWavePaint.setColor(aboveWaveColor); + setAboveWaveAlpha(mAboveWaveAlpha); } public void setBlowWaveColor(int blowWaveColor) { - this.mBlowWaveColor = blowWaveColor; + mBlowWavePaint.setColor(blowWaveColor); + setBlowWaveAlpha(mBlowWaveAlpha); } - public Paint getAboveWavePaint() { - return mAboveWavePaint; - } + public int getBlowWaveColor() { return mBlowWaveColor;} - public Paint getBlowWavePaint() { - return mBlowWavePaint; - } + public void setAboveWaveAlpha(float aboveWaveAlpha) { mAboveWavePaint.setAlpha(alphaPercentToInt(aboveWaveAlpha)); } - public void initializeWaveSize(int waveMultiple, int waveHeight, int waveHz) { - mWaveMultiple = getWaveMultiple(waveMultiple); - mWaveHeight = getWaveHeight(waveHeight); - mWaveHz = getWaveHz(waveHz); - mBlowOffset = mWaveHeight * 0.4f; - ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, - mWaveHeight * 2); - setLayoutParams(params); - } + public void setBlowWaveAlpha(float blowWaveAlpha) { mBlowWavePaint.setAlpha(alphaPercentToInt(blowWaveAlpha)); } - public void initializePainters() { - mAboveWavePaint.setColor(mAboveWaveColor); - mAboveWavePaint.setAlpha(DEFAULT_ABOVE_WAVE_ALPHA); - mAboveWavePaint.setStyle(Paint.Style.FILL); - mAboveWavePaint.setAntiAlias(true); + public void setWaveHz(float waveHz){ mWaveHz = waveHz; } - mBlowWavePaint.setColor(mBlowWaveColor); - mBlowWavePaint.setAlpha(DEFAULT_BLOW_WAVE_ALPHA); - mBlowWavePaint.setStyle(Paint.Style.FILL); - mBlowWavePaint.setAntiAlias(true); - } + public float getBlowWaveColorAlpha() { return mBlowWaveAlpha; } + + public float getAboveColorAlpha() { return mAboveWaveAlpha; } + + public float getWaveHz() { return mWaveHz; } private float getWaveMultiple(int size) { switch (size) { - case WaveView.LARGE: + case LARGE: return WAVE_LENGTH_MULTIPLE_LARGE; - case WaveView.MIDDLE: + case MIDDLE: return WAVE_LENGTH_MULTIPLE_MIDDLE; case WaveView.LITTLE: return WAVE_LENGTH_MULTIPLE_LITTLE; @@ -121,9 +110,9 @@ private float getWaveMultiple(int size) { private int getWaveHeight(int size) { switch (size) { - case WaveView.LARGE: + case LARGE: return WAVE_HEIGHT_LARGE; - case WaveView.MIDDLE: + case MIDDLE: return WAVE_HEIGHT_MIDDLE; case WaveView.LITTLE: return WAVE_HEIGHT_LITTLE; @@ -133,9 +122,9 @@ private int getWaveHeight(int size) { private float getWaveHz(int size) { switch (size) { - case WaveView.LARGE: + case LARGE: return WAVE_HZ_FAST; - case WaveView.MIDDLE: + case MIDDLE: return WAVE_HZ_NORMAL; case WaveView.LITTLE: return WAVE_HZ_SLOW; @@ -143,6 +132,51 @@ private float getWaveHz(int size) { return 0; } + private void initializeWave(WaveAttributes waveAttributes) { + mAboveWaveColor = waveAttributes.getAboveWaveColor(); + mAboveWaveAlpha = waveAttributes.getAboveWaveAlpha(); + mBlowWaveColor = waveAttributes.getBlowWaveColor(); + mBlowWaveAlpha = waveAttributes.getBlowWaveAlpha(); + int waveLength = waveAttributes.getWaveLength(); + int waveHeight = waveAttributes.getWaveHeight(); + int waveHz = waveAttributes.getWaveHz(); + initializeWaveSize(waveLength, waveHeight, waveHz); + initializeAboveWaveColorAlpha(); + initializeBlowWaveColorAlpha(); + initializePainters(); + } + + private void initializeWaveSize(int waveMultiple, int waveHeight, int waveHz) { + mWaveMultiple = getWaveMultiple(waveMultiple); + mWaveHeight = getWaveHeight(waveHeight); + mWaveHz = getWaveHz(waveHz); + mBlowOffset = mWaveHeight * 0.4f; + ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mWaveHeight * 2); + setLayoutParams(params); + } + + private void initializePainters() { + mAboveWavePaint.setColor(mAboveWaveColor); + mAboveWavePaint.setAlpha(alphaPercentToInt(mAboveWaveAlpha)); + mAboveWavePaint.setStyle(Paint.Style.FILL); + mAboveWavePaint.setAntiAlias(true); + + mBlowWavePaint.setColor(mBlowWaveColor); + mBlowWavePaint.setAlpha(alphaPercentToInt(mBlowWaveAlpha)); + mBlowWavePaint.setStyle(Paint.Style.FILL); + mBlowWavePaint.setAntiAlias(true); + } + + private void initializeAboveWaveColorAlpha() { + setAboveWaveAlpha(mAboveWaveAlpha); + setAboveWaveColor(mAboveWaveColor); + } + + private void initializeBlowWaveColorAlpha() { + setBlowWaveAlpha(mBlowWaveAlpha); + setBlowWaveColor(mBlowWaveColor); + } + /** * calculate wave track */ @@ -168,6 +202,13 @@ private void calculatePath() { mBlowWavePath.lineTo(right, bottom); } + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + canvas.drawPath(mBlowWavePath, mBlowWavePaint); + canvas.drawPath(mAboveWavePath, mAboveWavePaint); + } + @Override protected void onWindowVisibilityChanged(int visibility) { super.onWindowVisibilityChanged(visibility); diff --git a/library/src/main/java/com/john/waveview/WaveAttributes.java b/library/src/main/java/com/john/waveview/WaveAttributes.java new file mode 100644 index 0000000..547b03a --- /dev/null +++ b/library/src/main/java/com/john/waveview/WaveAttributes.java @@ -0,0 +1,46 @@ +package com.john.waveview; + +/** + * Data class to store wave attributes*/ +public class WaveAttributes { + + private int mAboveWaveColor; + private float mAboveWaveAlpha; + private int mBlowWaveColor; + private float mBlowWaveAlpha; + private int mWaveLength; + private int mWaveHeight; + private int mWaveHz; + + WaveAttributes( + int aboveWaveColor, + float aboveWaveAlpha, + int blowWaveColor, + float blowWaveAlpha, + int waveLength, + int waveHz, + int waveHeight){ + mAboveWaveAlpha = aboveWaveAlpha; + mAboveWaveColor = aboveWaveColor; + mBlowWaveAlpha = blowWaveAlpha; + mBlowWaveColor = blowWaveColor; + mWaveLength = waveLength; + mWaveHz = waveHz; + mWaveHeight = waveHeight; + } + + public int getAboveWaveColor() { return mAboveWaveColor; } + + public int getBlowWaveColor() { return mBlowWaveColor; } + + public float getBlowWaveAlpha() { return mBlowWaveAlpha; } + + public float getAboveWaveAlpha() { return mAboveWaveAlpha; } + + public int getWaveLength() { return mWaveLength; } + + public int getWaveHeight() { return mWaveHeight; } + + public int getWaveHz() { return mWaveHz; } + +} diff --git a/library/src/main/java/com/john/waveview/WaveView.java b/library/src/main/java/com/john/waveview/WaveView.java index 62088d1..785e057 100644 --- a/library/src/main/java/com/john/waveview/WaveView.java +++ b/library/src/main/java/com/john/waveview/WaveView.java @@ -3,6 +3,7 @@ import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; +import android.graphics.drawable.Drawable; import android.os.Parcel; import android.os.Parcelable; import android.util.AttributeSet; @@ -17,44 +18,30 @@ public class WaveView extends LinearLayout { protected static final int MIDDLE = 2; protected static final int LITTLE = 3; - private int mAboveWaveColor; - private int mBlowWaveColor; private int mProgress; - private int mWaveHeight; - private int mWaveMultiple; - private int mWaveHz; - - private int mWaveToTop; - private Wave mWave; private Solid mSolid; + private int mWaveToTop; - private final int DEFAULT_ABOVE_WAVE_COLOR = Color.WHITE; - private final int DEFAULT_BLOW_WAVE_COLOR = Color.WHITE; - private final int DEFAULT_PROGRESS = 80; + public final int DEFAULT_COLOR = Color.WHITE; + public final int DEFAULT_PROGRESS = 80; + public final float DEFAULT_ALPHA = 0.65f; public WaveView(Context context, AttributeSet attrs) { super(context, attrs); setOrientation(VERTICAL); - //load styled attributes. + // Load styled attributes. final TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.WaveView, R.attr.waveViewStyle, 0); - mAboveWaveColor = attributes.getColor(R.styleable.WaveView_above_wave_color, DEFAULT_ABOVE_WAVE_COLOR); - mBlowWaveColor = attributes.getColor(R.styleable.WaveView_blow_wave_color, DEFAULT_BLOW_WAVE_COLOR); + mProgress = attributes.getInt(R.styleable.WaveView_progress, DEFAULT_PROGRESS); - mWaveHeight = attributes.getInt(R.styleable.WaveView_wave_height, MIDDLE); - mWaveMultiple = attributes.getInt(R.styleable.WaveView_wave_length, LARGE); - mWaveHz = attributes.getInt(R.styleable.WaveView_wave_hz, MIDDLE); - attributes.recycle(); - mWave = new Wave(context, null); - mWave.initializeWaveSize(mWaveMultiple, mWaveHeight, mWaveHz); - mWave.setAboveWaveColor(mAboveWaveColor); - mWave.setBlowWaveColor(mBlowWaveColor); - mWave.initializePainters(); + SolidAttributes solidAttributes = getSolidAttributes(attributes); + WaveAttributes waveAttributes = getWaveAttributes(attributes,solidAttributes); + attributes.recycle(); - mSolid = new Solid(context, null); - mSolid.setAboveWavePaint(mWave.getAboveWavePaint()); - mSolid.setBlowWavePaint(mWave.getBlowWavePaint()); + // Not passing attrs and retrieving attrs there as cannot access attrs from this parent view 's attrs - see https://stackoverflow.com/a/50865837/11200630 + mWave = new Wave(context, waveAttributes); + mSolid = new Solid(context, solidAttributes); addView(mWave); addView(mSolid); @@ -62,19 +49,68 @@ public WaveView(Context context, AttributeSet attrs) { setProgress(mProgress); } - public void setProgress(int progress) { - this.mProgress = progress > 100 ? 100 : progress; - computeWaveToTop(); + private SolidAttributes getSolidAttributes(TypedArray attributes) { + int waveColor = attributes.getColor(R.styleable.WaveView_wave_color,DEFAULT_COLOR); + float waveAlpha = attributes.getFloat(R.styleable.WaveView_wave_alpha,DEFAULT_ALPHA); + Drawable drawable = attributes.getDrawable(R.styleable.WaveView_background); + return new SolidAttributes( + waveColor, + waveAlpha, + drawable); } - @Override - public void onWindowFocusChanged(boolean hasWindowFocus) { - super.onWindowFocusChanged(hasWindowFocus); - if (hasWindowFocus) { - computeWaveToTop(); - } + private WaveAttributes getWaveAttributes(TypedArray attributes, SolidAttributes solidAttributes) { + int aboveWaveColor = attributes.getColor(R.styleable.WaveView_above_wave_color, solidAttributes.getWaveColor()); + int blowWaveColor = attributes.getColor(R.styleable.WaveView_blow_wave_color, solidAttributes.getWaveColor()); + float aboveWaveAlpha = attributes.getFloat(R.styleable.WaveView_above_wave_color_alpha,DEFAULT_ALPHA); + float blowWaveAlpha = attributes.getFloat(R.styleable.WaveView_blow_wave_color_alpha, DEFAULT_ALPHA); + int waveLength = attributes.getInt(R.styleable.WaveView_wave_length, LARGE); + int waveHz = attributes.getInt(R.styleable.WaveView_wave_hz, MIDDLE); + int waveHeight = attributes.getInt(R.styleable.WaveView_wave_height, MIDDLE); + return new WaveAttributes( + aboveWaveColor, + aboveWaveAlpha, + blowWaveColor, + blowWaveAlpha, + waveLength, + waveHeight, + waveHz); } + public int getProgress() { return mProgress; } + + public void setWaveAlpha(float alpha) { mSolid.setWaveAlpha(alpha); } + + public float getWaveAlpha(){ return mSolid.getWaveAlpha();} + + public void setAboveWaveColor(int aboveWaveColor) { mWave.setAboveWaveColor(aboveWaveColor);} + + private int getAboveWaveColor(){ return mWave.getAboveWaveColor();} + + public void setAboveWaveColorAlpha(float aboveWaveColorAlpha) { mWave.setAboveWaveAlpha(aboveWaveColorAlpha);} + + public float getAboveWaveColorAlpha() { return mWave.getAboveColorAlpha(); } + + public void setWaveColor(int waveColor) { mSolid.setWaveColor(waveColor); } + + public int getWaveColor(){ return mSolid.getWaveColor();} + + public void setBlowWaveColor(int blowWaveColor) { mWave.setBlowWaveColor(blowWaveColor); } + + public int getBlowWaveColor(){ return mWave.getBlowWaveColor();} + + public void setBlowWaveColorAlpha(float alpha) { mSolid.setWaveAlpha(alpha); } + + public float getBlowWaveColorAlpha() { return mWave.getBlowWaveColorAlpha(); } + + public void setWaveHz(int waveHz) { mWave.setBlowWaveColor(waveHz); } + + public float getWaveHz() { return mWave.getWaveHz(); } + + public Drawable getWaveBackground(){ return mSolid.getBackground();} + + public void setWaveBackgroundDrawable(Drawable drawable){ mSolid.setBackgroundDrawable(drawable);} + private void computeWaveToTop() { mWaveToTop = (int) (getHeight() * (1f - mProgress / 100f)); ViewGroup.LayoutParams params = mWave.getLayoutParams(); @@ -84,6 +120,14 @@ private void computeWaveToTop() { mWave.setLayoutParams(params); } + @Override + public void onWindowFocusChanged(boolean hasWindowFocus) { + super.onWindowFocusChanged(hasWindowFocus); + if (hasWindowFocus) { + computeWaveToTop(); + } + } + @Override public Parcelable onSaveInstanceState() { // Force our ancestor class to save its state @@ -100,6 +144,17 @@ public void onRestoreInstanceState(Parcelable state) { setProgress(ss.progress); } + public void setProgress(int progress) { + this.mProgress = progress > 100 ? 100 : progress; + computeWaveToTop(); + } + + public static int alphaPercentToInt(float alphaPercent){ + if(alphaPercent>1) return 255; + else if(alphaPercent<0) return 0; + return (int) (alphaPercent*255); + } + private static class SavedState extends BaseSavedState { int progress; diff --git a/library/src/main/res/values/attr.xml b/library/src/main/res/values/attr.xml index 4994d9b..8f09659 100644 --- a/library/src/main/res/values/attr.xml +++ b/library/src/main/res/values/attr.xml @@ -3,7 +3,11 @@ + + + + @@ -20,6 +24,11 @@ + + + + +