繁体   English   中英

我有一个图像的网格视图。我正在从中选择一个图像并在 activity2.xml 中显示它。现在我想拖动这个图像视图。如何做到这一点?

[英]I have a grid view of images.I am selecting one image from it and showing it in activity2.xml.Now I want to drag this image view .How to do this?

我有一个 grid.class,我从中选择了一张图片并将其显示在 activity_main.xml 中。 每当我选择一个图像时,它的 id 将是“imageChosen”。 我可以拖动从 drawable 文件夹中获取的任何图像,但不能拖动带有图像 ID 的动态选择的图像。

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_vertical_margin" >
<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/image1"
    android:src="@mipmap/ic_launcher"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
   </RelativeLayout>

我不太明白你在说什么,但假设你有一个带有一些图像的网格适配器,所以你选择一个并希望该图像显示在主要活动中; 你只需要这个:

1) 创建一个包含所有图像 refs 的数组:

public class Images{
    // references to our images
    private Integer[] mIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
} 

2) 在您的项目上单击网格视图,将索引发送到 displayActivity:

 gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Intent mInDisplay= new Intent(MainActivity.this, DisplayActivity.class);
                mInDisplay.putExtra("Index", position);
                startActivity(mInDisplay);
            }
        });

3)并在 DisplayActivity 中显示 img :

 //onCreate()
        Bundle bundle=getIntent().getExtras();
                int index=bundle.getInt("Index");      
                ImageView mImage = (ImageView) findViewById(R.id.Img);
                mImage.setImageResource(Images.mIds[index]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM