我想在我的項目中使用UIL gridview佈局。出於測試目的,我修改了項目本身的代碼。我的要求是顯示一個TransitionDrawable與原始位圖作爲基礎圖像在零層和第一層的刻度線。這裏是自定義的BitmapDisplayer代碼。ClassCastException當使用自定義顯示器
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 Android模擬器的代碼。問題依然存在。有人請幫助我。 – 2013-03-19 14:06:17
LogCat的Stacktrace? – NOSTRA 2013-03-19 14:38:32
請對您的帖子進行修改,而不是發佈包含更新的答案。 – jprofitt 2013-03-23 15:31:23