Fancy Coverflow显示2位图而不是1位图

时间:2014-01-01 07:24:00

标签: android android-layout android-imageview android-gallery

在使用ImageLoader的Fancy davidschreiber / FancyCoverFlow 中,我的Coverflow显示2张图片而不是1张图片。

我的ImageLoader类是:

public class ImageLoader {

MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
Handler handler = new Handler();// handler to display images in UI thread

Context mContext;
boolean isReflectionEnabled;

public ImageLoader(Context context) {
    fileCache = new FileCache(context);
    executorService = Executors.newFixedThreadPool(5);
    this.mContext = context;
    this.stubImageBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.default_image);
}

final Bitmap stubImageBitmap;

public void displayImageFromUrl(String url, ImageView imageView, boolean isReflectionEnabled) {
    imageViews.put(imageView, url);
    Bitmap bitmap = memoryCache.get(url);
    this.isReflectionEnabled = isReflectionEnabled;
    if (bitmap != null) {
        if (this.isReflectionEnabled)
            imageView.setImageBitmap(createReflectedImage(bitmap));
        else
            imageView.setImageBitmap(bitmap);
    } else {
        queuePhoto(url, imageView);
        if (this.isReflectionEnabled)
            imageView.setImageBitmap(createReflectedImage(stubImageBitmap));
        else
            imageView.setImageBitmap(stubImageBitmap);
    }
}

private void queuePhoto(String url, ImageView imageView) {
    PhotoToLoad p = new PhotoToLoad(url, imageView);
    executorService.submit(new PhotosLoader(p));
}

private Bitmap getBitmap(String url) {
    File f = fileCache.getFile(url);

    // from SD cache
    Bitmap b = decodeFile(f);
    if (b != null)
        return b;

    // from web
    try {
        Bitmap bitmap = null;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is = conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        ActivityUtils.copyStream(is, os);
        os.close();
        conn.disconnect();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Throwable ex) {
        ex.printStackTrace();
        if (ex instanceof OutOfMemoryError)
            memoryCache.clear();
        return null;
    }
}

// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
    try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        FileInputStream stream1 = new FileInputStream(f);
        BitmapFactory.decodeStream(stream1, null, o);
        stream1.close();

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 70;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        FileInputStream stream2 = new FileInputStream(f);
        Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
        stream2.close();
        return bitmap;
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

// Task for the queue
private class PhotoToLoad {
    public String url;
    public ImageView imageView;

    public PhotoToLoad(String u, ImageView i) {
        url = u;
        imageView = i;
    }
}

class PhotosLoader implements Runnable {
    PhotoToLoad photoToLoad;

    PhotosLoader(PhotoToLoad photoToLoad) {
        this.photoToLoad = photoToLoad;
    }

    @Override
    public void run() {
        try {
            if (imageViewReused(photoToLoad))
                return;
            Bitmap bmp = getBitmap(photoToLoad.url);
            memoryCache.put(photoToLoad.url, bmp);
            if (imageViewReused(photoToLoad))
                return;
            BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
            handler.post(bd);
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }
}

boolean imageViewReused(PhotoToLoad photoToLoad) {
    String tag = imageViews.get(photoToLoad.imageView);
    if (tag == null || !tag.equals(photoToLoad.url))
        return true;
    return false;
}

// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
    Bitmap bitmap;
    PhotoToLoad photoToLoad;

    public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
        bitmap = b;
        photoToLoad = p;
    }

    public void run() {
        if (imageViewReused(photoToLoad))
            return;
        if (bitmap != null) {
            if (isReflectionEnabled)
                photoToLoad.imageView.setImageBitmap(createReflectedImage(bitmap));
            else
                photoToLoad.imageView.setImageBitmap(bitmap);
        } else {
            if (isReflectionEnabled)
                photoToLoad.imageView.setImageBitmap(createReflectedImage(stubImageBitmap));
            else
                photoToLoad.imageView.setImageBitmap(stubImageBitmap);
        }
    }
}

public void clearCache() {
    memoryCache.clear();
    fileCache.clear();
}

private Bitmap createReflectedImage(Bitmap originalImage) {
    final int reflectionGap = 4;

    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    // This will not scale but will flip on the Y axis
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    // Create a Bitmap with the flip matrix applied to it.
    // We only want the bottom half of the image
    Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false);

    // Create a new bitmap with same width but taller to fit reflection
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    // Create a new Canvas with the bitmap that's big enough for
    // the image plus gap plus reflection
    Canvas canvas = new Canvas(bitmapWithReflection);
    // Draw in the original image
    canvas.drawBitmap(originalImage, 0, 0, null);
    // Draw in the gap
    Paint deafaultPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
    // Draw in the reflection
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    // Create a shader that is a linear gradient that covers the reflection
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    // Set the paint to use this shader (linear gradient)
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}
}

This is the coverflow I am seeing

1 个答案:

答案 0 :(得分:1)

尝试添加此 // Edited by @Gorcyn { childView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); childView.setDrawingCacheEnabled(true); }

在类FancyCoverFlowItemWrapper中的方法protected void dispatchDraw(Canvas canvas),所以它看起来像:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void dispatchDraw(Canvas canvas) {
    View childView = getChildAt(0);

    if (childView != null) {

        // If on honeycomb or newer, cache the view.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            if (childView.isDirty() && !childView.isDrawingCacheEnabled()) {
                childView.draw(this.wrappedViewDrawingCanvas);

                // Edited by @Gorcyn
                {
                    childView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
                    childView.setDrawingCacheEnabled(true);
                }
                if (this.isReflectionEnabled) {
                    this.createReflectedImages();
                }
            }
        } else {
            childView.draw(this.wrappedViewDrawingCanvas);
        }
    }

    canvas.drawBitmap(this.wrappedViewBitmap, (this.getWidth() - childView.getWidth()) / 2, 0, this.paint);
}