在Android中执行全屏图像滑块的最佳方式(如Android Gallery)

时间:2013-11-14 10:02:20

标签: android performance android-fragments android-viewpager

我的数据库中有一些(例如100到200张)图像,我必须在我的应用程序中使用滑块动画显示:

 fullscreen image slider

我尝试使用viewpager,但如果我的数据库中有300张图像,我将不得不为viewpager创建300位图和片段。它应该看起来像android设备如何在图库中显示图像(参见上面的图像以供参考)。这是实现这一目标的最佳方式?我也尝试过自定义循环viewpager(它不支持小于4的图像)。

(见http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/

我也尝试过来自how to create circular viewpager?的链接,但我认为这些链接不是执行任务的有效方式 (如3页逻辑)

我还有另一个疑问

 imageview.setImageURI(Uri.parse(currentvalue.getimagepath()));

如果我使用此代码将图像从db设置为imageview我可以摆脱使用位图还是这种方法也使用位图?

1 个答案:

答案 0 :(得分:3)

我认为最有效的方法是使用AsyncTask。看一个个人的例子:

public class LoadImage extends AsyncTask<Void, Void, String> {
        private LoaderImageView loaderImageView;
        private int position;
        private ImageView image;
        private Bitmap bmd;

        public LoadImage(LoaderImageView loaderImageView, int position) {
            this.loaderImageView = loaderImageView;
            this.position = position;
            image = loaderImageView.getImageView();
        }

        @Override
        protected void onPreExecute() {
            loaderImageView.showProgress();
            super.onPreExecute();
        }

        private void setImageView(int heigthToSet, int weightToSet, int color) {
            // set the imageview to the current bitmap
            Bitmap bitmap = null;
            ReceiptImageDataSource ds = ReceiptImageDataSource.getInstance();
            List<ReceiptImage> images = ds.selectReceiptImagesByReceiptID(currentReceipt.getId());
            if (images.size() > 0) {
                if (BitmapFactory.decodeFile(images.get(position).getPhotoURL()) != null) {
                    if (images.size() > position) {
                        bitmap = BitmapFactory.decodeFile(images.get(position).getPhotoURL());
                        image.setTag(position);
                    } else {
                        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo_take_photo);
                    }
                } else {
                    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.no_photo_receipt_x2);
                }
            }

            assert bitmap != null;
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();

            // calculate the scale - in this case = 0.6f
            float scaleWidth = ((float) weightToSet) / width;
            float scaleHeight = ((float) heigthToSet) / height;

            // create a matrix for the manipulation
            Matrix matrix = new Matrix();
            // resize the bit map
            matrix.postScale(scaleWidth, scaleHeight);

            // recreate the new Bitmap
            Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

            bmd = Utils.getRoundedCornerBitmap(resizedBitmap, color, 8, 0, EditReceiptActivity.this);

        }

        @Override
        protected String doInBackground(Void... arg0) {
            int Measuredwidth;
            int Measuredheight;
            Point size = new Point();
            WindowManager w = getWindowManager();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                w.getDefaultDisplay().getSize(size);
                Measuredwidth = size.x;
                Measuredheight = size.y;
            } else {
                Display d = w.getDefaultDisplay();
                Measuredwidth = d.getWidth();
                Measuredheight = d.getHeight();
            }

            setImageView(Measuredheight - Utils.dpToPx(120, EditReceiptActivity.this), Measuredwidth - Utils.dpToPx(60, EditReceiptActivity.this),
                    Color.TRANSPARENT);
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            loaderImageView.hideProgress();
            image.setImageBitmap(bmd);
            image.setBackgroundColor(Color.TRANSPARENT);

            // center the Image
            image.setScaleType(ScaleType.FIT_CENTER);
            super.onPostExecute(result);
        }

    }