2017-09-18 11 views
0

Android Universal Image Loader를 사용 중이고 재생 목록 항목과 슬라이더의 이미지 크기를 조정하고 싶습니다. 내 슬라이더의 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

나는 한 번 전에 오래된 질문에서 그것을 가지고 당신의 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; 
    }