0

我正在尝试在我的项目中将 UIL 与 gridview 布局一起使用。出于测试目的,我修改了您项目本身的代码。我的要求是在第 0 层显示一个带有原始位图的 TransitionDrawable 作为基础图像,并在第 1 层显示一个刻度线。这是自定义 BitmapDisplayer 代码。

public class customBitmapDisplayer implements BitmapDisplayer
{

public static Drawable tick;
Drawable[] layers = new Drawable[2];
TransitionDrawable mytransition;

@Override
public Bitmap display(Bitmap bitmap, ImageView imageView)
{
BitmapDrawable base_image = new BitmapDrawable(imageView.getResources(), bitmap);
layers[0] = base_image;
layers[1] = tick;

mytransition = new TransitionDrawable(layers);
imageView.setImageDrawable(mytransition);
return bitmap;
}

public customBitmapDisplayer(Drawable tick_mark)
{
tick = tick_mark;
}
}

我还稍微修改了 getView。这是代码。

if(selected[position])  //selected is a boolean array
{
TransitionDrawable quickTransition = (TransitionDrawable)imageView.getDrawable();
quickTransition.startTransition(0);
}

顺便说一句,我使用的是 UIL 1.8.0,默认 ImageConfiguration,DisplayImageOptions 修改为与自定义 BitmapDisplayer 一起使用,其代码在上面。我在 android 2.3.4 上进行了测试,发现没有问题,但是在 android 4.0.4 上,我在 getView 方法中得到了一个 ClassCastException,说当我在“选择”每个图像后滚动整个 gridview 时,BitmapDrawable 无法转换为 TransitionDrawable。

这是堆栈跟踪。

03-20 20:46:40.797: E/AndroidRuntime(11943): FATAL EXCEPTION: main
03-20 20:46:40.797: E/AndroidRuntime(11943): java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.TransitionDrawable
03-20 20:46:40.797: E/AndroidRuntime(11943):    at com.nostra13.example.universalimageloader.ImageGridActivity$ImageAdapter.getView(ImageGridActivity.java:219)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.AbsListView.obtainView(AbsListView.java:2019)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.GridView.makeAndAddView(GridView.java:1338)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.GridView.makeRow(GridView.java:343)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.GridView.fillUp(GridView.java:384)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.GridView.fillGap(GridView.java:260)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4567)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3834)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.os.Handler.handleCallback(Handler.java:605)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.os.Looper.loop(Looper.java:137)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at android.app.ActivityThread.main(ActivityThread.java:4448)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at java.lang.reflect.Method.invokeNative(Native Method)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at java.lang.reflect.Method.invoke(Method.java:511)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-20 20:46:40.797: E/AndroidRuntime(11943):    at dalvik.system.NativeStart.main(Native Method)
03-20 20:46:44.377: I/Process(11943): Sending signal. PID: 11943 SIG: 9

ImageGridActivity.java 在第 219 行有以下代码。

TransitionDrawable quickTransition = (TransitionDrawable)imageView.getDrawable();

这是我的 getView 代码。

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    final ImageView imageView;
    if (convertView == null)
    {
        imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
    }
    else
    {
        imageView = (ImageView) convertView;
    }

    imageLoader.displayImage(imageUrls[position], imageView, options);

    if(selected[position])
    {

        TransitionDrawable quickTransition = (TransitionDrawable)imageView.getDrawable();
        quickTransition.startTransition(0);

    }
    return imageView;
}
4

1 回答 1

0

我认为您应该将getView()方法更改为以下内容:

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    final ImageView imageView;
    if (convertView == null) {
        imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
    } else {
        imageView = (ImageView) convertView;
    }

    imageLoader.displayImage(imageUrls[position], imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                if(selected[position]) {
                    TransitionDrawable quickTransition = (TransitionDrawable)imageView.getDrawable();
                    quickTransition.startTransition(0);
                }
            }
        });
    return imageView;
}
于 2013-03-29T22:18:31.290 回答