显示自定义尺寸图像通用ImageLoader

时间:2014-02-25 16:10:49

标签: android image universal-image-loader coverflow

我使用Universal ImageLoader从服务器下载大图像并在coverflow中显示它们。我面临的问题是我无法为我的图像设置自定义大小。它只需要整个屏幕。例如,我正在下载尺寸为500 x 1200的图像 - 我想要显示尺寸为300 x 300的图像。但它需要完整显示。因为我被困3天以上,请帮我解决这个问题。感谢。

代码

public class HomeCoverFlow {

    Context mContext;
    public CoverFlow coverFlow;
    ImageAdapter coverImageAdapter;
    ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions options;

     /** Called when the activity is first created. */

    public HomeCoverFlow(Context context)
    {
        mContext = context;
           options = new DisplayImageOptions.Builder()
           .showImageOnLoading(R.drawable.flyer_placeholder)
           .showImageForEmptyUri(R.drawable.flyer_placeholder)
           .imageScaleType(ImageScaleType.EXACTLY)
           .cacheInMemory(true)
           .build();

         coverFlow = new CoverFlow(context);

         coverImageAdapter =  new ImageAdapter(mContext);

         coverFlow.setAdapter(coverImageAdapter);

         coverFlow.setSpacing(-25);
         coverFlow.setSelection(2, true);
         coverFlow.setAnimationDuration(1000);

         coverFlow.setGravity(Gravity.CENTER_VERTICAL);


            // HEIGHT
            coverImageAdapter.coverflowHeight = 100; //display.getHeight() / 3;

            //WIDTH
            coverImageAdapter.coverflowWidth = 100;// display.getWidth() / 2;


        }

适配器中创建项目视图的方法,我在这里使用imageloader下载图像并显示

public View getView(int position, View convertView, ViewGroup parent) {

         FeaturedFlyerData data = (FeaturedFlyerData) getItem(position);

             if(data.flyerImage == null)
             {
                 setNewImage(position);
             }
             //see text or image
             if(data.displayImage)
             {
                 data.flyerImage.setBackgroundColor(Color.BLACK);

                 imageLoader.displayImage(data.url, data.flyerImage, options, null);

             }


         return data.flyerImage;

}       

void setNewImage(int position)
     {
         FeaturedFlyerData data = (FeaturedFlyerData) getItem(position);

         data.flyerImage = new ImageView(mContext);
         data.flyerImage.layout(0, 10, 200,350);
         data.flyerImage.setScaleType(ScaleType.CENTER_INSIDE);
         data.flyerImage.setTag(Integer.toString(position));
         data.flyerImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            imageClicked(Integer.parseInt(v.getTag().toString()));

        }
    });
    }

}

我不知道这里还提供了哪些代码段。如果您需要进一步说明,请发表评论。感谢。

1 个答案:

答案 0 :(得分:1)

我使用以下代码使用通用图像加载器在coverflow中显示自定义尺寸图像。

在构造函数中,将DisplayImageOptions设置为:

DisplayImageOptions options = new DisplayImageOptions.Builder()
      .showImageOnLoading(R.drawable.flyer_placeholder)
      .showImageForEmptyUri(R.drawable.flyer_placeholder)
      .imageScaleType(ImageScaleType.EXACTLY)
      .cacheInMemory(true)
      .build();

//set coverflow full screen, and images size.

         coverFlow.setSpacing(-25);
         coverFlow.setAnimationDuration(1000);
         coverFlow.setGravity(Gravity.CENTER_VERTICAL);
         coverFlow.setLayoutParams(new CoverFlow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

         // GET SCREEN SIZE
            WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();

            DisplayMetrics dm = new DisplayMetrics();
            display.getMetrics(dm);

//Here I am setting image size using my custom adapter in "dp". Avoid to use pixels as things will mess up for different screen sizes

            coverImageAdapter.coverflowWidth = (int) Math.ceil(dm.widthPixels * (dm.densityDpi / 160.0));
            coverImageAdapter.coverflowHeight = (int) Math.ceil(dm.heightPixels * (dm.densityDpi / 160.0)) / 2;

在适配器的setNewImage方法(在getView中调用)中,使用 Gallery.LayoutParams

void setNewImage(int position)
 {
     FeaturedFlyerData data = (FeaturedFlyerData) getItem(position);

     data.flyerImage = new ImageView(mContext);      
     data.flyerImage.setLayoutParams(new Gallery.LayoutParams(coverflowWidth, coverflowHeight));
     data.flyerImage.setScaleType(ScaleType.CENTER_INSIDE);
}