如何在imageview中查看下一张图片?[英] how to view next image in imageview?

本文是小编为大家收集整理的关于如何在imageview中查看下一张图片?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

嗨,我在网格视图中有图像.当我单击电网视图图像时,它在全屏中显示.如果我想要查看另一个图像,请在网格视图中返回,然后在全屏中选择另一个图像视图.我觉得它很难.所以我试图查看图片全屏视图使用它来滑动....不知道如何创建此方法请指导我并给出一些不错的代码...

这是我的工作应用程序屏幕截图:

在此处输入图像说明

在此处输入图像说明

源代码:1.ImageViewExample.java

public class ImageViewExample extends Activity {
      /** Called when the activity is first created. */
      private Cursor imagecursor, actualimagecursor;
      private int image_column_index, actual_image_column_index;
      GridView imagegrid;
      private int count;
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            init_phone_image_grid();
      }
      private void init_phone_image_grid() {
            String[] img = { MediaStore.Images.Thumbnails._ID };
            imagecursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
            image_column_index = imagecursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
            count = imagecursor.getCount();
            imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
            imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
            imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,int position, long id) {

            System.gc();
            String[] proj = { MediaStore.Images.Media.DATA };
            actualimagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, null);
                        actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                        actualimagecursor.moveToPosition(position);
                        String i = actualimagecursor.getString(actual_image_column_index);
                        System.gc();
                        Intent intent = new Intent(getApplicationContext(), ViewImage.class);
                        intent.putExtra("filename", i);
                        startActivity(intent);
                  }
            });
      }


      public class ImageAdapter extends BaseAdapter {
            private             Context mContext;
            public ImageAdapter(Context c) {
                  mContext = c;
            }
            public int getCount() {
                  return count;
            }
            public Object getItem(int position) {
                  return position;
            }
            public long getItemId(int position) {
                  return position;
            }
            public View getView(int position,View convertView,ViewGroup parent) {
                  System.gc();
                  ImageView i = new ImageView(mContext.getApplicationContext());
                  if (convertView == null) {
                        imagecursor.moveToPosition(position);
                        int id = imagecursor.getInt(image_column_index);
                        i.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+ id));
                        i.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        i.setLayoutParams(new GridView.LayoutParams(92, 92));
                  }
                  else {
                        i = (ImageView) convertView;
                  }
                  return i;
            }
      }
}
  1. ViewImage.java

    public class ViewImage extends Activity {
    private String filename;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.gc();
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inSampleSize = 2;
        filename = extras.getString("filename");
        ImageView iv = new ImageView(getApplicationContext());
        Bitmap bm = BitmapFactory.decodeFile(filename, bfo);
        iv.setImageBitmap(bm);
        setContentView(iv);
           }
         }
    

推荐答案

获取所选图像的ID后,实现此方法..

//网格视图图像的此类

package com.thumbnailview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class Activity_ThumbView extends Activity
{

    GridView gridview;


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id)
            {

                Toast.makeText(Activity_ThumbView.this, "" + position, 2).show();
                id=parent.getPositionForView(v);

                Intent i=new Intent(Activity_ThumbView.this,Activity_ImageView.class);
                i.putExtra("position", position);
                startActivity(i);
                finish();
            }
        });
    }

    public class ImageAdapter extends BaseAdapter
    {

        Context context;



        public ImageAdapter(Context c ) 
        {
            context = c ;
        }
        public int getCount() {


            return mThumbIds.length;
        }


        public Object getItem(int position) {

            return null;
        }


        public long getItemId(int position) {

            return mThumbIds[position];
        }


        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) 
            {  
                imageView = new ImageView(context);
                imageView.setLayoutParams(new GridView.LayoutParams(75, 75));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            }
            else 
            {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

    }

            public static Integer[] mThumbIds = {
                    R.drawable.a,R.drawable.icon,
                    R.drawable.b, R.drawable.s,
                    R.drawable.c, R.drawable.r,
                    R.drawable.d, R.drawable.q,
                    R.drawable.e, R.drawable.p,
                    R.drawable.f, R.drawable.o,
                    R.drawable.g, R.drawable.n,
                    R.drawable.h, R.drawable.m,
                    R.drawable.i, R.drawable.l,
                    R.drawable.j, R.drawable.k,
                    R.drawable.t,R.drawable.y,
                    R.drawable.u,R.drawable.x,
                    R.drawable.v,R.drawable.s,
                    R.drawable.cd,R.drawable.z,
                    R.drawable.bc,R.drawable.ab
            };

}

//这个翻转图像类

package com.thumbnailview;

