Android:具有滑动跳过图像和缓慢加载的图库

Android:具有滑动跳过图像和缓慢加载的图库,android,performance,android-gallery,image-preloader,Android,Performance,Android Gallery,Image Preloader,大家好,我是android新手,所以对我要温柔:), 我有画廊,我需要加载图片,我有很多图片,我用AsyncTask解决了图像加载的性能问题。 现在我有画廊滚动的问题,它的跳过图像,它看起来不好 现在我已经尝试实现我的自定义图库,但没有成功,它只是不起作用 现在我还有一个问题 final ScrollableGallery galleryView = (ScrollableGallery)findViewById(R.id.gallery); //always returns null 突

大家好,我是android新手,所以对我要温柔:),
我有画廊,我需要加载图片,我有很多图片,我用AsyncTask解决了图像加载的性能问题。

现在我有画廊滚动的问题,它的跳过图像,它看起来不好

现在我已经尝试实现我的自定义图库,但没有成功,它只是不起作用
现在我还有一个问题

final ScrollableGallery galleryView = (ScrollableGallery)findViewById(R.id.gallery); //always returns null
突然,你能解释一下为什么吗

代码如下:


适配器初始化:

galleryV.setAdapter(new BaseAdapter() {                     
            public int getCount() {             
                return _imageList.size();
            }

            public Object getItem(int position) {
                return _imageList.get(position);
            }

            public long getItemId(int position) {
                _position = position;
                return position;
            }

            public View getView(final int position, View convertView,final ViewGroup parent) {              
                if (convertView == null) {
                    convertView = new ImageView(Database.this);
                }               
                ImageDispatcher dispatch = new ImageDispatcher(((ImageView) convertView));              
                dispatch.execute(_imageList.get(position));                                     
                ((ImageView) convertView).setScaleType(ImageView.ScaleType.FIT_XY);
                ((ImageView) convertView).setLayoutParams(new ScrollableGallery.LayoutParams(675, 492));

                return convertView;
            }           
        });
<br/>
The image dispatcher:

    import java.lang.ref.WeakReference;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.AsyncTask;
    import android.widget.ImageView;

    public class ImageDispatcher extends AsyncTask<String, Void, Bitmap> {

        private final WeakReference<ImageView> imageViewReference;

        public ImageDispatcher(ImageView imageView) {
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        @Override
        // Actual download method, run in the task thread
        protected Bitmap doInBackground(String... params) { 

            return ShrinkBitmap(params[0], 300 ,300);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (isCancelled()) {
                bitmap = null;
            }

            if (imageViewReference != null) {
                ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }

        /*********************************************************
         * PRIVATE METHODS 
         *********************************************************/
        @SuppressWarnings("unused")
        private Bitmap ShrinkBitmap(String file, int width, int height) {
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inTempStorage = new byte[16 * 1024];
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
            int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / (float) height);
            int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / (float) width);

            bmpFactoryOptions.inSampleSize = heightRatio > 1 || widthRatio > 1 ? heightRatio : widthRatio;                  
            bmpFactoryOptions.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
            return bitmap;
        }

        @SuppressWarnings("unused")
        private Bitmap ShrinkBitmap(String file) {
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inTempStorage = new byte[16 * 1024];
            bmpFactoryOptions.inJustDecodeBounds = true;

            Bitmap bMap = BitmapFactory.decodeFile(file);       
            Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap,150, 100, true);

            return bMapScaled;
        }
    }
galleryV.setAdapter(新的BaseAdapter(){
public int getCount(){
返回_imageList.size();
}
公共对象getItem(int位置){
返回imageList.get(位置);
}
公共长getItemId(int位置){
_位置=位置;
返回位置;
}
公共视图getView(最终整型位置、视图转换视图、最终视图组父级){
if(convertView==null){
convertView=新的ImageView(Database.this);
}               
ImageDispatcher Dispatcher=新的ImageDispatcher(((ImageView)convertView));
dispatch.execute(_imageList.get(position));
((ImageView)convertView).setScaleType(ImageView.ScaleType.FIT_-XY);
setLayoutParams(新的ScrollableGallery.LayoutParams(675492));
返回视图;
}           
});

