【问题标题】:how to send a image to next screen on button click如何在按钮单击时将图像发送到下一个屏幕
【发布时间】:2014-06-19 11:05:59
【问题描述】:

我创建了一个网格视图应用程序,在其中我在 swipe_view 屏幕上滑动图像。

现在我想将 swipe_screen 上显示的图像发送到新屏幕。

我想要的是图像在 swipe_view 屏幕上是当前的我想在按钮单击时将该图像发送到新屏幕。

查看我的代码。

MainActivity.java

package com.td.gridview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {


    private GridView photoGrid;
    private int mPhotoSize, mPhotoSpacing;
    private ImageAdapter imageAdapter;

    // Some items to add to the GRID
    private static final String[] CONTENT = new String[] 
            { 
            "Akon", "Justin Bieber", "AlRight", "Big Sean",
            "Britney Spears", "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean",
            "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
            "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
            "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
            "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears",
            "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary",
            "Micheal Buble" 
            };
    static final int[] ICONS = new int[] 
            { 
            R.drawable.cover_akon, R.drawable.cover_justin,
            R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
            R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
            R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
            R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
            R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
            R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin,
            R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
            R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
            R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
            R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
            R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
            R.drawable.cover_hilary, R.drawable.cover_mb 
            };

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


        // get the photo size and spacing
        mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
        mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);

        // initialize image adapter
        imageAdapter = new ImageAdapter();

        photoGrid = (GridView) findViewById(R.id.albumGrid);

        //start sent image to full screen             

        /**
         * On Click event for Single Gridview Item
         * */
        photoGrid.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,  View v,
                    int position,   long id ) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), SwipeActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);

            }            
        });
        //end sent image to full screen

        // set image adapter to the GridView
        photoGrid.setAdapter(imageAdapter);

        // get the view tree observer of the grid and set the height and numcols dynamically
        photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (imageAdapter.getNumColumns() == 0) {
                    final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                    if (numColumns > 0) {
                        final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                        imageAdapter.setNumColumns(numColumns);
                        imageAdapter.setItemHeight(columnWidth);

                    }
                }
            }
        });     
    }

    // ///////// ImageAdapter class /////////////////
    public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private int mItemHeight = 0;
        private int mNumColumns = 0;
        private RelativeLayout.LayoutParams mImageViewLayoutParams;

        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
        }

        public int getCount() {
            return CONTENT.length;
        }

        // set numcols
        public void setNumColumns(int numColumns) {
            mNumColumns = numColumns;
        }

        public int getNumColumns() {
            return mNumColumns;
        }

        // set photo item height
        public void setItemHeight(int height) {
            if (height == mItemHeight) {
                return;
            }
            mItemHeight = height;
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
            notifyDataSetChanged();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View view, ViewGroup parent) {

            if (view == null)
                view = mInflater.inflate(R.layout.photo_item, null);

            ImageView cover = (ImageView) view.findViewById(R.id.cover);
            TextView title = (TextView) view.findViewById(R.id.title);

            cover.setLayoutParams(mImageViewLayoutParams);

            // Check the height matches our calculated column width
            if (cover.getLayoutParams().height != mItemHeight) {
                cover.setLayoutParams(mImageViewLayoutParams);
            }

            cover.setImageResource(ICONS[position % ICONS.length]);
            title.setText(CONTENT[position % CONTENT.length]);

            return view;
        }
    }

}

//swipe image class

SwipeActivity.java

package com.td.gridview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

