From fe5ff05badcb39a94ea76cdd8cd365f8d8920cdf Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Wed, 10 Mar 2010 22:42:44 -0600 Subject: [PATCH] Create a custom layout for the song selector items Makes the code nicer, speeds things a little, and allows text to be centered better. Also add a high DPI expander icon --- res/drawable-hdpi/expander_arrow.9.png | Bin 0 -> 2505 bytes src/org/kreed/vanilla/AbstractAdapter.java | 74 +--------- src/org/kreed/vanilla/AlbumAdapter.java | 10 +- src/org/kreed/vanilla/ArtistAdapter.java | 8 +- src/org/kreed/vanilla/MediaView.java | 153 +++++++++++++++++++++ src/org/kreed/vanilla/SongAdapter.java | 9 +- 6 files changed, 173 insertions(+), 81 deletions(-) create mode 100644 res/drawable-hdpi/expander_arrow.9.png create mode 100644 src/org/kreed/vanilla/MediaView.java diff --git a/res/drawable-hdpi/expander_arrow.9.png b/res/drawable-hdpi/expander_arrow.9.png new file mode 100644 index 0000000000000000000000000000000000000000..7bddbcef5dbd9e7cc59e4d70b196776bf68b08d3 GIT binary patch literal 2505 zcmV;)2{!hLP)e5nd@)PX&T4x9<^1fv{f^jt&L#}yVv8va ze!uo3li zKUYWb&!doHvPE;A`4-w`&(`M$LJ$t^KGiT215hF&7jwcZR&0t7MO7|i8e)Z~A{e=q` z{cS5gZ zk|jbUh6;<09zD8z`t<2@%y`o(`zZ0rl$DkJTV(dB!;?b20GS>IFZU*p$P)gvckkY1 z8eqN|Z@RY*k}0>o(8a~Yqo+)nGWf`mBdnEggkCp*-iQz}w$R^h-MaPfOO`AN)&zrf zbaXg$6r#%2S7aOdvhvc>(lL^h)2B~=r#zctL3!98yxo&PG*9@A&WGQSg^p|x^*kE9(DabNLwCS(f_^1{)I3?=+Wst6%q}m)YKh2b}aj~gn|4B z#D7y(US96jtXbo-va+1c2`7)b$&)9$f`Wo>ZT)^Mt+9eSrZY1$vnEWKkb2_82_o`H zXs0YpdWr}nbA<6TXU?43-rnxO+qZ86O&YJoW7n=-E-x?76&4n{l9Cc9W88`rD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h=l(2@b&iX+wZ8&ErFeqXw(U;)zIBa`GNEn5h_7~P5|?h?-sqGJFBa!TXl;~(~k_f zW{|I$>tWyI1B?f{Jv8jzz58ns;TL(<#Ww;ZI0)J9m{N=zDc$ErWVIj?ya)1yPlZxp zv@k{}(RMpVKTwPwH*VZ;f9FwIf>=b*RaI3l^m9?|{{8zgoZSiISr64+nmZQTZRi45 zeoT@jLIYd$!;~%-N`y}Yo}oVz3iKnxu!Rd37U}2C{86Ju<>)1tt`}mC%ABtBt$IYg zee&c5eG1kanJnP{Hfm57%y+%TH0_jUc{5Cwu_#uGT(g*=kXrcv4 zrXfUFgSHMa%7}2VK?WiiPE?ElyA$<#ToWqqCjB8o2ebni3n7e$)Q~RCBZU0mFqKBw zEVzat=x3KjrRv1NDp{mzykvw81TT4=7E8I{6}+r$iV*v)qHyi TZ7cx500000NkvXXu0mjf5YF7U literal 0 HcmV?d00001 diff --git a/src/org/kreed/vanilla/AbstractAdapter.java b/src/org/kreed/vanilla/AbstractAdapter.java index 81eee7bb..a1a261da 100644 --- a/src/org/kreed/vanilla/AbstractAdapter.java +++ b/src/org/kreed/vanilla/AbstractAdapter.java @@ -26,22 +26,14 @@ import java.util.regex.Pattern; import android.content.Context; import android.content.Intent; -import android.graphics.Color; -import android.util.DisplayMetrics; -import android.util.TypedValue; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.BaseAdapter; import android.widget.Filter; import android.widget.Filterable; -import android.widget.ImageView; -import android.widget.RelativeLayout; -import android.widget.TextView; public abstract class AbstractAdapter extends BaseAdapter implements Filterable { - public static final int ONE_LINE = 0x1; - private Context mContext; private OnClickListener mExpanderListener; @@ -52,9 +44,6 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable private int mLimiterId = -1; private CharSequence mLastFilter; - private float mSize; - private int mPadding; - private int mDrawFlags; private int mMediaField; @@ -64,10 +53,6 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable mAllObjects = allObjects; mDrawFlags = drawFlags; mMediaField = mediaField; - - DisplayMetrics metrics = context.getResources().getDisplayMetrics(); - mSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, metrics); - mPadding = (int)mSize / 2; } public void setExpanderListener(View.OnClickListener listener) @@ -81,69 +66,24 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable return true; } - protected abstract void updateText(int position, TextView upper, TextView lower); + protected abstract void updateView(int position, MediaView view); public View getView(int position, View convertView, ViewGroup parent) { - RelativeLayout view = null; + MediaView view = null; try { - view = (RelativeLayout)convertView; + view = (MediaView)convertView; } catch (ClassCastException e) { } if (view == null) { - view = new RelativeLayout(mContext); + view = new MediaView(mContext, mDrawFlags); - RelativeLayout.LayoutParams params; - - if (mExpanderListener != null) { - params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); - params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); - params.addRule(RelativeLayout.CENTER_VERTICAL); - - ImageView button = new ImageView(mContext); - button.setPadding(mPadding * 2, mPadding, mPadding, mPadding); - button.setImageResource(R.drawable.expander_arrow); - button.setId(3); - button.setLayoutParams(params); - button.setTag(R.id.field, mMediaField); - button.setOnClickListener(mExpanderListener); - - view.addView(button); - } - - params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); - params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); - params.addRule(RelativeLayout.LEFT_OF, 3); - - TextView title = new TextView(mContext); - title.setPadding(mPadding, mPadding, 0, (mDrawFlags & ONE_LINE) == 0 ? 0 : mPadding); - title.setSingleLine(); - title.setTextColor(Color.WHITE); - title.setTextSize(mSize); - title.setId(1); - title.setLayoutParams(params); - view.addView(title); - - if ((mDrawFlags & ONE_LINE) == 0) { - params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); - params.addRule(RelativeLayout.BELOW, 1); - params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); - params.addRule(RelativeLayout.LEFT_OF, 3); - - TextView artist = new TextView(mContext); - artist.setPadding(mPadding, 0, 0, mPadding); - artist.setSingleLine(); - artist.setTextSize(mSize); - artist.setId(2); - artist.setLayoutParams(params); - view.addView(artist); - } + if (mExpanderListener != null) + view.setupExpander(mMediaField, mExpanderListener); } - updateText(position, (TextView)view.findViewById(1),(TextView)view.findViewById(2)); - if (mExpanderListener != null) - view.findViewById(3).setTag(R.id.id, (int)getItemId(position)); + updateView(position, view); return view; } diff --git a/src/org/kreed/vanilla/AlbumAdapter.java b/src/org/kreed/vanilla/AlbumAdapter.java index 257ca259..0f333eee 100644 --- a/src/org/kreed/vanilla/AlbumAdapter.java +++ b/src/org/kreed/vanilla/AlbumAdapter.java @@ -1,19 +1,19 @@ package org.kreed.vanilla; import android.content.Context; -import android.widget.TextView; public class AlbumAdapter extends AbstractAdapter { public AlbumAdapter(Context context, Song[] allSongs) { - super(context, Song.filter(allSongs, new Song.AlbumComparator()), 0, Song.FIELD_ALBUM); + super(context, Song.filter(allSongs, new Song.AlbumComparator()), MediaView.EXPANDER | MediaView.SECONDARY_LINE, Song.FIELD_ALBUM); } @Override - protected void updateText(int position, TextView upper, TextView lower) + protected void updateView(int position, MediaView view) { Song song = get(position); - upper.setText(song.album); - lower.setText(song.artist); + view.setPrimaryText(song.album); + view.setSecondaryText(song.artist); + view.setMediaId(song.albumId); } } \ No newline at end of file diff --git a/src/org/kreed/vanilla/ArtistAdapter.java b/src/org/kreed/vanilla/ArtistAdapter.java index 9fb265db..3f5aad9f 100644 --- a/src/org/kreed/vanilla/ArtistAdapter.java +++ b/src/org/kreed/vanilla/ArtistAdapter.java @@ -19,18 +19,18 @@ package org.kreed.vanilla; import android.content.Context; -import android.widget.TextView; public class ArtistAdapter extends AbstractAdapter { public ArtistAdapter(Context context, Song[] allSongs) { - super(context, Song.filter(allSongs, new Song.ArtistComparator()), ONE_LINE, Song.FIELD_ARTIST); + super(context, Song.filter(allSongs, new Song.ArtistComparator()), MediaView.EXPANDER, Song.FIELD_ARTIST); } @Override - protected void updateText(int position, TextView upper, TextView lower) + protected void updateView(int position, MediaView view) { Song song = get(position); - upper.setText(song.artist); + view.setPrimaryText(song.artist); + view.setMediaId(song.artistId); } } \ No newline at end of file diff --git a/src/org/kreed/vanilla/MediaView.java b/src/org/kreed/vanilla/MediaView.java new file mode 100644 index 00000000..8530d0ae --- /dev/null +++ b/src/org/kreed/vanilla/MediaView.java @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2010 Christopher Eby + * + * This file is part of Vanilla Music Player. + * + * Vanilla Music Player is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Vanilla Music Player is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.kreed.vanilla; + +import android.content.Context; +import android.graphics.Color; +import android.util.DisplayMetrics; +import android.util.TypedValue; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +public class MediaView extends ViewGroup { + public static final int SECONDARY_LINE = 0x1; + public static final int EXPANDER = 0x2; + + private TextView mPrimaryLine; + private TextView mSecondaryLine; + private ImageView mExpander; + + private int mPadding; + + public MediaView(Context context, int flags) + { + super(context); + + DisplayMetrics metrics = context.getResources().getDisplayMetrics(); + float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, metrics); + mPadding = (int)textSize / 2; + + mPrimaryLine = new TextView(context); + mPrimaryLine.setSingleLine(); + mPrimaryLine.setTextColor(Color.WHITE); + mPrimaryLine.setTextSize(textSize); + addView(mPrimaryLine); + + if ((flags & SECONDARY_LINE) != 0) { + mSecondaryLine = new TextView(context); + mSecondaryLine.setSingleLine(); + mSecondaryLine.setTextSize(textSize); + addView(mSecondaryLine); + } + + if ((flags & EXPANDER) != 0) { + mExpander = new ImageView(context); + mExpander.setPadding(mPadding * 2, mPadding, mPadding, mPadding); + mExpander.setImageResource(R.drawable.expander_arrow); + addView(mExpander); + } + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) + { + int width = MeasureSpec.getSize(widthMeasureSpec); + + widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST); + heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); + + int expanderHeight = 0; + int textHeight = 4 * mPadding; + + if (mExpander != null) { + mExpander.measure(widthMeasureSpec, heightMeasureSpec); + expanderHeight = mExpander.getMeasuredHeight(); + } + + if (mSecondaryLine != null) { + mSecondaryLine.measure(widthMeasureSpec, heightMeasureSpec); + textHeight = mSecondaryLine.getMeasuredHeight() + mPadding; + } + + mPrimaryLine.measure(widthMeasureSpec, heightMeasureSpec); + textHeight += mPrimaryLine.getMeasuredHeight(); + + setMeasuredDimension(width, Math.max(expanderHeight, textHeight)); + } + + @Override + protected void onLayout(boolean changed, int left, int top, int right, int bottom) + { + int width = right - left; + int height = bottom - top; + + int textWidth; + int textHeight = height - 2 * mPadding; + + int actualHeight; + + if (mExpander != null) { + textWidth = width - mExpander.getMeasuredWidth(); + mExpander.layout(textWidth, 0, width, height); + } else { + textWidth = width; + } + + textWidth -= 2 * mPadding; + + if (mSecondaryLine != null) { + textHeight = (textHeight - mPadding) / 2; + + actualHeight = mSecondaryLine.getMeasuredHeight(); + top = mPadding * 3 / 2 + textHeight + (textHeight - actualHeight) / 2; + mSecondaryLine.layout(mPadding, top, mPadding + textWidth, top + actualHeight); + } + + actualHeight = mPrimaryLine.getMeasuredHeight(); + top = mPadding + (textHeight - actualHeight) / 2; + mPrimaryLine.layout(mPadding, top, mPadding + textWidth, top + actualHeight); + } + + public void setupExpander(int field, View.OnClickListener listener) + { + if (mExpander != null) { + mExpander.setTag(R.id.field, field); + mExpander.setOnClickListener(listener); + } + } + public void setPrimaryText(String text) + { + if (mPrimaryLine != null) + mPrimaryLine.setText(text); + } + + public void setSecondaryText(String text) + { + if (mSecondaryLine != null) + mSecondaryLine.setText(text); + } + + public void setMediaId(int id) + { + if (mExpander != null) + mExpander.setTag(R.id.id, id); + } +} \ No newline at end of file diff --git a/src/org/kreed/vanilla/SongAdapter.java b/src/org/kreed/vanilla/SongAdapter.java index 3c47c3a9..0a4fd870 100644 --- a/src/org/kreed/vanilla/SongAdapter.java +++ b/src/org/kreed/vanilla/SongAdapter.java @@ -21,7 +21,6 @@ package org.kreed.vanilla; import java.util.Arrays; import android.content.Context; -import android.widget.TextView; public class SongAdapter extends AbstractAdapter { private static Song[] sort(Song[] songs) @@ -32,14 +31,14 @@ public class SongAdapter extends AbstractAdapter { public SongAdapter(Context context, Song[] allSongs) { - super(ContextApplication.getContext(), sort(allSongs), 0, Song.FIELD_TITLE); + super(ContextApplication.getContext(), sort(allSongs), MediaView.SECONDARY_LINE, Song.FIELD_TITLE); } @Override - protected void updateText(int position, TextView upper, TextView lower) + protected void updateView(int position, MediaView view) { Song song = get(position); - upper.setText(song.title); - lower.setText(song.artist); + view.setPrimaryText(song.title); + view.setSecondaryText(song.artist); } } \ No newline at end of file