Integrate MediaView into MediaAdapter

This commit is contained in:
Christopher Eby 2010-03-20 16:50:06 -05:00
parent 9b7254cd5c
commit cb013cc51e
3 changed files with 109 additions and 134 deletions

View File

@ -26,6 +26,13 @@ import java.util.regex.Pattern;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
@ -76,7 +83,7 @@ public class MediaAdapter extends BaseAdapter implements Filterable {
}
if (view == null)
view = new MediaView(mContext, mPrimaryField, mSecondaryField, mExpanderListener != null);
view = new MediaView(mContext);
view.updateMedia(get(position));
@ -257,4 +264,104 @@ public class MediaAdapter extends BaseAdapter implements Filterable {
intent.putExtra("title", song.getField(mPrimaryField));
return intent;
}
private static float mTextSize = -1;
private static Bitmap mExpander = null;
private int mViewHeight = -1;
public class MediaView extends View {
private SongData mData;
private boolean mExpanderPressed;
public MediaView(Context context)
{
super(context);
if (mExpander == null)
mExpander = BitmapFactory.decodeResource(context.getResources(), R.drawable.expander_arrow);
if (mTextSize == -1)
mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, context.getResources().getDisplayMetrics());
if (mViewHeight == -1)
mViewHeight = measureHeight();
}
private int measureHeight()
{
int expanderHeight;
int textHeight;
if (mExpanderListener != null)
expanderHeight = mExpander.getHeight() + (int)mTextSize;
else
expanderHeight = 0;
if (mSecondaryField != -1)
textHeight = (int)(7 * mTextSize / 2);
else
textHeight = (int)(2 * mTextSize);
return Math.max(expanderHeight, textHeight);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mViewHeight);
}
@Override
public void onDraw(Canvas canvas)
{
if (mData == null)
return;
int width = getWidth();
int height = getHeight();
float padding = mTextSize / 2;
Paint paint = new Paint();
paint.setTextSize(mTextSize);
paint.setAntiAlias(true);
if (mExpanderListener != null) {
width -= padding * 3 + mExpander.getWidth();
canvas.drawBitmap(mExpander, width + padding * 2, (height - mExpander.getHeight()) / 2, paint);
}
canvas.clipRect(padding, 0, width - padding, height);
float allocatedHeight;
if (mSecondaryField != -1) {
allocatedHeight = height / 2 - padding * 3 / 2;
paint.setColor(Color.GRAY);
canvas.drawText(mData.getField(mSecondaryField), padding, height / 2 + padding / 2 + (allocatedHeight - mTextSize) / 2 - paint.ascent(), paint);
} else {
allocatedHeight = height - padding * 2;
}
paint.setColor(Color.WHITE);
canvas.drawText(mData.getField(mPrimaryField), padding, padding + (allocatedHeight - mTextSize) / 2 - paint.ascent(), paint);
}
public void updateMedia(SongData data)
{
mData = data;
invalidate();
}
public SongData getExpanderData()
{
return mExpanderPressed ? mData : null;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
mExpanderPressed = mExpanderListener != null && event.getX() > getWidth() - mExpander.getWidth() - 3 * mTextSize / 2;
return false;
}
}
}

View File

@ -1,132 +0,0 @@
/*
* Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.kreed.vanilla;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
public class MediaView extends View {
private static float mTextSize = -1;
private static Bitmap mExpander = null;
private SongData mData;
private int mPrimaryField;
private int mSecondaryField;
private boolean mHasExpander;
private boolean mExpanderPressed;
public MediaView(Context context, int primaryField, int secondaryField, boolean hasExpander)
{
super(context);
if (mExpander == null)
mExpander = BitmapFactory.decodeResource(context.getResources(), R.drawable.expander_arrow);
if (mTextSize == -1) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, metrics);
}
mPrimaryField = primaryField;
mSecondaryField = secondaryField;
mHasExpander = hasExpander;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int expanderHeight;
int textHeight;
if (mHasExpander)
expanderHeight = mExpander.getHeight() + (int)mTextSize;
else
expanderHeight = 0;
if (mSecondaryField != -1)
textHeight = (int)(7 * mTextSize / 2);
else
textHeight = (int)(2 * mTextSize);
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
Math.max(expanderHeight, textHeight));
}
@Override
public void onDraw(Canvas canvas)
{
if (mData == null)
return;
int width = getWidth();
int height = getHeight();
float padding = mTextSize / 2;
Paint paint = new Paint();
paint.setTextSize(mTextSize);
paint.setAntiAlias(true);
if (mHasExpander) {
width -= padding * 3 + mExpander.getWidth();
canvas.drawBitmap(mExpander, width + padding * 2, (height - mExpander.getHeight()) / 2, paint);
}
canvas.clipRect(padding, 0, width - padding, height);
float allocatedHeight;
if (mSecondaryField != -1) {
allocatedHeight = height / 2 - padding * 3 / 2;
paint.setColor(Color.GRAY);
canvas.drawText(mData.getField(mSecondaryField), padding, height / 2 + padding / 2 + (allocatedHeight - mTextSize) / 2 - paint.ascent(), paint);
} else {
allocatedHeight = height - padding * 2;
}
paint.setColor(Color.WHITE);
canvas.drawText(mData.getField(mPrimaryField), padding, padding + (allocatedHeight - mTextSize) / 2 - paint.ascent(), paint);
}
public void updateMedia(SongData data)
{
mData = data;
invalidate();
}
public SongData getExpanderData()
{
return mExpanderPressed ? mData : null;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
mExpanderPressed = mHasExpander && event.getX() > getWidth() - mExpander.getWidth() - 3 * mTextSize / 2;
return false;
}
}

View File

@ -168,7 +168,7 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
public void onItemClick(AdapterView<?> list, View view, int pos, long id)
{
MediaAdapter adapter = (MediaAdapter)list.getAdapter();
SongData data = ((MediaView)view).getExpanderData();
SongData data = ((MediaAdapter.MediaView)view).getExpanderData();
if (data != null) {
int field = adapter.getPrimaryField();
SongData.Field limiter = new SongData.Field(field, data);