public class SwipeActivity extends Activity 
{           

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.swipe_view);        

 // get intent data
    Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImagePagerAdapter adapter = new ImagePagerAdapter();
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(position);

    //send image to full_view.xml screen on button click
    Button b = (Button)findViewById(R.id.xoom);
    b.setOnClickListener(new View.OnClickListener() 
    {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            // how to sent current showing image to new screen

        }

    });
    //

    //send data to grid view on button click
    Button b1 = (Button)findViewById(R.id.grid);
    b1.setOnClickListener(new View.OnClickListener() 
    {

        public void onClick(View v2) 
        {
//          // TODO Auto-generated method stub

            Intent i4 = new Intent(getApplicationContext(), MainActivity.class);
            // passing array index            
            startActivity(i4);
        }
    });
  }  

  private class ImagePagerAdapter extends PagerAdapter 
  {
      int[] icons = MainActivity.ICONS;    

    @Override
    public int getCount() 
    {
      return icons.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) 
    {
      return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) 
    {
      Context context = SwipeActivity.this;
      ImageView imageView = new ImageView(context);
//      int padding = context.getResources().getDimensionPixelSize(
//          R.dimen.padding_large);
//      imageView.setPadding(padding, padding, padding, padding);
      imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
      imageView.setImageResource(icons[position]);
      ((ViewPager) container).addView(imageView, 0);
      return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) 
    {
      ((ViewPager) container).removeView((ImageView) object);
    }
  }
}


//swipe image layout

swipe_view.xml

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

     <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:weightSum="3" >
        <Button
            android:id="@+id/grid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Grid" />
        <Button
            android:id="@+id/xoom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Zoom"/>
        <Button
            android:id="@+id/wll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Wallpaper" />
    </LinearLayout>
</RelativeLayout>

下面是我想从 swipe_view 屏幕获取图像的类和 xml

Full_Zoom.java

package com.td.gridview;

import android.os.Bundle;
import android.app.Activity;

public class Full_Zoom extends Activity {

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



}
}

full_view.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_full"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

_________________________________________________________________________________

这是我尝试过的,但在尝试后会发生什么是从选定的网格图像而不是从 swipe_view 屏幕获取位置。我想从 swipe_view 屏幕而不是从选定的网格图像中获取和发送图像。

因为我想在新屏幕中缩放当前的滑动图像。

但是因为这段代码

// get intent data
            Intent i3 = getIntent();

            // Selected image id
            int position = i3.getExtras().getInt("id");
            // Sending image id to FullScreenActivity
            Intent i2 = new Intent(getApplicationContext(), Full_Zoom.class);
            // passing array index
            i2.putExtra(null, R.id.view_pager);
            startActivity(i2);

它从选定的网格视图中获取图像,但我想从当前显示的滑动图像中获取图像。

SwipeActivity.java

//send image to full_view.xml screen on button click
    Button b = (Button)findViewById(R.id.xoom);
    b.setOnClickListener(new View.OnClickListener() 
    {

        public void onClick(View v) {
//          // TODO Auto-generated method stub

            // get intent data
            Intent i3 = getIntent();

            // Selected image id
            int position = i3.getExtras().getInt("id");
            // Sending image id to FullScreenActivity
            Intent i2 = new Intent(getApplicationContext(), Full_Zoom.class);
            // passing array index
            i2.putExtra(null, R.id.view_pager);
            startActivity(i2);
        }
    });
    //

Full_Zoom.java(新屏幕类)

public class Full_Zoom extends Activity {

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

     // get intent data
        Intent i = getIntent();

        // Selected image id
        int position = i.getExtras().getInt("id");
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

        PinchZoom img = new PinchZoom(this);
        img.setImageResource(MainActivity.ICONS[R.id.view_pager]);
        img.setMaxZoom(4f);
        setContentView(img);

}
}

【问题讨论】:

标签: java android xml navigation buttonclick


【解决方案1】:

在点击内发送图片:

  Intent i = new Intent(this,YourClassName.class);
    i.putExtra("imageID", icons[position]);
    startActivity(i);

在新活动中获取此信息:

Bundle bd = getIntent().getExtras();
int imageRes = bd.getInt("imageID");
ImageView image = (ImageView) findViewById(R.id.imageview);
image.setImageResource(imageRes);

【讨论】:

  • 如果图像大小很大,则首先将图像转换为字节数组,然后传入 Intent,在下一个活动中从 Bundle 中获取字节数组并转换为图像(位图)并设置为 ImageView。
  • 如何将文件存储在 SD 卡中并在下一个活动中检索它?
  • 请在 s.o 上提出一个新问题,我很乐意为您提供帮助。因为在评论中给出答案并非不可能。
  • 没关系,我不需要答案。我只是问你。
  • 等我上传我的代码,你试试看,你就会明白哪里错了
猜你喜欢
  • 2018-02-11
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
相关资源
最近更新 更多