Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Android.App;
- using Android.Content;
- using Android.Graphics;
- using Android.Graphics.Drawables;
- using Android.Views;
- using Android.Widget;
- using Java.Util;
- namespace Android.Dialog
- {
- public class BadgeElement : Element, IImageUpdated
- {
- // Height for rows
- const int dimx = 48;
- const int dimy = 44;
- // radius for rounding
- const int roundPx = 12;
- public readonly ImageView Value;
- ImageView scaled;
- string imageUrl = "";
- Uri imageUri = null;
- static ImageLoader badgeIL = null;
- public BadgeElement (ImageView image, string url)
- : base(string.Empty)
- {
- if (badgeIL == null) {
- badgeIL = new ImageLoader (100, 4000000);
- }
- imageUrl = url;
- if (imageUri == null) {
- imageUri = new System.Uri (imageUrl);
- }
- if (image == null)
- {
- Value = MakeEmpty();
- scaled = Value;
- }
- else
- {
- Value = image;
- if (image.Drawable != null)
- scaled = Scale(Value);
- }
- }
- static ImageView MakeEmpty()
- {
- return new ImageView(null);
- }
- ImageView Scale(ImageView source)
- {
- var drawable = (BitmapDrawable)source.Drawable;
- var bitmap = drawable.Bitmap;
- var bMapScaled = Bitmap.CreateScaledBitmap(bitmap, dimx, dimy, true);
- source.SetImageBitmap(bMapScaled);
- return source;
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (scaled != null)
- scaled.Dispose();
- if (Value != null)
- Value.Dispose();
- }
- base.Dispose(disposing);
- }
- public override View GetView (Context context, View convertView, ViewGroup parent)
- {
- Drawable myDrawable;
- if (scaled == null)
- scaled = Scale (Value);
- Click = delegate {
- SelectImage ();
- };
- Android.Dialog.IImageUpdated callback = new ImageLoaderCallback (context, scaled, imageUri); // ImageLoaderCallback implements IImageUpdated
- myDrawable = badgeIL.RequestImage ((Uri)imageUri, (IImageUpdated)callback);
- if (myDrawable != null) {
- scaled.SetImageDrawable (myDrawable);
- }
- // var bitmap = BitmapFactory.DecodeStream (new Java.Net.URL(imageUrl).OpenStream());
- // scaled.SetImageBitmap(bitmap);
- var view = convertView as RelativeLayout ?? new RelativeLayout (context);
- var parms = new RelativeLayout.LayoutParams (ViewGroup.LayoutParams.WrapContent,
- ViewGroup.LayoutParams.WrapContent);
- parms.SetMargins (5, 2, 5, 2);
- parms.AddRule (LayoutRules.AlignParentLeft);
- scaled.Id = ViewId.getUniqueId();
- // SEC bug fix, not yet submitted to Kenny. Getting exception "specified view already has a parent"
- if (scaled.Parent != view) {
- view.AddView (scaled, parms);
- }
- TextView tv = new TextView(context);
- tv.Text = "This is a badge";
- view.AddView(tv);
- return view;
- }
- private void SelectImage()
- {
- var activity = (Activity)GetContext();
- Toast.MakeText(activity, "CLICKED", ToastLength.Short);
- var intent = new Intent(Intent.ActionPick, Provider.MediaStore.Images.Media.InternalContentUri);
- activity.StartActivity(intent);
- }
- public void UpdatedImage (System.Uri uri)
- {
- int i =0;
- }
- private class ImageLoaderCallback : IImageUpdated
- {
- int i=0;
- public ImageLoaderCallback(Context context, ImageView iv, Uri imageUri)
- {
- int i=0;
- Drawable newDrawable;
- newDrawable = badgeIL.RequestImage ((Uri)imageUri, null);
- if (newDrawable != null) {
- iv.SetImageDrawable (newDrawable);
- }
- }
- public void UpdatedImage (System.Uri uri)
- {
- int i=0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement