【问题标题】: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?我有一个图像的网格视图。我从中选择一个图像并在 activity2.xml 中显示它。现在我想拖动这个图像视图。如何做到这一点?
【发布时间】:2015-09-25 11:52:30
【问题描述】:

我有一个 grid.class,我从中选择一张图像并将其显示在 activity_main.xml 中。 每当我选择一张图片时,它的 id 都会是“imageChosen”。 我可以拖动从可绘制文件夹中获取的任何图像,但不能拖动带有图像 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>

【问题讨论】:

    标签: android xml image android-layout


    【解决方案1】:

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

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

    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]);
    

    【讨论】:

      猜你喜欢
      • 2014-03-08
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多