gpt4 book ai didi

android - 在回收站内的 cardview 中滑动图像的最佳方法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:08:07 26 4
gpt4 key购买 nike

我有一个带有 CardviewRecyclerView。我想在这个 CardView 中滑动图像,就像在 OLX 应用程序中一样。最好的方法是什么?我考虑将 viewpager 放在 cardview 中。可以吗,或者我应该试试别的东西?

我用 ViewPager 做到了,但它看起来太慢了。这是 viewpager 适配器的一部分。

 @Override
public Object instantiateItem(ViewGroup collection, int position) {

LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.viewpager_custom, collection, false);
collection.addView(layout);

ImageView image = (ImageView) layout.findViewById(R.id.viewPagerImageView);
image.setImageResource(mPics[position]);

return layout;
}

enter image description here

最佳答案

你走对了路。

只做一件事加载压缩位图而不是未压缩位图。您直接将位图资源设置为 ImageView 。要么使用像 Picasso 这样的库 https://github.com/square/picasso/

或使用 google 的官方资源来高效加载大型位图。

首先在你的activity中复制这个方法:

public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}

return inSampleSize;
}

然后这个解码位图的方法:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

然后像这样加载你的位图:

@Override
public Object instantiateItem(ViewGroup collection, int position) {

LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.viewpager_custom, collection, false);
collection.addView(layout);

ImageView image = (ImageView) layout.findViewById(R.id.viewPagerImageView);
image.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.myimage, reqwidth, reqheight));

return layout;
}

关于android - 在回收站内的 cardview 中滑动图像的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33389786/

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