我正在尝试开发
Android 3.1平板电脑应用程序.
这个应用程序将有很多图像,我已经按照Processing Bitmaps Off the UI Thread教程,但我做错了,因为我得到:
java.lang.classCastException:android.widget.AbsListView $LayoutParams无法强制转换为android.widget.gallery $LayoutParams
这是我的代码:
mFactgallery = (gallery)mView.findViewById(R.id.factgallery); mFactgallery.setAdapter(new ImagegalleryAdapter(mActivity,ImageView.ScaleType.FIT_END,180,90));
ImagegalleryAdapter.java
public class ImagegalleryAdapter extends BaseAdapter { private ArrayList<String> mImagesPath; private Context mContext; private int mWidth; private int mHeight; public ArrayList<String> getmImagesPath() { return mImagesPath; } public void setmImagesPath(ArrayList<String> mImagesPath) { this.mImagesPath = mImagesPath; } public void addImage(String imagePath) { mImagesPath.add(imagePath); } public ImagegalleryAdapter(Context context,ImageView.ScaleType scaleType,int width,int height) { mContext = context; mWidth = width; mHeight = height; mScaleType = scaleType; mImagesPath = new ArrayList<String>(); } @Override public int getCount() { return mImagesPath.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position,View convertView,ViewGroup parent) { // Get a View to display image data ImageView imageView; // if it's not recycled,initialize some attributes if (convertView == null) { imageView = new ImageView(mContext); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setLayoutParams(new GridView.LayoutParams( LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); } else { imageView = (ImageView) convertView; // Recicla el Bitmap. Bitmap bm = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); if (bm != null) bm.recycle(); } String filePath = mImagesPath.get(position); if (BitmapTools.cancelPotentialWork(filePath,imageView)) { String[] params = {filePath,Integer.toString(mWidth),Integer.toString(mHeight)}; final BitmapWorkerTask task = new BitmapWorkerTask(imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable(mContext.getResources(),filePath,task); imageView.setimageDrawable(asyncDrawable); task.execute(params); } return imageView; } }
BitmapWorkerTask.java
public class BitmapWorkerTask extends AsyncTask<String,Void,Bitmap> { private final WeakReference<ImageView> imageViewReference; public String imgPath = ""; public BitmapWorkerTask(ImageView imageView) { // Use a WeakReference to ensure the ImageView can be garbage collected imageViewReference = new WeakReference<ImageView>(imageView); } // Decode image in background. @Override protected Bitmap doInBackground(String... params) { imgPath = params[0]; int width = Integer.valueOf(params[1]).intValue(); int height = Integer.valueOf(params[2]).intValue(); return BitmapTools.decodeSampledBitmapFromdisk(imgPath,width,height); } // Once complete,see if ImageView is still around and set bitmap. @Override protected void onPostExecute(Bitmap bitmap) { if (isCancelled()) { bitmap = null; } if (imageViewReference != null && bitmap != null) { final ImageView imageView = imageViewReference.get(); final BitmapWorkerTask bitmapWorkerTask = BitmapTools.getBitmapWorkerTask(imageView); if (this == bitmapWorkerTask && imageView != null) { imageView.setimageBitmap(bitmap); } } } }
AsyncDrawable.java
public class AsyncDrawable extends BitmapDrawable { private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference; public AsyncDrawable(Resources res,String filepath,BitmapWorkerTask bitmapWorkerTask) { super(res,filepath); bitmapWorkerTaskReference = new WeakReference<BitmapWorkerTask>(bitmapWorkerTask); } public BitmapWorkerTask getBitmapWorkerTask() { return bitmapWorkerTaskReference.get(); } }
我究竟做错了什么?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。