抽象适配器
package pl.polidea.coverflow;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
/**
* This class is an adapter that provides base, abstract class for images
* adapter.
*
*/
public abstract class AbstractCoverFlowImageAdapter extends BaseAdapter {
/** The Constant TAG. */
private static final String TAG = AbstractCoverFlowImageAdapter.class.getSimpleName();
/** The width. */
private float width = 0;
/** The height. */
private float height = 0;
/** The bitmap map. */
private final Map<Integer, WeakReference<Bitmap>> bitmapMap = new HashMap<Integer, WeakReference<Bitmap>>();
public AbstractCoverFlowImageAdapter() {
super();
}
/**
* Set width for all pictures.
*
* @param width
* picture height
*/
public synchronized void setWidth(final float width) {
this.width = width;
}
/**
* Set height for all pictures.
*
* @param height
* picture height
*/
public synchronized void setHeight(final float height) {
this.height = height;
}
@Override
public final Bitmap getItem(int position) {
final WeakReference<Bitmap> weakBitmapReference = bitmapMap.get(position);
if (weakBitmapReference != null) {
final Bitmap bitmap = weakBitmapReference.get();
if (bitmap == null) {
// Log.v(TAG, "Empty bitmap reference at position: " + position + ":" + this);
} else {
// Log.v(TAG, "Reusing bitmap item at position: " + position + ":" + this);
return bitmap;
}
}
Log.v(TAG, "Creating item at position: " + position + ":" + this);
final Bitmap bitmap = createBitmap(position);
bitmapMap.put(position, new WeakReference<Bitmap>(bitmap));
// Log.v(TAG, "Created item at position: " + position + ":" + this);
return bitmap;
}
/**
* Creates new bitmap for the position specified.
*
* @param position
* position
* @return Bitmap created
*/
protected abstract Bitmap createBitmap(int position);
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public final synchronized long getItemId(final int position) {
return position;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
@Override
public final synchronized ImageView getView(final int position, final View convertView, final ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
final Context context = parent.getContext();
// Log.v(TAG, "Creating Image view at position: " + position + ":" + this);
imageView = new ImageView(context);
imageView.setLayoutParams(new CoverFlow.LayoutParams((int) width, (int) height));
} else {
// Log.v(TAG, "Reusing view at position: " + position + ":" + this);
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(getItem(position));
return imageView;
}
}
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white">
<!-- <view class="pl.polidea.coverflow.CoverFlow" xmlns:coverflow="http://schemas.android.com/apk/res/pl.polidea.coverflow"
coverflow:imageWidth="100dip" coverflow:imageHeight="150dip" android:id="@+id/coverflow" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="5dip">
</view> -->
<TextView android:text="STATUS" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:textColor="#ffffff"
android:padding="5dip" android:id="@+id/statusText"></TextView>
<pl.polidea.coverflow.CoverFlow xmlns:coverflow="http://schemas.android.com/apk/res/pl.polidea.coverflow"
coverflow:imageWidth="170dp" coverflow:imageHeight="170dp" coverflow:withReflection="true"
coverflow:imageReflectionRatio="0.4" coverflow:reflectionGap="4dp" android:id="@+id/coverflowReflect"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="25dp"
android:layout_marginBottom="20dp" />
</LinearLayout>
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CoverFlow">
<attr name="imageWidth" format="dimension" />
<attr name="imageHeight" format="dimension" />
<attr name="withReflection" format="boolean" />
<attr name="reflectionGap" format="dimension" />
<attr name="imageReflectionRatio" format="float" />
</declare-styleable>
</resources>