import java.io.IOException;



import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class Activity_ImageView extends Activity
{
     ImageView thumb_imgview;
     ViewFlipper viewFlipper;
     Button b_wall;
     Button b_home;

    // Animation a,b;
     private static final int SWIPE_MIN_DISTANCE = 120;
     private static final int SWIPE_MAX_OFF_PATH = 250;
     private static final int SWIPE_THRESHOLD_VELOCITY = 200;
     private GestureDetector gestureDetector;
     View.OnTouchListener gestureListener;
     int j;

     WallpaperManager myWall;
    public void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.image);

         j=getIntent().getExtras().getInt("position");
         myWall = WallpaperManager.getInstance(getApplicationContext());

        b_wall=(Button) findViewById(R.id.button3);
        b_home=(Button) findViewById(R.id.button1);

         thumb_imgview=(ImageView) findViewById(R.id.thumb_txt_image);
        thumb_imgview.setImageResource(Activity_ThumbView.mThumbIds[j]);

         gestureDetector = new GestureDetector(new MyGestureDetector());

         b_wall.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {



                try {

                    myWall.setResource(Activity_ThumbView.mThumbIds[j]);

                } catch (IOException e) {

                e.printStackTrace();
            }


            }
        });
         b_home.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {
                Intent i=new Intent(Activity_ImageView.this,Activity_ThumbView.class);
                startActivity(i);
                finish();

            }
        });


     }

    public class MyGestureDetector extends SimpleOnGestureListener implements OnGestureListener 
    {
          public boolean onFling(MotionEvent m1, MotionEvent m2, float velocityX, float velocityY)
          {
            try
            {
                if (Math.abs(m1.getY() - m2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;

                if(m1.getX() - m2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
                {

                    if(Activity_ThumbView.mThumbIds.length>j)
                    {
                        j++;



                        thumb_imgview.setImageResource(Activity_ThumbView.mThumbIds[j]);

                    }


                } 

                else if (m2.getX() - m1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
                {

                    if(j>0)
                    {
                        j--;

                        thumb_imgview.setImageResource(Activity_ThumbView.mThumbIds[j]);

                    }


                }
            } 
            catch (Exception e)
            {

            }
            return false;
        }
    }

             public boolean onTouchEvent(MotionEvent event)
             {
                 if (gestureDetector.onTouchEvent(event))
                     return true;
                 else
                     return false;
             }
         }

本文地址:https://www.itbaoku.cn/post/102345.html

问题描述

Hi I have images in grid view. when i click my grid view images, it's display in full screen. if i want view another image go back in grid view then select another image view in full screen. i feel it's hard one.. so i am trying to view images full screen view using to swipe....no idea how to create this method please guide me and give some nice codes also...

This is my working app screen shot:

enter image description here

enter image description here

source code: 1. ImageViewExample.java

public class ImageViewExample extends Activity {
      /** Called when the activity is first created. */
      private Cursor imagecursor, actualimagecursor;
      private int image_column_index, actual_image_column_index;
      GridView imagegrid;
      private int count;
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            init_phone_image_grid();
      }
      private void init_phone_image_grid() {
            String[] img = { MediaStore.Images.Thumbnails._ID };
            imagecursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
            image_column_index = imagecursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
            count = imagecursor.getCount();
            imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
            imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
            imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,int position, long id) {

            System.gc();
            String[] proj = { MediaStore.Images.Media.DATA };
            actualimagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, null);
                        actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                        actualimagecursor.moveToPosition(position);
                        String i = actualimagecursor.getString(actual_image_column_index);
                        System.gc();
                        Intent intent = new Intent(getApplicationContext(), ViewImage.class);
                        intent.putExtra("filename", i);
                        startActivity(intent);
                  }
            });
      }


      public class ImageAdapter extends BaseAdapter {
            private             Context mContext;
            public ImageAdapter(Context c) {
                  mContext = c;
            }
            public int getCount() {
                  return count;
            }
            public Object getItem(int position) {
                  return position;
            }
            public long getItemId(int position) {
                  return position;
            }
            public View getView(int position,View convertView,ViewGroup parent) {
                  System.gc();
                  ImageView i = new ImageView(mContext.getApplicationContext());
                  if (convertView == null) {
                        imagecursor.moveToPosition(position);
                        int id = imagecursor.getInt(image_column_index);
                        i.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+ id));
                        i.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        i.setLayoutParams(new GridView.LayoutParams(92, 92));
                  }
                  else {
                        i = (ImageView) convertView;
                  }
                  return i;
            }
      }
}
  1. ViewImage.java

    public class ViewImage extends Activity {
    private String filename;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.gc();
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inSampleSize = 2;
        filename = extras.getString("filename");
        ImageView iv = new ImageView(getApplicationContext());
        Bitmap bm = BitmapFactory.decodeFile(filename, bfo);
        iv.setImageBitmap(bm);
        setContentView(iv);
           }
         }
    

