使转盘与ViewFlipper或ViewPager转盘、ViewFlipper、ViewPager

2023-09-04 09:41:19 作者:无敌呆萌帅

由于 GalleryView 德precated我们应该移民到一些替代部件,在我的情况 ViewFlipper 是最好的,但我也面临着几个问题,你可以在下面的截图我设计了一个旋转木马看到 ImageGallery GalleryView

Since GalleryView deprecated we should immigrate to some alternative widgets, In my case ViewFlipper is the best but I have faced with several issues, as you can see in the following screenshot I have designed a carousel ImageGallery with GalleryView:

使用 ViewFlipper 一切正常如我所料,但我没能实现两件事情:

With ViewFlipper everything works as I expected, But I'm not able to implement two things:

1 ViewFlipper 总是显示一个项目;然而,我需要一次展示三个项目(甚至更多)。的

1- ViewFlipper always shows one item; however I need to display three items (or even more) at once.

2 - ViewFlipper 是不可触及的部件,这不是我想要的!的

2- ViewFlipper is non-touchable widget and it's not what I want!

作为FlávioFaria下面的帖子中提到关于 ViewPager ,这是一个伟大的情况太多,但我不能把我的规模达的动画吧!

As FlávioFaria mentioned about ViewPager in the following post, It's a great case too but I can't pass my scale up animation to it!

我所做的一切与 ViewPager ,现在它的伟大的工作,但我已经错过了的功能,那就是无限滚动的!

I've done everything with ViewPager, now it's working great but I have missed one functionality and that is infinity scrolling!

添加我的 PagerAdapter

Added my PagerAdapter class

public class CarouselAdapter extends PagerAdapter  
{  
    private Context mContext;
    private ImageLoader imageLoader;
    private String[] bannerUri;

    public CarouselAdapter (Context c, String[] bannerArray) 
    { 
        this.mContext = c; 
        this.bannerUri = bannerArray;
        // Setup image loader
        this.imageLoader = ImageLoader.getInstance();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(c)
            .threadPoolSize(2) 
            .memoryCache(new WeakMemoryCache())
            .discCacheFileNameGenerator(new Md5FileNameGenerator())
            .build();
        this.imageLoader.init(config);
    } 

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        if (position >= bannerUri.length) 
            position %= bannerUri.length;

        ImageView i = new ImageView(mContext);
        displayImage(i, bannerUri[position]);
        i.setScaleType(ScaleType.FIT_XY); 
        container.addView(i);
        return i;
    }

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

    @Override
    public int getCount() 
    {
        return Integer.MAX_VALUE;
    }

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

    private void displayImage(final ImageView mImage, String ImageUri)
    {
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.border)
            .showImageForEmptyUri(R.drawable.border)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .resetViewBeforeLoading()
            .cacheOnDisc() 
            .displayer(new FadeInBitmapDisplayer(740))
            .build();

        imageLoader.loadImage(ImageUri, defaultOptions, new SimpleImageLoadingListener() 
        {
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
            {
                mImage.setImageDrawable(new BitmapDrawable(mContext.getResources()
                        , getDesiredBitmap(loadedImage, 12)));
            }
        });
    }

    private Bitmap getDesiredBitmap(Bitmap originalImage, int roundValue)
    {
        // Create required bitmaps
        Bitmap shadowBitmap = BitmapFactory.decodeResource(mContext.getResources()
                , R.drawable.samsungapps_thumb_shadow);
        Bitmap outputBitmap = Bitmap.createBitmap(originalImage.getWidth()
                , originalImage.getHeight() + 80, Bitmap.Config.ARGB_8888);
        // Create canvas and pass bitmap to it 
        Canvas mCanvas = new Canvas(outputBitmap);
        // And finally draw the shaodw
        mCanvas.drawBitmap(Bitmap.createScaledBitmap(shadowBitmap, originalImage.getWidth()
                , (int)(shadowBitmap.getHeight() / 2.3), false), 0, originalImage.getHeight(), null);

        mCanvas.drawBitmap(originalImage, 0, 0, null);

        return outputBitmap;
    }
}

有关如何完成这两件事你知道吗?

Any idea about how to accomplish these two things?

推荐答案

通过你的codeI后发现,CarouselAdapter是表现在循环方式(无限滚动) 的code部分是导致这就是: -

after going through your code i found that CarouselAdapter is behaving in circular manner(infinite scrolling) part of code is which causes this is:-

@Override
    public int getCount() 
    {
        return Integer.MAX_VALUE;
    }

@Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        if (position >= bannerUri.length) 
            position %= bannerUri.length;
       .
       . 
       .
    }

然而,你在做什么

However you are doing

        ImageView i = new ImageView(mContext);
        displayImage(i, bannerUri[position]);
        i.setScaleType(ScaleType.FIT_XY); 
        container.addView(i);
        return i;

在instantiateItem,所以在您的ViewPager每一页,你是为ImageView的分配内存和ViewPager是越来越运行,因为这样的内存不足。 ViewPager有法公共无效setOffscreenPageLimit(INT限制)这将限制摧毁空闲页视图层次结构分配的内存.. 下面从Android文档

in instantiateItem, so for each page in your ViewPager, you are allocating memory for ImageView and ViewPager is getting run out of memory because of this. ViewPager has method public void setOffscreenPageLimit (int limit) which will limit the memory allocated by destroying idle pages in view hierarchy.. See below from the android documentation

设置应该保留到的任一侧的页数   当前页面中处于空闲状态的视图层次。超出此页   限制,由需要的时候适配器重新创建。

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

这是作为一个优化。如果你事先知道多少   网页,你需要支持或有延迟加载机制   放置在您的网页,调整这个设置可以有益处   感知分页动画和互动的平滑度。如果你有   少数页(3-4),你可以保持所有活动的一次,   更少的时间将在布局新创建的视图子树的花   用户页面来回。

This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.

您应该保持此限制较低,特别是如果你的网页有复杂   布局。此设置默认为1。

You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.

正如上面提到的,如果不指定任何价值,它会使用默认值1和ViewPager会破坏从视图层次空闲页。所以,你需要优化的值传递给它,根据你的内存需求(图像和平均图像大小数),将解决你的内存不足的问题。

As mentioned above, If do not specify any value to it , it will use default value 1 and ViewPager will destroy idle pages from view hierarchy. So you need to pass optimised value to it, according to your memory requirements(number of images and average image size), that will solve your Out of memory issue..