Advertisement
bit

BadgeElement.cs

bit
Jun 24th, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. using System;
  2. using Android.App;
  3. using Android.Content;
  4. using Android.Graphics;
  5. using Android.Graphics.Drawables;
  6. using Android.Views;
  7. using Android.Widget;
  8. using Java.Util;
  9.  
  10. namespace Android.Dialog
  11. {
  12. public class BadgeElement : Element, IImageUpdated
  13. {
  14. // Height for rows
  15. const int dimx = 48;
  16. const int dimy = 44;
  17.  
  18. // radius for rounding
  19. const int roundPx = 12;
  20.  
  21. public readonly ImageView Value;
  22. ImageView scaled;
  23.  
  24. string imageUrl = "";
  25. Uri imageUri = null;
  26.  
  27. static ImageLoader badgeIL = null;
  28.  
  29. public BadgeElement (ImageView image, string url)
  30. : base(string.Empty)
  31. {
  32. if (badgeIL == null) {
  33. badgeIL = new ImageLoader (100, 4000000);
  34. }
  35.  
  36. imageUrl = url;
  37. if (imageUri == null) {
  38. imageUri = new System.Uri (imageUrl);
  39. }
  40. if (image == null)
  41. {
  42. Value = MakeEmpty();
  43. scaled = Value;
  44. }
  45. else
  46. {
  47. Value = image;
  48. if (image.Drawable != null)
  49. scaled = Scale(Value);
  50. }
  51. }
  52.  
  53. static ImageView MakeEmpty()
  54. {
  55. return new ImageView(null);
  56. }
  57.  
  58. ImageView Scale(ImageView source)
  59. {
  60. var drawable = (BitmapDrawable)source.Drawable;
  61. var bitmap = drawable.Bitmap;
  62. var bMapScaled = Bitmap.CreateScaledBitmap(bitmap, dimx, dimy, true);
  63. source.SetImageBitmap(bMapScaled);
  64. return source;
  65. }
  66.  
  67. protected override void Dispose(bool disposing)
  68. {
  69. if (disposing)
  70. {
  71. if (scaled != null)
  72. scaled.Dispose();
  73. if (Value != null)
  74. Value.Dispose();
  75. }
  76. base.Dispose(disposing);
  77. }
  78.  
  79. public override View GetView (Context context, View convertView, ViewGroup parent)
  80. {
  81. Drawable myDrawable;
  82. if (scaled == null)
  83. scaled = Scale (Value);
  84.  
  85. Click = delegate {
  86. SelectImage ();
  87. };
  88.  
  89. Android.Dialog.IImageUpdated callback = new ImageLoaderCallback (context, scaled, imageUri); // ImageLoaderCallback implements IImageUpdated
  90.  
  91. myDrawable = badgeIL.RequestImage ((Uri)imageUri, (IImageUpdated)callback);
  92.  
  93. if (myDrawable != null) {
  94. scaled.SetImageDrawable (myDrawable);
  95. }
  96. // var bitmap = BitmapFactory.DecodeStream (new Java.Net.URL(imageUrl).OpenStream());
  97. // scaled.SetImageBitmap(bitmap);
  98.  
  99. var view = convertView as RelativeLayout ?? new RelativeLayout (context);
  100.  
  101. var parms = new RelativeLayout.LayoutParams (ViewGroup.LayoutParams.WrapContent,
  102. ViewGroup.LayoutParams.WrapContent);
  103. parms.SetMargins (5, 2, 5, 2);
  104. parms.AddRule (LayoutRules.AlignParentLeft);
  105.  
  106. scaled.Id = ViewId.getUniqueId();
  107.  
  108. // SEC bug fix, not yet submitted to Kenny. Getting exception "specified view already has a parent"
  109. if (scaled.Parent != view) {
  110. view.AddView (scaled, parms);
  111. }
  112. TextView tv = new TextView(context);
  113. tv.Text = "This is a badge";
  114. view.AddView(tv);
  115. return view;
  116. }
  117.  
  118. private void SelectImage()
  119. {
  120.  
  121. var activity = (Activity)GetContext();
  122. Toast.MakeText(activity, "CLICKED", ToastLength.Short);
  123. var intent = new Intent(Intent.ActionPick, Provider.MediaStore.Images.Media.InternalContentUri);
  124. activity.StartActivity(intent);
  125. }
  126.  
  127. public void UpdatedImage (System.Uri uri)
  128. {
  129. int i =0;
  130.  
  131. }
  132.  
  133. private class ImageLoaderCallback : IImageUpdated
  134. {
  135. int i=0;
  136. public ImageLoaderCallback(Context context, ImageView iv, Uri imageUri)
  137. {
  138. int i=0;
  139. Drawable newDrawable;
  140. newDrawable = badgeIL.RequestImage ((Uri)imageUri, null);
  141.  
  142. if (newDrawable != null) {
  143. iv.SetImageDrawable (newDrawable);
  144. }
  145.  
  146.  
  147. }
  148. public void UpdatedImage (System.Uri uri)
  149. {
  150. int i=0;
  151. }
  152.  
  153.  
  154. }
  155.  
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement