Skip to content

Commit

Permalink
Android: Adjusted bitmap cache sizes depending on screen size, layout…
Browse files Browse the repository at this point in the history
… bug fixes in MobileLibraryBrowser, and now using SquareImageView when displaying album art throughout the application.

Related to issue #406.
  • Loading branch information
ycastonguay committed Jul 24, 2013
1 parent 4fd8d4a commit e0b2c6b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 36 deletions.
15 changes: 10 additions & 5 deletions MPfm/MPfm.Android/Classes/Activities/MainActivity.cs
Expand Up @@ -22,6 +22,7 @@
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.Support.V4.App;
using Android.Support.V4.View;
using Android.Views;
Expand All @@ -39,6 +40,7 @@
using MPfm.MVP.Views;
using MPfm.Sound.AudioFiles;
using TinyMessenger;
using org.sessionsapp.android;
using DialogFragment = Android.App.DialogFragment;
using Fragment = Android.App.Fragment;

Expand All @@ -57,7 +59,7 @@ public class MainActivity : BaseActivity, IMobileOptionsMenuView, View.IOnTouchL
private TextView _lblArtistName;
private TextView _lblAlbumTitle;
private TextView _lblSongTitle;
private ImageView _imageAlbum;
private SquareImageView _imageAlbum;
private ImageButton _btnPrevious;
private ImageButton _btnPlayPause;
private ImageButton _btnNext;
Expand Down Expand Up @@ -88,7 +90,7 @@ protected override void OnCreate(Bundle bundle)
_btnPrevious = FindViewById<ImageButton>(Resource.Id.main_miniplayer_btnPrevious);
_btnPlayPause = FindViewById<ImageButton>(Resource.Id.main_miniplayer_btnPlayPause);
_btnNext = FindViewById<ImageButton>(Resource.Id.main_miniplayer_btnNext);
_imageAlbum = FindViewById<ImageView>(Resource.Id.main_miniplayer_imageAlbum);
_imageAlbum = FindViewById<SquareImageView>(Resource.Id.main_miniplayer_imageAlbum);
_miniPlayer.Visibility = ViewStates.Gone;
_miniPlayer.Click += (sender, args) => {
Console.WriteLine("MainActivity - Mini player click - Showing player view...");
Expand All @@ -101,11 +103,14 @@ protected override void OnCreate(Bundle bundle)
_btnPlayPause.Click += BtnPlayPauseOnClick;
_btnNext.Click += BtnNextOnClick;

// Get screen size
Point size = new Point();
WindowManager.DefaultDisplay.GetSize(size);

// Create bitmap cache
int maxMemory = (int)(Runtime.GetRuntime().MaxMemory() / 1024);
int cacheSize = maxMemory / 8;
//int cacheSize = 4*1024*1024;
BitmapCache = new BitmapCache(this, cacheSize, 800, 800);
int cacheSize = maxMemory / 16;
BitmapCache = new BitmapCache(this, cacheSize, size.X / 6, size.X / 6);

// Listen to player changes to show/hide the mini player
_messengerHub = Bootstrapper.GetContainer().Resolve<ITinyMessengerHub>();
Expand Down
14 changes: 10 additions & 4 deletions MPfm/MPfm.Android/Classes/Activities/PlayerActivity.cs
Expand Up @@ -21,6 +21,7 @@
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.Support.V4.View;
using Android.Views;
using Android.OS;
Expand All @@ -38,6 +39,7 @@
using MPfm.Player.Objects;
using MPfm.Sound.AudioFiles;
using TinyMessenger;
using org.sessionsapp.android;
using Exception = System.Exception;

namespace MPfm.Android
Expand All @@ -47,7 +49,7 @@ public class PlayerActivity : BaseActivity, IPlayerView, View.IOnTouchListener
{
private ITinyMessengerHub _messengerHub;
private BitmapCache _bitmapCache;
private ImageView _imageViewAlbumArt;
private SquareImageView _imageViewAlbumArt;
private TextView _lblPosition;
private TextView _lblLength;
private ImageButton _btnPlayPause;
Expand Down Expand Up @@ -82,7 +84,7 @@ protected override void OnCreate(Bundle bundle)
_viewPager.Adapter = _tabPagerAdapter;
_viewPager.SetOnPageChangeListener(_tabPagerAdapter);

_imageViewAlbumArt = FindViewById<ImageView>(Resource.Id.player_imageViewAlbumArt);
_imageViewAlbumArt = FindViewById<SquareImageView>(Resource.Id.player_imageViewAlbumArt);
_lblPosition = FindViewById<TextView>(Resource.Id.player_lblPosition);
_lblLength = FindViewById<TextView>(Resource.Id.player_lblLength);
_btnPlayPause = FindViewById<ImageButton>(Resource.Id.player_btnPlayPause);
Expand All @@ -108,10 +110,14 @@ protected override void OnCreate(Bundle bundle)
_seekBar.StopTrackingTouch += SeekBarOnStopTrackingTouch;
_seekBar.ProgressChanged += SeekBarOnProgressChanged;

// Get screen size
Point size = new Point();
WindowManager.DefaultDisplay.GetSize(size);

// Create bitmap cache
int maxMemory = (int)(Runtime.GetRuntime().MaxMemory() / 1024);
int cacheSize = maxMemory / 8;
_bitmapCache = new BitmapCache(this, cacheSize, 800, 800);
int cacheSize = maxMemory / 12;
_bitmapCache = new BitmapCache(this, cacheSize, size.X, size.X); // The album art takes the whole screen width

// Match height with width (cannot do that in xml)
//_imageViewAlbumArt.LayoutParameters = new ViewGroup.LayoutParams(_imageViewAlbumArt.Width, _imageViewAlbumArt.Width);
Expand Down
Expand Up @@ -122,18 +122,18 @@ public override View GetView(int position, View convertView, ViewGroup parent)
if(_fragment.BitmapCache.KeyExists(bitmapKey))
{
//Console.WriteLine(">>>>>>>>> MobileLibraryBrowserGridAdapter - Getting album art from cache - position: {0} artistName: {1} albumTitle: {2}", position, _items[position].Query.ArtistName, _items[position].Query.AlbumTitle);
Task.Factory.StartNew(() => {
//Task.Factory.StartNew(() => {
imageView.Tag = bitmapKey;
//imageView.SetImageBitmap(mainActivity.BitmapCache.GetBitmapFromMemoryCache(bitmapKey));
imageView.SetImageBitmap(_fragment.BitmapCache.GetBitmapFromMemoryCache(bitmapKey));
});
//});
}
else
{
Task.Factory.StartNew(() => {
//Task.Factory.StartNew(() => {
//Console.WriteLine(">>>>>>>>> MobileLibraryBrowserGridAdapter - Requesting album art from presenter - position: {0} artistName: {1} albumTitle: {2}", position, _items[position].Query.ArtistName, _items[position].Query.AlbumTitle);
_fragment.OnRequestAlbumArt(item.Query.ArtistName, item.Query.AlbumTitle);
});
//});
}

return view;
Expand All @@ -154,13 +154,13 @@ public void RefreshAlbumArtCell(string artistName, string albumTitle, byte[] alb
//Console.WriteLine(">>>>>>>>>MobileLibraryBrowserGridAdapter - *RECEIVED* album art for {0}/{1} - index: {2} visibleCellIndex: {3} firstVisiblePosition: {4}", artistName, albumTitle, index, visibleCellIndex, _gridView.FirstVisiblePosition);
if (view != null)
{
Task.Factory.StartNew(() => {
//Task.Factory.StartNew(() => {
//Console.WriteLine(">>>>>>>>>MobileLibraryBrowserGridAdapter - *LOADING BITMAP* from byte array for {0}/{1} - Index found: {2}", artistName, albumTitle, index);
var image = view.FindViewById<ImageView>(Resource.Id.albumCell_image);
image.Tag = artistName + "_" + albumTitle;
//mainActivity.BitmapCache.LoadBitmapFromByteArray(albumArtData, artistName + "_" + albumTitle, image);
_fragment.BitmapCache.LoadBitmapFromByteArray(albumArtData, artistName + "_" + albumTitle, image);
});
//});
}
else
{
Expand Down
Expand Up @@ -20,6 +20,7 @@
using System.Linq;
using System.Threading.Tasks;
using Android.App;
using Android.Graphics;
using Android.OS;
using Android.Views;
using Android.Views.Animations;
Expand All @@ -32,6 +33,7 @@
using MPfm.MVP.Models;
using MPfm.MVP.Views;
using MPfm.Sound.AudioFiles;
using org.sessionsapp.android;
using Exception = System.Exception;

namespace MPfm.Android.Classes.Fragments
Expand All @@ -45,7 +47,7 @@ public class MobileLibraryBrowserFragment : BaseFragment, IMobileLibraryBrowserV
private MobileLibraryBrowserGridAdapter _gridAdapter;
private List<LibraryBrowserEntity> _entities = new List<LibraryBrowserEntity>();

ImageView _imageAlbum;
SquareImageView _imageAlbum;
LinearLayout _layoutAlbum;
TextView _lblArtistName;
TextView _lblAlbumTitle;
Expand All @@ -70,13 +72,16 @@ public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
Console.WriteLine("MLBFragment - OnCreateView");
_view = inflater.Inflate(Resource.Layout.MobileLibraryBrowser, container, false);

// Get screen size
Point size = new Point();
Activity.WindowManager.DefaultDisplay.GetSize(size);

// Create bitmap cache
int maxMemory = (int)(Runtime.GetRuntime().MaxMemory() / 1024);
//int cacheSize = 4 * 1024 * 1024;
int cacheSize = maxMemory / 8;
BitmapCache = new BitmapCache(Activity, cacheSize, 800, 800);
BitmapCache = new BitmapCache(Activity, cacheSize, size.X / 2, size.X / 2); // Max size = half the screen (grid has 2 columns)

_imageAlbum = _view.FindViewById<ImageView>(Resource.Id.mobileLibraryBrowser_imageAlbum);
_imageAlbum = _view.FindViewById<SquareImageView>(Resource.Id.mobileLibraryBrowser_imageAlbum);
_layoutAlbum = _view.FindViewById<LinearLayout>(Resource.Id.mobileLibraryBrowser_layoutAlbum);
_lblArtistName = _view.FindViewById<TextView>(Resource.Id.mobileLibraryBrowser_lblArtistName);
_lblAlbumTitle = _view.FindViewById<TextView>(Resource.Id.mobileLibraryBrowser_lblAlbumTitle);
Expand Down
3 changes: 2 additions & 1 deletion MPfm/MPfm.Android/Resources/Layout/Main.axml
Expand Up @@ -13,10 +13,11 @@
android:background="#36454F"
android:gravity="center_vertical"
android:layout_alignParentBottom="true">
<ImageView
<org.sessionsapp.android.SquareImageView
android:id="@+id/main_miniplayer_imageAlbum"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:background="@android:color/transparent" />
<LinearLayout
android:layout_width="0dp"
Expand Down
32 changes: 18 additions & 14 deletions MPfm/MPfm.Android/Resources/Layout/MobileLibraryBrowser.axml
Expand Up @@ -7,38 +7,40 @@
<LinearLayout
android:id="@+id/mobileLibraryBrowser_layoutAlbum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_height="100dp"
android:background="@color/background"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<ImageView
<org.sessionsapp.android.SquareImageView
android:id="@+id/mobileLibraryBrowser_imageAlbum"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:background="@android:color/transparent" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:layout_margin="8dp"
android:orientation="vertical">
<TextView
android:id="@+id/mobileLibraryBrowser_lblArtistName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Artist Name"
android:text=""
android:textColor="#FFFFFF"
android:textSize="18dp" />
android:textSize="16dp" />
<TextView
android:id="@+id/mobileLibraryBrowser_lblAlbumTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Album Title"
android:text=""
android:textColor="#CCCCCC"
android:textSize="16dp" />
android:textSize="15dp" />
<TextView
android:id="@+id/mobileLibraryBrowser_lblAlbumSongCount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="10 songs"
android:text=""
android:textColor="#AAAAAA"
android:textSize="14dp" />
<TextView
Expand All @@ -52,18 +54,20 @@
</LinearLayout>
<ListView
android:id="@+id/mobileLibraryBrowser_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/mobileLibraryBrowser_layoutAlbum"
android:layout_centerInParent="true"
android:background="#FFFFFF"
android:cacheColorHint="#333333"
android:divider="#BBBBBB"
android:dividerHeight="1px"
android:listSelector="#CCCCCC" />
<GridView
android:id="@+id/mobileLibraryBrowser_gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:background="@color/background"
android:columnWidth="100dp"
android:numColumns="2"
Expand Down
4 changes: 2 additions & 2 deletions MPfm/MPfm.Android/Resources/Layout/Player.axml
Expand Up @@ -47,12 +47,12 @@
android:layout_height="0dp"
android:layout_weight="1"
android:background="#000000">
<ImageView
<org.sessionsapp.android.SquareImageView
android:id="@+id/player_imageViewAlbumArt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:scaleType="fitStart"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:background="#000000" />
<android.support.v4.view.ViewPager
Expand Down

0 comments on commit e0b2c6b

Please sign in to comment.