繁体   English   中英

imageview上的onClickListener开始新的意图

[英]onClickListener on imageview to start new intent

因此,我试图使用onClickListener将图像从一个活动传递到新活动,但我不知道该怎么做。 该应用程序的工作方式是从一个列表视图开始,您可以在其中选择一个项目,然后打开一个新活动,该活动在下面显示一些信息和一张图片。 我希望能够单击该图像以在新活动中将其打开。

这是我的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;
}

}

以及随之而来的activity_route_details.xml

<?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>

我要在其中打开图像的Heres FullScreenImage.java

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.xml

<?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>

您将使用键imageResourceId但尝试使用键imageFullScreen来将其拉出。 尝试在两个地方使用相同的密钥。

将图像用作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()。 上下文是您的应用程序上下文。 祝好运:)

暂无
暂无

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

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