推荐答案

After getting id of selected image, implement this..

// this Class For Grid view images

package com.thumbnailview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class Activity_ThumbView extends Activity
{

    GridView gridview;


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id)
            {

                Toast.makeText(Activity_ThumbView.this, "" + position, 2).show();
                id=parent.getPositionForView(v);

                Intent i=new Intent(Activity_ThumbView.this,Activity_ImageView.class);
                i.putExtra("position", position);
                startActivity(i);
                finish();
            }
        });
    }

    public class ImageAdapter extends BaseAdapter
    {

        Context context;



        public ImageAdapter(Context c ) 
        {
            context = c ;
        }
        public int getCount() {


            return mThumbIds.length;
        }


        public Object getItem(int position) {

            return null;
        }


        public long getItemId(int position) {

            return mThumbIds[position];
        }


        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) 
            {  
                imageView = new ImageView(context);
                imageView.setLayoutParams(new GridView.LayoutParams(75, 75));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            }
            else 
            {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

    }

            public static Integer[] mThumbIds = {
                    R.drawable.a,R.drawable.icon,
                    R.drawable.b, R.drawable.s,
                    R.drawable.c, R.drawable.r,
                    R.drawable.d, R.drawable.q,
                    R.drawable.e, R.drawable.p,
                    R.drawable.f, R.drawable.o,
                    R.drawable.g, R.drawable.n,
                    R.drawable.h, R.drawable.m,
                    R.drawable.i, R.drawable.l,
                    R.drawable.j, R.drawable.k,
                    R.drawable.t,R.drawable.y,
                    R.drawable.u,R.drawable.x,
                    R.drawable.v,R.drawable.s,
                    R.drawable.cd,R.drawable.z,
                    R.drawable.bc,R.drawable.ab
            };

}

// This Flip image Class

package com.thumbnailview;

import java.io.IOException;



import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class Activity_ImageView extends Activity
{
     ImageView thumb_imgview;
     ViewFlipper viewFlipper;
     Button b_wall;
     Button b_home;

    // Animation a,b;
     private static final int SWIPE_MIN_DISTANCE = 120;
     private static final int SWIPE_MAX_OFF_PATH = 250;
     private static final int SWIPE_THRESHOLD_VELOCITY = 200;
     private GestureDetector gestureDetector;
     View.OnTouchListener gestureListener;
     int j;

     WallpaperManager myWall;
    public void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.image);

         j=getIntent().getExtras().getInt("position");
         myWall = WallpaperManager.getInstance(getApplicationContext());

        b_wall=(Button) findViewById(R.id.button3);
        b_home=(Button) findViewById(R.id.button1);

         thumb_imgview=(ImageView) findViewById(R.id.thumb_txt_image);
        thumb_imgview.setImageResource(Activity_ThumbView.mThumbIds[j]);

         gestureDetector = new GestureDetector(new MyGestureDetector());

         b_wall.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {



                try {

                    myWall.setResource(Activity_ThumbView.mThumbIds[j]);

                } catch (IOException e) {

                e.printStackTrace();
            }


            }
        });
         b_home.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {
                Intent i=new Intent(Activity_ImageView.this,Activity_ThumbView.class);
                startActivity(i);
                finish();

            }
        });


     }

    public class MyGestureDetector extends SimpleOnGestureListener implements OnGestureListener 
    {
          public boolean onFling(MotionEvent m1, MotionEvent m2, float velocityX, float velocityY)
          {
            try
            {
                if (Math.abs(m1.getY() - m2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;

                if(m1.getX() - m2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
                {

                    if(Activity_ThumbView.mThumbIds.length>j)
                    {
                        j++;



                        thumb_imgview.setImageResource(Activity_ThumbView.mThumbIds[j]);

                    }


                } 

                else if (m2.getX() - m1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
                {

                    if(j>0)
                    {
                        j--;

                        thumb_imgview.setImageResource(Activity_ThumbView.mThumbIds[j]);

                    }


                }
            } 
            catch (Exception e)
            {

            }
            return false;
        }
    }

             public boolean onTouchEvent(MotionEvent event)
             {
                 if (gestureDetector.onTouchEvent(event))
                     return true;
                 else
                     return false;
             }
         }