2

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

全屏图像滑块

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

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

我还尝试了如何创建圆形查看器的链接?但我认为这些链接不会是完成我的任务的有效方式(如 3 页逻辑)

我还有一个疑问

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

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

4

1 回答 1

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);
        }

    }
于 2013-11-14T12:58:00.693 回答