android - 在 Android 中做全屏图像 slider 的最佳方式(如 Android Gallery)

标签 android performance android-fragments android-viewpager

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

 fullscreen image slider

我尝试使用 viewpager,但如果我的数据库中有 300 张图像,我将不得不为 viewpager 创建 300 个位图和 fragment 。它应该看起来像 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,我可以摆脱使用位图还是这种方法也使用位图?

最佳答案

我认为最有效的方法是为此使用 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);
        }

    }

关于android - 在 Android 中做全屏图像 slider 的最佳方式(如 Android Gallery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19974676/

相关文章:

android - Firebase:如何实时捕获数据

android - Android 中的 2 路 SSL : client authentication not working

performance - Azure 网站 Kudu HTMLLog 分析显示始终开启且响应时间较长

android - 如何获取 fragment 的ID?

android - 当应用程序恢复时,关闭的对话框 fragment 再次出现

android - 当我导航回到 fragment 时, fragment 的内容消失了

android - Android 中的 iOS UILabel 等价物?

android - 在android中有没有办法比较两个音频文件之间的相似性?

java - 为什么执行 Mockito 模拟的性能如此不稳定?

python - Postgres 调整 max_connections