eclipse 单击时放大图像视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22409920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:35:19  来源:igfitidea点击:

Enlarging an Imageview on click

androideclipseandroid-imageviewandroid-image

提问by PAPERCLIPSANDPAPER

Hello Stackoverflow friendo's. I'm just wondering if someone can assist me.

你好 Stackoverflow 朋友的。我只是想知道是否有人可以帮助我。

I've created an app which holds a lot of images, the images are currently pretty small but I want them to expand once they're clicked. Is there any way of doing this? My app holds about 50 xml and the same in classes. But it holds about 100 images, is there any simple way to do this?

我创建了一个包含大量图像的应用程序,这些图像目前非常小,但我希望它们在被点击后展开。有没有办法做到这一点?我的应用程序在类中包含大约 50 个 xml 和相同的内容。但是它包含大约 100 张图像,有什么简单的方法可以做到这一点?

An example of my XML

我的 XML 的一个例子

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#808080"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="499dp"
        android:orientation="vertical" >

 <ImageView
     android:id="@+id/Logo"
     android:layout_width="120dp"
     android:layout_height="wrap_content"
     android:layout_marginLeft="255dp"
     android:layout_marginTop="3dp"
     android:src="@drawable/logov2" />


            <TextView
                android:id="@+id/title"
                android:layout_width="210dp"
                android:layout_height="84dp"
                android:layout_marginLeft="40dp"
                android:layout_marginTop="-90dp"
                android:gravity="center"
                android:text="@string/seatedcablerows"
                android:textColor="#FFFFFF"
                android:textSize="32sp" />

            <ImageView
                android:id="@+id/blackline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:src="@drawable/blackline" />

               <TextView
                   android:id="@+id/textView1"
                   android:layout_width="95dp"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="3dp"
                   android:layout_marginLeft="15dp"
                   android:text="@string/startphase"
                   android:textAppearance="?android:attr/textAppearanceSmall"
                   android:textColor="#ffffff"
                   android:textSize="16sp" />

                  <TextView
                      android:id="@+id/textView2"
                      android:layout_width="121dp"
                      android:layout_height="wrap_content"
                      android:layout_marginLeft="130dp"
                      android:layout_marginTop="-20dp"
                      android:text="@string/middlephase"
                      android:textAppearance="?android:attr/textAppearanceSmall"
                      android:textColor="#ffffff"
                      android:textSize="16sp" />

      <TextView
                   android:id="@+id/textView3"
                   android:layout_width="95dp"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="-20dp"
                   android:layout_marginLeft="260dp"
                   android:text="@string/endphase"
                   android:textAppearance="?android:attr/textAppearanceSmall"
                   android:textColor="#ffffff"
                   android:textSize="16sp" />
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="97dp"
                android:layout_height="100dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:src="@drawable/seatedcablerowstart" />

            <ImageView
                android:id="@+id/imageView2"
                 android:layout_width="97dp"
                android:layout_height="100dp"
                android:layout_marginLeft="130dp"
                android:layout_marginTop="-100dp"
                android:src="@drawable/seatedcablerowmid" />

            <ImageView
                android:id="@+id/imageView3"
                 android:layout_width="97dp"
                android:layout_height="100dp"
                android:layout_marginLeft="245dp"
                android:layout_marginTop="-100dp"
                android:src="@drawable/seatedcablerowend" />

            <ImageView
                android:id="@+id/blackline2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:src="@drawable/blackline" />

            <TextView
                android:id="@+id/seatedcablerowsdesc"
                android:layout_width="match_parent"
                android:layout_height="41dp"
                android:layout_marginTop="3dp"
                android:gravity="center"
                android:text="@string/seatedcablerowsdesc"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#ffffff"
                android:textSize="16sp" />

                 <TextView
                     android:id="@+id/seatedcablerowsperf"
                     android:layout_width="match_parent"
                     android:layout_height="238dp"
                     android:layout_marginTop="3dp"
                     android:gravity="center"
                     android:text="@string/seatedcablerowsperf"
                     android:textAppearance="?android:attr/textAppearanceSmall"
                     android:textColor="#FFFFFF"
                     android:textSize="14sp" />

                 <TextView
                     android:id="@+id/seatedcablerowscaut"
                     android:layout_width="match_parent"
                     android:layout_height="64dp"
                     android:layout_marginTop="3dp"
                     android:gravity="center"
                     android:text="@string/seatedcablerowscaut"
                     android:textAppearance="?android:attr/textAppearanceSmall"
                     android:textColor="#FFFFFF"
                     android:textSize="14sp" />

                 <ImageView
                      android:id="@+id/blackline3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:src="@drawable/blackline" />

                                                            <!--Set 1 starts here  -->
                 <TextView
                     android:id="@+id/set1"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="@string/set1"
                     android:textColor="#FFFF00"
                     android:textSize="25sp" />

                   <TextView
                     android:id="@+id/weight"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_marginTop="-30dp"
                     android:layout_marginLeft="100dp"
                     android:text="@string/weight"
                     android:textColor="#FFFF00"
                     android:textSize="16sp" 
                     />

                     <TextView
                         android:id="@+id/repititions"
                         android:layout_width="116dp"
                         android:layout_height="wrap_content"
                         android:layout_marginLeft="240dp"
                         android:layout_marginTop="-20dp"
                         android:text="@string/repetitions"
                         android:textColor="#FFFF00"
                         android:textSize="16sp" />

                 <EditText
                     android:id="@+id/editText1"
                     android:layout_width="67dp"
                     android:layout_height="36dp"
                     android:layout_marginLeft="100dp"
                     android:layout_marginTop="5dp"
                     android:background="#FFFFFF"
                     android:ems="10"
                     android:inputType="number"
                      >

              </EditText>

                 <EditText
                     android:id="@+id/editText2"
                     android:layout_width="67dp"
                     android:layout_height="36dp"
                     android:layout_marginLeft="240dp"
                     android:layout_marginTop="-37dp"
                     android:background="#FFFFFF"
                     android:ems="10"
                     android:inputType="number" >

                </EditText>
                 <ImageView
                     android:id="@+id/imageView4"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:src="@drawable/blackline2" />
                                                                <!--Set 2 starts here  -->
                   <TextView
                     android:id="@+id/set2"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="@string/set2"
                     android:textColor="#FFFF00"
                     android:textSize="25sp" 
                     />
                   <TextView
                     android:id="@+id/weight1"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_marginTop="-30dp"
                     android:layout_marginLeft="100dp"
                     android:text="@string/weight"
                     android:textColor="#FFFF00"
                     android:textSize="16sp" 
                     />
                      <TextView
                         android:id="@+id/repititions2"
                         android:layout_width="116dp"
                         android:layout_height="wrap_content"
                         android:layout_marginLeft="240dp"
                         android:layout_marginTop="-20dp"
                         android:text="@string/repetitions"
                         android:textColor="#FFFF00"
                         android:textSize="16sp" />
                            <EditText
                     android:id="@+id/editText3"
                     android:layout_width="67dp"
                     android:layout_height="36dp"
                     android:layout_marginLeft="100dp"
                     android:layout_marginTop="5dp"
                     android:background="#FFFFFF"
                     android:ems="10"
                     android:inputType="number" >
                     </EditText>
                        <EditText
                     android:id="@+id/editText4"
                     android:layout_width="67dp"
                     android:layout_height="36dp"
                     android:layout_marginLeft="240dp"
                     android:layout_marginTop="-37dp"
                     android:background="#FFFFFF"
                     android:ems="10"
                     android:inputType="number" >

                </EditText>

                   <ImageView
                       android:id="@+id/imageView5"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:src="@drawable/blackline2" />
                                                                        <!--Set 3 starts here  -->
                     <TextView
                     android:id="@+id/set3"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="@string/set3"
                     android:textColor="#FFFF00"
                     android:textSize="25sp" 
                     />
                   <TextView
                     android:id="@+id/weight3"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_marginTop="-30dp"
                     android:layout_marginLeft="100dp"
                     android:text="@string/weight"
                     android:textColor="#FFFF00"
                     android:textSize="16sp" 
                     />
                      <TextView
                         android:id="@+id/repititions3"
                         android:layout_width="116dp"
                         android:layout_height="wrap_content"
                         android:layout_marginLeft="240dp"
                         android:layout_marginTop="-20dp"
                         android:text="@string/repetitions"
                         android:textColor="#FFFF00"
                         android:textSize="16sp" />
                            <EditText
                     android:id="@+id/editText5"
                     android:layout_width="67dp"
                     android:layout_height="36dp"
                     android:layout_marginLeft="100dp"
                     android:layout_marginTop="5dp"
                     android:background="#FFFFFF"
                     android:ems="10"
                     android:inputType="number" >
                     </EditText>
                        <EditText
                     android:id="@+id/editText6"
                     android:layout_width="67dp"
                     android:layout_height="36dp"
                     android:layout_marginLeft="240dp"
                     android:layout_marginTop="-37dp"
                     android:background="#FFFFFF"
                     android:ems="10"
                     android:inputType="number" >
 </EditText>
                     <ImageView
                         android:id="@+id/imageView6"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:src="@drawable/blackline2" />
                                                                     <!--Set 4 starts here  -->

                          <TextView
                     android:id="@+id/set4"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="@string/set4"
                     android:textColor="#FFFF00"
                     android:textSize="25sp" 
                     />
                   <TextView
                     android:id="@+id/weight4"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_marginTop="-30dp"
                     android:layout_marginLeft="100dp"
                     android:text="@string/weight"
                     android:textColor="#FFFF00"
                     android:textSize="16sp" 
                     />
                      <TextView
                         android:id="@+id/repititions4"
                         android:layout_width="116dp"
                         android:layout_height="wrap_content"
                         android:layout_marginLeft="240dp"
                         android:layout_marginTop="-20dp"
                         android:text="@string/repetitions"
                         android:textColor="#FFFF00"
                         android:textSize="16sp" />
                            <EditText
                     android:id="@+id/editText7"
                     android:layout_width="67dp"
                     android:layout_height="36dp"
                     android:layout_marginLeft="100dp"
                     android:layout_marginTop="5dp"
                     android:background="#FFFFFF"
                     android:ems="10"
                     android:inputType="number" >
                     </EditText>
                        <EditText
                     android:id="@+id/editText8"
                     android:layout_width="67dp"
                     android:layout_height="36dp"
                     android:layout_marginLeft="240dp"
                     android:layout_marginTop="-37dp"
                     android:background="#FFFFFF"
                     android:ems="10"
                     android:inputType="number" >
 </EditText>

                                  <ImageButton
                            android:id="@+id/imageButton1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="240dp"
                            android:layout_marginTop="35dp"
                            android:src="@drawable/savebutton"
                             android:background="@null" />

                        <ImageButton
                            android:id="@+id/QR"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="-10dp"
                            android:layout_marginBottom="-60dp"
                            android:src="@drawable/qrscanner" />

