我正在使用Android通用图像加载器,我想调整图像我的回收器视图项和我的滑块。我在我的滑块viewPager中使用了这个方法:
public class ViewPagerAdapter extends PagerAdapter {
private ArrayList<String> imageUrls;
private Context context;
private DisplayImageOptions defaultOptions2;
private ImageLoaderConfiguration config2;
public ViewPagerAdapter(Context context, ArrayList<String> imageUrls) {
this.context = context;
this.imageUrls = imageUrls;
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
final float dpHeight = displayMetrics.heightPixels ;
final float dpWidth = displayMetrics.widthPixels ;
byte[] toEncrypt = (G.getPremiumState()[0] + ":" + G.getPremiumState()[1]).getBytes();
String encryptedCredentials = Base64.encodeToString(toEncrypt, Base64.DEFAULT);
Map<String, String> headers = new HashMap();
headers.put("Authorization", "Basic " + encryptedCredentials);
defaultOptions2 = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.showImageOnLoading(R.drawable.loading_02)
.showImageOnFail(R.drawable.ic_empty_white_box)
.extraForDownloader(headers)
.postProcessor(new BitmapProcessor() {
@Override
public Bitmap process(Bitmap bmp) {
return Bitmap.createScaledBitmap(bmp, (int) dpWidth, (int) (dpHeight/2), false);
}
})
.build();
config2 = new ImageLoaderConfiguration.Builder(G.context)
.defaultDisplayImageOptions(defaultOptions2)
.imageDownloader(new AuthDownloader(G.context))
.build();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return imageUrls.size();
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.slide_layout, null);
((ViewPager) collection).addView(view);
final ImageView img = (ImageView) view.findViewById(R.id.slideImage);
ImageLoader imageLoader = ImageLoader.getInstance(); //Get singleton instance
imageLoader.init(config2);
imageLoader.displayImage(imageUrls.get(position), img);
imageLoader.handleSlowNetwork(true);
return view;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((FrameLayout) object);
}
}
这是我的recyclerView适配器:
public class BaseListAdapter extends RecyclerView.Adapter<BaseListAdapter.ViewHolder> {
private DisplayImageOptions defaultOptions;
private ImageLoaderConfiguration config;
public BaseListAdapter(List<PList> menuItems, Context mContext) {
this.menuItems = menuItems;
this.mContext = mContext;
this.activitySingleGroup = (ActivitySingleGroup) mContext;
byte[] toEncrypt = (G.getPremiumState()[0] + ":" + G.getPremiumState()[1]).getBytes();
String encryptedCredentials = Base64.encodeToString(toEncrypt, Base64.DEFAULT);
Map<String, String> headers = new HashMap();
headers.put("Authorization", "Basic " + encryptedCredentials);
defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.showImageOnLoading(R.drawable.loading_02)
.showImageOnFail(R.drawable.ic_empty_white_box)
.extraForDownloader(headers)
.postProcessor(new BitmapProcessor() {
@Override
public Bitmap process(Bitmap bmp) {
return Bitmap.createScaledBitmap(bmp, 120, 120, false);
}
})
.build();
config = new ImageLoaderConfiguration.Builder(activitySingleGroup)
.defaultDisplayImageOptions(defaultOptions)
.imageDownloader(new AuthDownloader(activitySingleGroup))
.build();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView txtName;
TextView txtPrice;
TextView txtCode;
ImageView imgDefault;
LinearLayout parentLayout;
CoordinatorLayout coordinatorLayout;
public ViewHolder(View v) {
super(v);
txtName = (TextView) v.findViewById(R.id.txtName);
txtPrice = (TextView) v.findViewById(R.id.txtPrice);
txtCode = (TextView) v.findViewById(R.id.txt_code);
imgDefault = (ImageView) v.findViewById(R.id.img_defaultImage);
parentLayout = (LinearLayout) v.findViewById(R.id.parentLayout);
coordinatorLayout = (CoordinatorLayout) v.findViewById(R.id.coordinatorLayout);
}
}
@Override
public BaseListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new View
final View v = LayoutInflater.from(activitySingleGroup).inflate(R.layout.activity_normal_group_recycler, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
//Download and Load Default Image from server into imgDefault ImageView
String url = "http://185.2.14.77:8080/api/images/download/";
if (menuItems.get(position).getPDefaultImage().getIId() != null) {
url += menuItems.get(position).getPDefaultImage().getIId();
ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance
imageLoader.init(config);
imageLoader.displayImage(url, holder.imgDefault);
imageLoader.handleSlowNetwork(true);
} else {
holder.imgDefault.setImageResource(R.drawable.loading_01);
}
holder.txtCode.setText("" + menuItems.get(position).getPCode());
holder.txtName.setText(menuItems.get(position).getPName());
holder.txtPrice.setText(String.valueOf(menuItems.get(position).getPPrice()));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
prepareBottomSheet(view, position, holder.coordinatorLayout);
}
});
// Here you apply the animation when the view is bound
//setAnimation(holder.parentLayout, position);
}
@Override
public int getItemCount() {
if (menuItems.size() > 0) {
return menuItems.size();
} else {
return 0;
}
}
我对它们使用了相同的方法,但是图像将在我的recyclerView中调整大小,但它们不会在我的viewPager中调整大小。
答案 0 :(得分:0)
我从一个旧问题中得到了它,把它放在你的onBindViewHolder
BitmapFactory.Options options = new BitmapFactory.Options();
final Bitmap originalImage = BitmapFactory.decodeByteArray(imagem, 0, imagem.length);
options.outHeight = originalImage.getHeight();
options.outWidth = originalImage.getWidth();
options.inSampleSize = calculateInSampleSize(options, 100, 100);
调用此方法传递选项(上图),请求的宽度和高度。
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;
}