【问题标题】:how to display fullscreen image by clicking imagebutton如何通过单击图像按钮显示全屏图像
【发布时间】:2023-03-14 22:58:02
【问题描述】:

我的布局中只有两个图像按钮,其中包含可绘制文件夹中的两张图片。我想要的是当用户单击图像按钮时,他应该看到全屏图像并且也应该能够缩放。

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >


<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/piq1" 
android:layout_weight="1"
/>

<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/piq2" 
android:layout_weight="1"/>

</LinearLayout>

当点击图像按钮时,它应该全屏显示完整图像,并且用户应该能够对其进行缩放。请帮我写代码。

我的 java 看起来像:

import android.app.Activity;
import android.os.Bundle;

public class Piq extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.piq);
}

【问题讨论】:

  • 其余代码在哪里??
  • 什么代码?你还需要什么?
  • 我不知道如何显示 imagebutton 的 onclick 监听器,所以它显示全屏图像
  • 我认为您在代码中进行了尝试并为我们展示了它
  • 好的,当用户按下图像按钮时,您可以删除图像 src 并将布局背景与所选按钮图像放在一起,在这种情况下它将占据整个屏幕

标签: android imagebutton


【解决方案1】:

首先,它只有一个你可以做的想法,它需要你用这段代码来处理你的情况,希望它对你有帮助,

这里是使用完整图像和缩放功能的教程

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

这里的布局将是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@android:drawable/btn_radio" />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

这里是你将使用的代码

    final ImageButton img1 = (ImageButton) findViewById(R.id.imageButton1);
    final ImageButton img2 = (ImageButton) findViewById(R.id.imageButton2);

    final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);

    img1.setOnClickListener(new OnClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            layout.setBackground(img1.getDrawable());

        }
    });

    img2.setOnClickListener(new OnClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            layout.setBackground(img2.getDrawable());
        }
    });

【讨论】:

  • 这会全屏显示图像,但图像按钮也是可见的。我也不能缩放。我知道从哪里开始。但我真正想要的是像画廊照片中显示的那样显示图片。
  • 你想怎么缩放??? ,如果你想做一些类似画廊的东西,为什么需要制作图像按钮而不是实现画廊??
  • 这可能吗?仅显示 2 张图像,就像画廊中显示的一样。缩放应该像捏缩放。也双击(放大)并再次双击(恢复正常)
  • 是的,我明白了,您需要使用手势来缩放图像,我将在我编辑的答案中为您提供示例
  • 我之前看过那个教程.. 看起来有点复杂。我只需要 2 张图片。我必须为 2 张图片制作整个代码吗?
【解决方案2】:

这段代码成功了。

How to Zoom in/Out an ImageView(not using Canvas) in android

仍然需要进行一些调整,例如设置默认视图以及设置最大和最小缩放限制。但我现在可以自己做。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    相关资源
    最近更新 更多