2017-01-23 28 views

回答

1

那麼我們如何才能實現它的GridView的適配器?

您可以創建一個類(以下ThumbImageFactory)包文件中提到的所有功能:

public class ThumbImageFactory 
{ 
    public readonly Context context; 
    public ThumbImageFactory(Context c) 
    { 
     context = c; 
    } 

    public static int CalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) 
    { 
     // Raw height and width of image 
     float height = options.OutHeight; 
     float width = options.OutWidth; 
     double inSampleSize = 1D; 

     if (height > reqHeight || width > reqWidth) 
     { 
      int halfHeight = (int)(height/2); 
      int halfWidth = (int)(width/2); 

      // Calculate a inSampleSize that is a power of 2 - the decoder will use a value that is a power of two anyway. 
      while ((halfHeight/inSampleSize) > reqHeight && (halfWidth/inSampleSize) > reqWidth) 
      { 
       inSampleSize *= 2; 
      } 

     } 
     return (int)inSampleSize; 
    } 

    public Bitmap LoadScaledDownBitmapForDisplay(Resources res, BitmapFactory.Options options, int reqWidth, int reqHeight,int resourceId) 
    { 
     // Calculate inSampleSize 
     options.InSampleSize = CalculateInSampleSize(options, reqWidth, reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.InJustDecodeBounds = false; 

     return BitmapFactory.DecodeResource(res, resourceId, options); 
    } 

    public BitmapFactory.Options GetBitmapOptionsOfImage(int resourceId) 
    { 
     BitmapFactory.Options options = new BitmapFactory.Options 
     { 
      InJustDecodeBounds = true 
     }; 

     // The result will be null because InJustDecodeBounds == true. 
     Bitmap result = BitmapFactory.DecodeResource(context.Resources, resourceId, options); 


     int imageHeight = options.OutHeight; 
     int imageWidth = options.OutWidth; 


     return options; 
    } 
} 

創建您的適配器的ThumbImageFactory實例,並調用函數GetView

public class ImageAdapter : BaseAdapter 
{ 
    private readonly Context context; 
    private ThumbImageFactory thumbFactory; 

    public ImageAdapter(Context c) 
    { 
     context = c; 
     thumbFactory = new ThumbImageFactory(c); 
    } 
    ... 
    public override View GetView(int position, View convertView, ViewGroup parent) 
    { 
     ImageView imageView; 
     if (convertView == null) 
     { 
      imageView = new ImageView(this.context); 
      imageView.LayoutParameters = new AbsListView.LayoutParams(150, 150); 
      imageView.SetScaleType(ImageView.ScaleType.CenterCrop); 
      imageView.SetPadding(0, 0, 0, 0); 
     } 
     else 
     { 
      imageView = (ImageView)convertView; 
     } 

     Bitmap bitmap = GetThumbImage(thumbIds[position]); 
     imageView.SetImageBitmap(bitmap); 
     return imageView; 
    } 

    public Bitmap GetThumbImage(int resourceId) 
    { 
     BitmapFactory.Options options = thumbFactory.GetBitmapOptionsOfImage(resourceId); 
     Bitmap bitmap=thumbFactory.LoadScaledDownBitmapForDisplay(context.Resources, options, 150, 150, resourceId); 
     return bitmap; 
    } 
} 

注意:我們不能將GetView修改爲異步,所以我將文檔中的所有功能都更改爲同步功能。這裏是完整的Demo

+0

謝謝。這非常有用 –