簡體   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