映像调度器: 导入java.lang.ref.WeakReference; 导入android.graphics.Bitmap; 导入android.graphics.BitmapFactory; 导入android.os.AsyncTask; 导入android.widget.ImageView; 公共类ImageDispatcher扩展异步任务{ 私有最终WeakReference imageViewReference; 公共ImageDispatcher(ImageView ImageView){ imageViewReference=新的WeakReference(imageView); } @凌驾 //实际下载方法,在任务线程中运行 受保护的位图doInBackground(字符串…参数){ 返回收缩位图(参数[0],300,300); } @凌驾 受保护的void onPostExecute(位图){ 如果(isCancelled()){ 位图=空; } if(imageViewReference!=null){ ImageView=imageViewReference.get(); 如果(imageView!=null){ 设置图像位图(位图); } } } /********************************************************* *私有方法 *********************************************************/ @抑制警告(“未使用”) 私有位图收缩位图(字符串文件、整型宽度、整型高度){ Options bmpFactoryOptions=新的BitmapFactory.Options(); bmpFactoryOptions.inTempStorage=新字节[16*1024]; bmpFactoryOptions.inJustDecodeBounds=true; 位图位图=BitmapFactory.decodeFile(文件,bmpFactoryOptions); int heightRatio=(int)Math.ceil(bmpFactoryOptions.outHeight/(float)height); int widthRatio=(int)Math.ceil(bmpFactoryOptions.outWidth/(float)width); bmpFactoryOptions.inSampleSize=高度比>1 | |宽度比>1?高度比:宽度比; bmpFactoryOptions.inJustDecodeBounds=false; 位图=BitmapFactory.decodeFile(文件,bmpFactoryOptions); 返回位图; } @抑制警告(“未使用”) 私有位图收缩位图(字符串文件){ Options bmpFactoryOptions=新的BitmapFactory.Options(); bmpFactoryOptions.inTempStorage=新字节[16*1024]; bmpFactoryOptions.inJustDecodeBounds=true; 位图bMap=BitmapFactory.decodeFile(文件); 位图bMapScaled=Bitmap.createScaledBitmap(bMap,150100,true); 返回bMapScaled; } }
我想这是我这部分的全部代码, 现在的问题是:
我如何制作滚动,一次一个图像,以及如何在加载图像时像在web中一样显示加载指示器

任何代码修复都将不胜感激


提前感谢您……

在您的自定义图库中

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
    -->> super.onFling(e1, e2, 20, velocityY); <<--

    return false;
}
@覆盖
公共布尔onFling(MotionEvent e1、MotionEvent e2、float-velocityX、float-velocityY){

-->>super.onFling(e1,e2,20,velocityY);而不是在xml中声明库

Gallery gallery = new  CustomGaller(getActivity());

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams  (LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
            params.addRule(RelativeLayout.RIGHT_OF, R.id.left);
            params.addRule(RelativeLayout.LEFT_OF,R.id.right);
            gallery.setLayoutParams(params);
            gallery.setAdapter(new GalleryAdapter(<Pass values>));
Gallery=newcustomgaller(getActivity());
RelativeLayout.LayoutParams params=新的RelativeLayout.LayoutParams(LayoutParams.FILL\u父级,LayoutParams.FILL\u父级);
参数addRule(RelativeLayout.RIGHT_OF,R.id.left);
参数addRule(RelativeLayout.LEFT_OF,R.id.right);
gallery.setLayoutParams(参数);
gallery.setAdapter(新GalleryAdapter());
您必须将视图id传递给CustomGallery

galleryV.setAdapter(new BaseAdapter() {                     
            public int getCount() {             
                return _imageList.size();
            }

            public Object getItem(int position) {
                return _imageList.get(position);
            }

            public long getItemId(int position) {
                _position = position;
                return position;
            }

            public View getView(final int position, View convertView,final ViewGroup parent) {              
                if (convertView == null) {
                    convertView = new ImageView(Database.this);
                }               
                ImageDispatcher dispatch = new ImageDispatcher(((ImageView) convertView));              
                dispatch.execute(_imageList.get(position));                                     
                ((ImageView) convertView).setScaleType(ImageView.ScaleType.FIT_XY);
                ((ImageView) convertView).setLayoutParams(new ScrollableGallery.LayoutParams(675, 492));

                return convertView;
            }           
        });
<br/>
The image dispatcher:

    import java.lang.ref.WeakReference;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.AsyncTask;
    import android.widget.ImageView;

    public class ImageDispatcher extends AsyncTask<String, Void, Bitmap> {

        private final WeakReference<ImageView> imageViewReference;

        public ImageDispatcher(ImageView imageView) {
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        @Override
        // Actual download method, run in the task thread
        protected Bitmap doInBackground(String... params) { 

            return ShrinkBitmap(params[0], 300 ,300);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (isCancelled()) {
                bitmap = null;
            }

            if (imageViewReference != null) {
                ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }

        /*********************************************************
         * PRIVATE METHODS 
         *********************************************************/
        @SuppressWarnings("unused")
        private Bitmap ShrinkBitmap(String file, int width, int height) {
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inTempStorage = new byte[16 * 1024];
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
            int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / (float) height);
            int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / (float) width);

            bmpFactoryOptions.inSampleSize = heightRatio > 1 || widthRatio > 1 ? heightRatio : widthRatio;                  
            bmpFactoryOptions.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
            return bitmap;
        }

        @SuppressWarnings("unused")
        private Bitmap ShrinkBitmap(String file) {
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inTempStorage = new byte[16 * 1024];
            bmpFactoryOptions.inJustDecodeBounds = true;

            Bitmap bMap = BitmapFactory.decodeFile(file);       
            Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap,150, 100, true);

            return bMapScaled;
        }
    }
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
    -->> super.onFling(e1, e2, 20, velocityY); <<--

    return false;
}
Gallery gallery = new  CustomGaller(getActivity());

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams  (LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
            params.addRule(RelativeLayout.RIGHT_OF, R.id.left);
            params.addRule(RelativeLayout.LEFT_OF,R.id.right);
            gallery.setLayoutParams(params);
            gallery.setAdapter(new GalleryAdapter(<Pass values>));