【问题标题】:How can i set an onclick for imageview to display full image?如何为 imageview 设置 onclick 以显示完整图像?
【发布时间】:2014-07-02 15:51:51
【问题描述】:

我有一个代码可以动态地将图像和图像视图添加到我的应用程序中。我想为所有图像视图设置一个 onClick,以便在单击每个图像时,图像会完整显示,就像当您单击 facebook 图像时它会打开并且您可以单击下一个和上一个一样。我已经看到了很多其他问题和答案,但没有一个符合我的要求。

 final int[] images = { R.drawable.abi1, R.drawable.abi2, R.drawable.abi3,
            R.drawable.abi4 };

    LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.bottomView);

    for (int x = 0; x < images.length; x++) {
        final Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
                images[x]);

        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        int newWidth = 200;
        int newHeight = 200;

        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();

        matrix.postScale(scaleWidth, scaleHeight);
        matrix.postRotate(0);

        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
                height, matrix, true);
        BitmapDrawable bmd = new BitmapDrawable(getResources(),
                resizedBitmap);

        ImageView imageView = new ImageView(this);
        imageView.setPadding(2, 0, 9, 5);
        imageView.setImageDrawable(bmd);
        imageView.setTag(x);

        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        linearLayout1.addView(imageView);
    }

【问题讨论】:

  • 您已经在 imageView 上设置了 OnClickListener。你能更具体地谈谈你的问题吗?您是在问如何实现 onClick() 方法以全屏显示图像?
  • 由于您有 OnClickListener(如循环所述),您始终可以制作自定义“弹出窗口”,其中已显示单击的图像,然后添加您自己的下一个/上一个按钮将显示下一个/上一个图像,如果有的话。但是我同意Loop,你应该解释得更好,这是什么问题?
  • @Loop 我在问如何为图像实现 onClick() 侦听器,以便它完全按照 Elven 所说的那样工作
  • @Elven 是的......这就是我要问的......我如何实现弹出窗口??

标签: android image bitmap imageview


【解决方案1】:

onClick()方法你可以开始新的活动

Intent intent = new Intent(YourActivity.this, YourFullScreenActivity.class);
intent.putExtra("selected_image", int imageResourceId);
startActivity(intent);

在您的新活动中,使用 ViewPager 全屏显示图像。 ViewPager 会让你切换到下一张图片。有很多关于实现 ViewPager 的教程。 请注意,在意图内,我额外传递了“selected_image”。您的新活动可以使用它来显示立即选择的图像。

顺便说一句,如果您的代码具有非常高分辨率的图像,则可能会出现内存不足错误。这个站点http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 解释了如何有效地加载位图。如果您只显示应用资源文件夹中的图像,您可以完全跳过加载位图。只需致电setImageResource(),如果需要,使用setLayoutParams()setScaleType() 设置大小。 Android 系统会为你缩放它们。

【讨论】:

  • 谢谢我明白了,但是现在我已经实现了我的全屏活动,我如何将图像资源从我的活动类添加到我的寻呼机类,因为我动态添加了资源?无论如何,也许我会问这个问题。还是谢谢
  • 我猜你正在下载这些图像并存储在文件系统上。如果是这样,您应该为每个文件都有 Uris。如果这样做,只需使用该 Uri 作为标识符而不是资源 ID。您的适配器将存储 Uris 列表并在创建视图时加载位图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 2014-07-04
相关资源
最近更新 更多