Xamarin加载带有加载进度条的图像

时间:2018-10-08 07:05:59

标签: xamarin xamarin.forms xamarin.ios xamarin.android progress-bar

如何将此link代码更改为Xamarin 或找到另一种在Xamarin中加载进度的方法来加载图像

1 个答案:

答案 0 :(得分:0)

我解决了 pic

public class MhnImgview:ImageView{public string Url;
   private Context _context;
   int mDuration;
   int mProgress;
   Paint mPaint = new Paint();
   RectF mRectF = new RectF();
   Color mBackgroundColor;
   Color mPrimaryColor;
   float mStrokeWidth;
   public IOnProgressChangeListener MOnChangeListener; public interface IOnProgressChangeListener
   {

       void OnChange(int duration, int progress, float rate);
   }
   public void SetOnProgressChangeListener(IOnProgressChangeListener l)
   {
       MOnChangeListener = l;
   }
    protected MhnImgview(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
   {
   }

   public MhnImgview(Context context) : base(context)
   {

   }
   public void SetMax(int max)
   {
       if (max < 0)
       {
           max = 0;
       }
       mDuration = max;
   }
   public int GetMax()
   {
       return mDuration;
   }
   public void SetProgress(int progress)
   {
       if (progress > mDuration)
       {
           progress = mDuration;
       }
       mProgress = progress;
       MOnChangeListener?.OnChange(mDuration, progress, GetRateOfProgress());
       Invalidate();
   }
   public int GetProgress()
   {
       return mProgress;
   }

   public void SetBackgroundColr(Color color)
   {
       mBackgroundColor = color;
   }

   public void SetPrimaryColor(Color color)
   {
       mPrimaryColor = color;
   }
   float GetRateOfProgress()
   {
       return (float)mProgress / mDuration;
   }
    public void SetCircleWidth(float width)
   {
       mStrokeWidth = width;
   }
    public async Task ShowImage()
    {

        var d = await GetImageBitmapFromUrl(Url);

       SetImageBitmap(d); 
   }

   protected override void OnDraw(Canvas canvas)
   {
       base.OnDraw(canvas);
       int halfWidth = Width / 2;
       int halfHeight = Height / 2;
       int radius = halfWidth < halfHeight ? halfWidth : halfHeight;
       radius = radius / 2;
       float halfStrokeWidth = mStrokeWidth / 2;

       mPaint.Color = mBackgroundColor;
       mPaint.Dither = true;
       mPaint.Flags = PaintFlags.AntiAlias;
       mPaint.AntiAlias = true;
       mPaint.StrokeWidth = mStrokeWidth;
       mPaint.SetStyle(Paint.Style.Stroke);
       canvas.DrawCircle(halfWidth, halfHeight, radius - halfStrokeWidth, mPaint);

       mPaint.Color = mPrimaryColor;
       mRectF.Top = halfHeight - radius + halfStrokeWidth;
       mRectF.Bottom = halfHeight + radius - halfStrokeWidth;
       mRectF.Left = halfWidth - radius + halfStrokeWidth;
       mRectF.Right = halfWidth + radius - halfStrokeWidth;
       canvas.DrawArc(mRectF, -90, GetRateOfProgress() * 360, false, mPaint);
       canvas.Save();


    }

   private async Task<Bitmap> GetImageBitmapFromUrl(string imagename)
   {
       Bitmap imageBitmap = null;
       var baseurl = imagename;
       try
       {
           var url = baseurl; 
           using (var webClient = new WebClient())
           {
               webClient.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
                webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
               var imageBytes = await webClient.DownloadDataTaskAsync(url);

               if (imageBytes != null && imageBytes.Length > 0)
               {
                   imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
               }
           }
           return imageBitmap;
       }
       catch (Exception ex)
       {
           var d = ex.Message;
       }
       return null;
   }


    private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        SetMax(100);
        SetBackgroundColr(Color.Rgb(44,59,76));
        SetPrimaryColor(Color.Rgb(236,104,88));
        SetCircleWidth(7.5f);
       SetProgress(e.ProgressPercentage);

    }

    public MhnImgview(Context context, IAttributeSet attrs) : base(context, attrs)
   {

    }

   public MhnImgview(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
   {


    }

    public MhnImgview(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
   {


    }

}

使用

   var img = FindViewById<MhnImgview>(Resource.Id.imgmhn);
        img.Url = "http://www.neyrizcement.com/upload/3rcqesmc.jpg";

        await img.ShowImage();