Skip to content

Commit

Permalink
Android: Optimized OutputMeterView and WaveFormView. PlayerActivity n…
Browse files Browse the repository at this point in the history
…ow regenerates bitmap for wave form on configuration change.

Related to issue #406.
  • Loading branch information
ycastonguay committed Sep 24, 2013
1 parent 8a17e00 commit 0daacb6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 40 deletions.
17 changes: 14 additions & 3 deletions MPfm/MPfm.Android/Classes/Activities/PlayerActivity.cs
Expand Up @@ -21,8 +21,10 @@
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Content.Res;
using Android.Graphics;
using Android.Support.V4.View;
using Android.Util;
using Android.Views;
using Android.OS;
using Android.Widget;
Expand All @@ -41,6 +43,7 @@
using TinyMessenger;
using org.sessionsapp.android;
using Exception = System.Exception;
using Orientation = Android.Content.Res.Orientation;

namespace MPfm.Android
{
Expand Down Expand Up @@ -166,6 +169,14 @@ protected override void OnStart()
base.OnStart();
}

public override void OnConfigurationChanged(Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);

// The window manager returns the width depending on orientation
_waveFormView.RefreshWaveFormBitmap(WindowManager.DefaultDisplay.Width);
}

public void AddSubview(IBaseView view)
{
Console.WriteLine("PlayerActivity - AddSubview view: {0}", view.GetType().FullName);
Expand Down Expand Up @@ -277,7 +288,7 @@ private void SeekBarOnProgressChanged(object sender, SeekBar.ProgressChangedEven
//Console.WriteLine("PlayerActivity - SeekBarOnProgressChanged");
if (_isPositionChanging)
{
PlayerPositionEntity entity = OnPlayerRequestPosition((float) _seekBar.Progress/100f);
PlayerPositionEntity entity = OnPlayerRequestPosition((float) _seekBar.Progress/10000f);
_lblPosition.Text = entity.Position;
_waveFormView.SecondaryPosition = entity.PositionBytes;
}
Expand All @@ -293,7 +304,7 @@ private void SeekBarOnStartTrackingTouch(object sender, SeekBar.StartTrackingTou
private void SeekBarOnStopTrackingTouch(object sender, SeekBar.StopTrackingTouchEventArgs stopTrackingTouchEventArgs)
{
//Console.WriteLine("PlayerActivity - SeekBarOnStopTrackingTouch progress: {0}", _seekBar.Progress);
OnPlayerSetPosition(_seekBar.Progress);
OnPlayerSetPosition(_seekBar.Progress / 100f);
_isPositionChanging = false;
_waveFormView.ShowSecondaryPosition = false;
}
Expand Down Expand Up @@ -409,7 +420,7 @@ public void RefreshPlayerPosition(PlayerPositionEntity entity)
if (!_isPositionChanging)
{
_lblPosition.Text = entity.Position;
_seekBar.Progress = (int)entity.PositionPercentage;
_seekBar.Progress = (int) (entity.PositionPercentage * 100);
}
_waveFormView.Position = entity.PositionBytes;
Expand Down
14 changes: 8 additions & 6 deletions MPfm/MPfm.Android/Classes/Controls/OutputMeterView.cs
Expand Up @@ -31,7 +31,7 @@

namespace org.sessionsapp.android
{
public class OutputMeterView : View
public class OutputMeterView : SurfaceView
{
private List<WaveDataMinMax> _waveDataHistory = null;
/// <summary>
Expand Down Expand Up @@ -215,7 +215,8 @@ public override void Draw(global::Android.Graphics.Canvas canvas)
// Draw 0db line
var paintLine = new Paint {
AntiAlias = true,
Color = _color0dBLine
Color = _color0dBLine,
StrokeWidth = 1 * density
};
paintLine.SetStyle(Paint.Style.Fill);
canvas.DrawLine(0, 4, Width, 4, paintLine);
Expand Down Expand Up @@ -247,19 +248,20 @@ public override void Draw(global::Android.Graphics.Canvas canvas)
//}
//CoreGraphicsHelper.FillRect(context, rect, _colorMeter1);

//CoreGraphicsHelper.FillGradient(context, rect, _colorMeter1, _colorMeter2);
var paintMeter = new Paint {
AntiAlias = true,
Color = _colorMeter1
};
paintMeter.SetStyle(Paint.Style.Fill);
paintMeter.SetStyle(Paint.Style.Fill);
paintMeter.SetShader(new LinearGradient(0, 0, 0, Height, _colorMeter2, _colorMeter1, Shader.TileMode.Mirror));
canvas.DrawRect(rect, paintMeter);

// Draw peak line
var paintPeakLine = new Paint
{
AntiAlias = true,
Color = _colorPeakLine
Color = _colorPeakLine,
StrokeWidth = 1 * density
};
paintPeakLine.SetStyle(Paint.Style.Fill);
float leftPeakHeight = Height - (scaleMultiplier * (peakLeftDB + 100));
Expand Down Expand Up @@ -301,7 +303,7 @@ public override void Draw(global::Android.Graphics.Canvas canvas)
// Check for distortion
//if (maxLeftDB >= 0.2f)
// gradient = theme.MeterDistortionGradient;
//CoreGraphicsHelper.FillGradient(context, rect, _colorMeter1, _colorMeter2);
paintMeter.SetShader(new LinearGradient(0, 0, 0, Height, _colorMeterB2, _colorMeterB1, Shader.TileMode.Mirror));
canvas.DrawRect(rect, paintMeter);

// Draw peak line
Expand Down
79 changes: 49 additions & 30 deletions MPfm/MPfm.Android/Classes/Controls/WaveFormView.cs
Expand Up @@ -30,14 +30,15 @@
using Android.Widget;
using MPfm.Android.Classes.Managers;
using MPfm.Android.Classes.Managers.Events;
using MPfm.Core;
using MPfm.MVP.Bootstrap;
using MPfm.Player.Objects;
using MPfm.Sound.AudioFiles;
using MPfm.Sound.PeakFiles;

namespace org.sessionsapp.android
{
public class WaveFormView : View
public class WaveFormView : SurfaceView
{
private bool _isLoading;
private bool _isGeneratingImageCache;
Expand Down Expand Up @@ -348,30 +349,52 @@ private void DrawWaveFormBitmap(Canvas canvas)
float positionPercentage = (float)Position / (float)Length;
_cursorX = (int) (positionPercentage * Width);

//// Draw markers
//for (int a = 0; a < _markers.Count; a++)
//{
// float xPct = (float)_markers[a].PositionBytes / (float)Length;
// float x = xPct * Bounds.Width;

// // Draw cursor line
// context.SetStrokeColor(new CGColor(1, 0, 0, 1));
// context.SetLineWidth(1.0f);
// context.StrokeLineSegments(new PointF[2] { new PointF(x, 0), new PointF(x, heightAvailable) });

// // Draw text
// var rectText = new RectangleF(x, 0, 12, 12);
// CoreGraphicsHelper.FillRect(context, rectText, new CGColor(1, 0, 0, 0.7f));
// string letter = Conversion.IndexToLetter(a).ToString();
// UIGraphics.PushContext(context);
// CoreGraphicsHelper.DrawTextAtPoint(context, new PointF(x + 2, 0), letter, "HelveticaNeue", 10, new CGColor(1, 1, 1, 1));
// UIGraphics.PopContext();
//}
// Draw markers
for (int a = 0; a < _markers.Count; a++)
{
float xPct = (float)_markers[a].PositionBytes / (float)Length;
int x = (int) (xPct * Width);

// Draw cursor line
var paintMarkerCursor = new Paint
{
AntiAlias = true,
Color = new Color(255, 0, 0),
StrokeWidth = 1 * density
};
paintMarkerCursor.SetStyle(Paint.Style.Fill);
canvas.DrawLine(x, 0, x, heightAvailable, paintMarkerCursor);

// Draw text
//var rectText = new RectangleF(x, 0, 12, 12);
//CoreGraphicsHelper.FillRect(context, rectText, new CGColor(1, 0, 0, 0.7f));
string letter = Conversion.IndexToLetter(a).ToString();
//UIGraphics.PushContext(context);
//CoreGraphicsHelper.DrawTextAtPoint(context, new PointF(x + 2, 0), letter, "HelveticaNeue", 10, new CGColor(1, 1, 1, 1));
//UIGraphics.PopContext();

int flagWidth = (int) (12*density);
Rect rectTextBackground = new Rect(x - flagWidth, 0, x, flagWidth);
var paintTextBackground = new Paint
{
AntiAlias = true,
Color = new Color(255, 0, 0, 180)
};
paintTextBackground.SetStyle(Paint.Style.Fill);
canvas.DrawRect(rectTextBackground, paintTextBackground);

//paintText.GetTextBounds(letter, 0, letter.Length, rectText);
//int newX = (12 - rectText.Width()) / 2;
var paintText = new Paint
{
AntiAlias = true,
Color = Color.White,
TextSize = 10 * density
};
canvas.DrawText(letter, x - ((flagWidth * 3) / 4), (flagWidth * 3) / 4, paintText); // newX, Height - rectText.Height() - 4 - 1, paintText);
}

// Draw cursor line
//context.SetStrokeColor(new CGColor(0, 0.5f, 1, 1));
//context.SetLineWidth(1.0f);
//context.StrokeLineSegments(new PointF[2] { new PointF(_cursorX, 0), new PointF(_cursorX, heightAvailable) });
var paintCursor = new Paint
{
AntiAlias = true,
Expand All @@ -390,17 +413,13 @@ private void DrawWaveFormBitmap(Canvas canvas)

paintCursor.Color = Color.White;
canvas.DrawLine(_secondaryCursorX, 0, _secondaryCursorX, heightAvailable, paintCursor);

// // Draw cursor line
// context.SetStrokeColor(new CGColor(1, 1, 1, 1));
// context.SetLineWidth(1.0f);
// context.StrokeLineSegments(new PointF[2] { new PointF(_secondaryCursorX, 0), new PointF(_secondaryCursorX, heightAvailable) });
}
}

public void RefreshWaveFormBitmap()
public void RefreshWaveFormBitmap(int width)
{
GenerateWaveFormBitmap(_audioFile, new Rect(0, 0, Width, Height));
RefreshStatus("Generating new bitmap...");
GenerateWaveFormBitmap(_audioFile, new Rect(0, 0, width, Height));
}

private void GenerateWaveFormBitmap(AudioFile audioFile, Rect frame)
Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.Android/Resources/Layout/Player.axml
Expand Up @@ -23,7 +23,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="100" />
android:max="10000" />
<TextView
android:id="@+id/player_lblLength"
android:text="0.00:000"
Expand Down

0 comments on commit 0daacb6

Please sign in to comment.