</LinearLayout>
</ScrollView>

Java class:

Java类:

package com.example.workplease;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.example.workplease_v2.R;

public class Seatedcablerows extends Activity {
ImageButton imagebutton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seatedcablerows);

        AddListenerOnButton();
}

public void AddListenerOnButton(){
    final Context context = this;

    imagebutton = (ImageButton) findViewById(R.id.QR);

    imagebutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, Qrscanner.class);
                        startActivity(intent);  
        }
    });
}
}

回答by Pratap Singh

Google has nicely explained How to enlarge view on a touch. You can use same technique for your image view also. Check the given link for full example code.

谷歌很好地解释了如何在触摸时放大视图。您也可以对图像视图使用相同的技术。检查给定的链接以获取完整的示例代码。

回答by Eenvincible

Assuming you have a fragment with a gridview that holds your images, just attach an onItemClickListener to them and when someone clicks the image, get the ID and pass it back to the activity which will get the image from where it is located using the id and set up a new fragment with a single ImageView; that will do it.

假设您有一个带有 gridview 的片段,可以保存您的图像,只需将 onItemClickListener 附加到它们,当有人单击图像时,获取 ID 并将其传回活动,活动将使用 id 和使用单个 ImageView 设置一个新片段;那会做到的。

EDIT

编辑

