2016-11-15 57 views
0

因此,我試圖使用onClickListener將圖像從一個活動傳遞給一個新的活動,但我無法弄清楚如何去做。它的工作原理是,應用程序以列表視圖開始,您可以選擇一個項目,並打開一個新的活動,顯示下面的一些信息和圖像。我希望能夠點擊圖片在新的活動中打開它。onClickListener on imageview開始新的意圖

我的繼承人routeDetails.java這是我想點擊圖像位於

package com.example.zach.listview; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.TextView; 

import static com.example.zach.listview.R.id.image; 


public class RouteDetails extends AppCompatActivity { 

TouchImageView routeImage; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_route_details); 

    //back button for route details view 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    //TextView for route details 
    final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView); 
    routeDetailsView.setText(getIntent().getExtras().getString("route")); 

    //ImageView for route details 
    routeImage = (TouchImageView) findViewById(R.id.routeImage); 
    routeImage.setImageResource(getIntent().getIntExtra("imageResourceId", 0)); 


////////// Trying to pass image to new activity 

    routeImage.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) 
     { 
      int imageId = (int) image.getResourceId(i, -1); 

      Intent intent = new Intent(v.getContext(),  FullScreenImage.class); 
      intent.putExtra("imageResourceId", imageId); 
      startActivity(intent); 
     } 

    }); 

//////////// 

} 

//back button for route details view 
@Override 
public boolean onSupportNavigateUp(){ 
    finish(); 
    return true; 
} 

} 

和繼承人與它

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scrollView1" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_route_details" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.zach.listview.RouteDetails"> 



<TextView 
    android:text="TextView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/routeDetailsView" 
    android:textSize="18sp" 
    android:textAlignment="center" /> 

<com.example.zach.listview.TouchImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/routeImage" 
    android:scaleType="fitCenter" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:adjustViewBounds="false" 
    android:layout_below="@+id/routeDetailsView"/> 

</RelativeLayout> 
</ScrollView> 

我的繼承人FullScreenImage.java那張activity_route_details.xml那我想要打開圖像

package com.example.zach.listview; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 

/** 
* Created by Zach on 11/15/2016. 
*/ 

public class FullScreenImage extends AppCompatActivity { 

TouchImageView routeImage; 

@Override 
protected void onCreate(Bundle saveInstanceState){ 
    super.onCreate(saveInstanceState); 
    setContentView(R.layout.full_screen_image); 

    routeImage = (TouchImageView) findViewById(R.id.fullScreenImageView); 
    routeImage.setImageResource(getIntent().getIntExtra("imageFullScreen",  0)); 
} 



} 

繼承人full_scren_image.x毫升去用它

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

<com.example.zach.listview.TouchImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:srcCompat="@+id/routeImage" 
    android:id="@+id/fullScreenImageView" 
    android:layout_weight="1" /> 
</LinearLayout> 

回答

0

你在額外投入與關鍵imageResourceId但你試圖把它拉出來用鑰匙imageFullScreen。嘗試在兩個地方使用相同的密鑰。

+0

我改變了這一點,它仍然不起作用 – user4297729

0

將您的圖片用作ByteArray。這對我沒有任何問題。在發件人活動的點擊收聽者中,執行以下操作:

Intent senderint = new Intent(context, receiveractivity.class); 
Bitmap bitmap = drawableToBitmap(view.getBackground()); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
byte[] b = baos.toByteArray(); 
senderint.putExtra("myimage", b); 
startActivity(senderint); 

並在接收者活動中執行以下操作。

private void getImage(){ 
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View v = null; 
v = inflater.inflate(R.layout.receiverlayout, null); 

ViewGroup vg = (ViewGroup) findViewById(R.id.yourmainlayout); 
Bundle bundle = getIntent().getExtras(); 
byte[] ba = bundle.getByteArray("myimage"); 
Bitmap bm = BitmapFactory.decodeByteArray(ba, 0, ba.length()); 
Drawable dr = new BitmapDrawable(getResources, bm); 
vg.setBackground(dr); 
vg.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
} 

在您的接收器活動的oncreate方法中調用getImage()。上下文是您的應用程序上下文。祝你好運:)

+0

謝謝有更簡單的方式來傳遞圖像比那更?例如使用putextra如何將圖像從我的主要活動傳遞到路線詳情。我可以做同樣的事情從路線細節傳遞到全屏圖像? – user4297729

+0

我想不出一個更簡單的方法。我在這裏以byte []形式傳遞圖像。但是,如果不添加新佈局,只需獲取附加信息並將其轉換爲位圖,然後將其用作背景或您需要執行的任何操作以滿足您的需求。是的,您也可以通過切換髮件人和收件人活動來放置額外信息並獲得額外收入。我希望我明確自己。 –