gpt4 book ai didi

android - 使用 ViewFlipper 或 ViewPager 制作轮播

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:04 25 4
gpt4 key购买 nike

自从 GalleryView 被弃用后,我们应该迁移到一些替代小部件,在我的例子中,ViewFlipper 是最好的,但我遇到了几个问题,正如您在下面看到的screenshot 我用 GalleryView 设计了一个旋转木马 ImageGallery:

enter image description here

使用 ViewFlipper 一切都如我所料,但我无法实现两件事:

1- ViewFlipper 始终显示一项;但是我需要一次显示三个项目(或更多)。

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


正如 FlávioFaria 在下面的帖子中提到的关于 ViewPager 的那样,这也是一个很好的案例,但我无法将我的放大动画传递给它!

我已经用 ViewPager 完成了所有工作,现在它运行良好,但我错过了一个功能,那就是无限滚动!

添加了我的 PagerAdapter

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

关于如何完成这两件事有什么想法吗?

最佳答案

通过您的代码后,我发现 CarouselAdapter 以循环方式运行(无限滚动)部分代码是导致这个的原因是:-

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

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

在 instantiateItem 中,因此对于 ViewPager 中的每个页面,您都在为 ImageView 分配内存,因此 ViewPager 内存不足。ViewPager 有方法 public void setOffscreenPageLimit (int limit) 它将通过销毁 View 层次结构中的空闲页面来限制分配的内存。<强> 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.

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.

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

如上所述,如果不指定任何值,它将使用默认值 1,ViewPager 将从 View 层次结构中销毁空闲页面。因此,您需要根据您的内存要求(图像数量和平均图像大小)将优化值传递给它,这将解决您的内存不足问题..

关于android - 使用 ViewFlipper 或 ViewPager 制作轮播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8379261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com