After reading your question a second time, I think you might have to change the way you are doing it (up to you really)

第二次阅读你的问题后,我认为你可能需要改变你的做法(真的取决于你)

Have two arrays to store the smaller images and the larger versions of the images respectively.

有两个数组分别存储较小的图像和较大版本的图像。

Now, use a fragment or just a regular view with a gridview in it to display all your images and then implement an a click listener to keep track of which image was clicked using the position argument.

现在,使用一个片段或只是一个带有 gridview 的常规视图来显示所有图像,然后实现一个单击侦听器来跟踪使用位置参数单击了哪个图像。

After knowing which image was clicked, just pass back the position to the activity and use that position to load the bigger image from the second array of larger images and display it in an imageview with the preset dimensions/ width/height respectively.

知道点击了哪个图像后,只需将位置传回活动并使用该位置从较大图像的第二个数组中加载较大的图像,并分别以预设的尺寸/宽度/高度将其显示在图像视图中。

This is what I think will help solve your problem.

我认为这将有助于解决您的问题。

NOTEIn order to pass back the position of the clicked image, you just need an interface in a fragment if you use a fragment (then implement it in your activity), otherwise, you can just get the position from within the activity and do your next task there.

注意为了传回点击图像的位置,如果您使用片段(然后在您的活动中实现它),您只需要一个片段中的接口,否则,您可以从活动中获取位置并执行您的下一个任务。