android – 获取画廊从特定位置开始

android – 获取画廊从特定位置开始,第1张

概述我正在为我的应用创建一个图库部分,用于显示X个图像.这些图像是从外部服务器加载的.向用户显示包含图像缩略图的GridView,点击这些缩略图后,将打开图库视图,以全屏模式显示图像. ArrayList用于保存包含指向这些图像的链接的String对象.在我的Adapter的getView方法中,这个ArrayList用于查看需要显示的图像. 到目前为止这个工作正常,但是当用户选择不同的图像然后是Gr 我正在为我的应用创建一个图库部分,用于显示X个图像.这些图像是从外部服务器加载的.向用户显示包含图像缩略图的GrIDVIEw,点击这些缩略图后,将打开图库视图,以全屏模式显示图像.
ArrayList用于保存包含指向这些图像的链接的String对象.在我的Adapter的getVIEw方法中,这个ArrayList用于查看需要显示的图像.

到目前为止这个工作正常,但是当用户选择不同的图像然后是GrIDVIEw中的第一个图像时,我遇到了一个问题.例如,当用户单击第7个图像时,图库将显示第7个图像.但是当用户向右翻转以查看之前的图像(第6张图像以及之前的图像)时,没有任何反应.当他向左边看到即将到来的图像时,用户会看到列表中的第2项,然后是第3项,然后是第4项等.

显然,画廊认为无论我首先提供的是什么图像,实际上是列表中的第一个项目,并且开始显示之后的所有内容,如果图像实际上是第一个,它应该具有.但是,gallery应该将图像显示为GrIDVIEw上的实际位置.我怎样才能实现这样的目标?我已经尝试了解单击GrIDVIEw时获得的位置,但这只会导致我上面描述的行为.

这是我用于此的代码:

PhotoFullVIEw(图库的库和适配器)

package com.mobowski.appfrag.Json.pictures;import java.io.BufferedinputStream;import java.io.inputStream;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import com.mobowski.appfrag.R;import androID.app.Activity;import androID.content.Context;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.os.Bundle;import androID.vIEw.display;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.BaseAdapter;import androID.Widget.gallery;import androID.Widget.ImageVIEw;public class PhotoFullVIEw extends Activity {    /** Called when the activity is first created. */    Customgallery gallery;    public ArrayList<String> links;    private int pos;    @OverrIDe    public voID onCreate(Bundle icicle) {        super.onCreate(icicle);        setContentVIEw(R.layout.full_photo_gallery);        Bundle bun = getIntent().getExtras();        links = bun.getStringArrayList("links");        pos = bun.getInt("pos");        /*         * Find the gallery defined in the main.xml Apply a new (custom)         * ImageAdapter to it.         */        gallery = (Customgallery) findVIEwByID(R.ID.gallery);        gallery.setAdapter(new ImageAdapter(this,links,pos));        gallery.setSpacing(25);    }    public class ImageAdapter extends BaseAdapter {        /** The parent context */        private Context myContext;        private ArrayList<String> links;        int pos;        display display = getwindowManager().getDefaultdisplay();        int wIDth = display.getWIDth();        int height = display.getHeight();        /**         * All images to be displayed. Put some images to project-folder:         * '/res/drawable/uvw.xyz' .         */        /** Simple Constructor saving the 'parent' context. */        public ImageAdapter(Context c,ArrayList<String> links,int pos) {            this.myContext = c;            this.links = links;            this.pos = pos;        }        /** Returns the amount of images we have defined. */        public int getCount() {            return this.links.size();        }        /* Use the array-positions as unique IDs */        public Object getItem(int position) {            return position;        }        public long getItemID(int position) {            return position;        }        /**         * Returns a new ImageVIEw to be displayed,depending on the position         * passed.         */        public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {            ImageVIEw i = new ImageVIEw(this.myContext);            String imageURL = links.get(position);            if (position == 0) {                imageURL = links.get(pos);            }            try {                URL aURL = new URL(imageURL);                URLConnection conn = aURL.openConnection();                conn.connect();                inputStream is = conn.getinputStream();                /* Buffered is always good for a performance plus. */                BufferedinputStream bis = new BufferedinputStream(is);                /* Decode url-data to a bitmap. */                Bitmap bm = BitmapFactory.decodeStream(bis);                bis.close();                is.close();                /* Apply the Bitmap to the ImageVIEw that will be returned. */                i.setimageBitmap(bm);            } catch (Exception e) {                e.printstacktrace();            }            /* Image should be scaled as wIDth/height are set. */            // i.setScaleType(ImageVIEw.ScaleType.FIT_XY);            // /* Set the WIDth/Height of the ImageVIEw. */            // i.setLayoutParams(new gallery.LayoutParams());            return i;        }    }}

自定义库类(用于重载onFling,因此它一次只显示1个图像)

package com.mobowski.appfrag.Json.pictures;import androID.content.Context;import androID.util.AttributeSet;import androID.vIEw.MotionEvent;import androID.Widget.gallery;public class Customgallery extends gallery{    public Customgallery(Context context,AttributeSet attrs) {        super(context,attrs);        // Todo auto-generated constructor stub    }    @OverrIDe    public boolean onFling(MotionEvent e1,MotionEvent e2,float veLocityX,float veLocityY) {      return super.onFling(e1,e2,veLocityY);    }}

我刚刚设法在使用后选择自定义起始图像后进一步滚动时如何正确保持位置

String imageURL = links.get(pos+position);

其中pos是在GrIDVIEw中单击的图像的位置.但是,这仍然没有让我有机会在单击图像之前滚动回图像.

嗯,这比我想象的容易.
在再次阅读文档后,我发现我可以在gallery对象上使用setSelection(position)从特定位置开始.如果只有所有问题都很容易解决.谢谢你的阅读!

解决方法 嗯,这比我想象的容易. 在再次阅读文档后,我发现我可以在gallery对象上使用setSelection(position)从特定位置开始.如果只有所有问题都很容易解决.谢谢你的阅读! 总结

以上是内存溢出为你收集整理的android – 获取画廊从特定位置开始全部内容,希望文章能够帮你解决android – 获取画廊从特定位置开始所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1129673.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存