imageview上的onClickListener启动新意图 [英] onClickListener on imageview to start new intent

查看:112
本文介绍了imageview上的onClickListener启动新意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图使用onClickListener将图像从一个活动传递到一个新活动,但我无法弄清楚如何做到这一点。它是如何工作的应用程序以列表视图开始,您可以在其中选择一个项目,它会打开一个新活动,显示一些信息和下面的图像。我想能够点击图片在新活动中打开它。

So Im trying to use an onClickListener to pass an image from one activity to a new activity but I cannot figure out how to do it. How it works is the app starts with a listview where you can select an item and it opens a new activity that displays some information and an image below. I want to be able to click the image to open it in a new activity.

继承我的routeDetails.java这是我要点击的图片所在的位置

Heres my routeDetails.java this is where the image I want to click is located

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配合它

and heres the activity_route_details.xml that goes with it

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

我想要在

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

And heres the full_scren_image.xml to go with it

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


推荐答案

将您的图像用作ByteArray。这对我没有任何问题。在发件人活动的点击监听器中,执行以下操作:

Use your image as ByteArray. This worked for me without any problem. In your click listener of the sender activity do the following,

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

在您的接收器活动中执行以下操作。

And in your receiver activity do the following.

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

call getImage() in your oncreate method of your receiver activity. The context is your application context. Good luck:)

这篇关于imageview上的onClickListener启动新意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