Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. /**
  2. * Created by Chris on 7/29/16.
  3. *
  4. * Modified code from bfattori & N3ll on github
  5. *
  6. * function allows gradients to be applied to the background of view elements on Android & iOS
  7. *
  8. */
  9.  
  10. var platform = require("platform");
  11. var coreView = require("ui/core/view");
  12. var colorModule = require("color");
  13. var Color = colorModule.Color;
  14.  
  15. var coreView = require("ui/core/view"),
  16. colorModule = require("color"),
  17. Color = colorModule.Color;
  18.  
  19. function linearGradient(root, viewId, colors, stops) {
  20. console.log(platform.device.os);
  21. var _colors = [],
  22. _view = coreView.getViewById(root, viewId),
  23. nativeView;
  24. console.log(_view);
  25.  
  26. if (_view) {
  27. nativeView = _view._nativeView;
  28. } else {
  29. throw TraceableException("Cannot find view '" + _view + "' in page!");
  30. }
  31.  
  32. if (!nativeView) {
  33. return;
  34. }
  35.  
  36. colors.forEach(function(c, idx) {
  37. if (!(c instanceof Color)) {
  38. colors[idx] = new Color(c);
  39. }
  40. });
  41.  
  42. if (platform.device.os === platform.platformNames.android) {
  43. console.log("android");
  44. var backgroundDrawable = nativeView.getBackground(),
  45. orientation = android.graphics.drawable.GradientDrawable.Orientation.TOP_BOTTOM,
  46. LINEAR_GRADIENT = 0;
  47.  
  48. colors.forEach(function(c) {
  49. _colors.push(c.android);
  50. });
  51.  
  52. if (!(backgroundDrawable instanceof android.graphics.drawable.GradientDrawable)) {
  53. backgroundDrawable = new android.graphics.drawable.GradientDrawable();
  54. backgroundDrawable.setColors(_colors);
  55. backgroundDrawable.setGradientType(LINEAR_GRADIENT);
  56. nativeView.setBackgroundDrawable(backgroundDrawable);
  57. }
  58. } else if (platform.device.os === platform.platformNames.ios) {
  59. console.log("ios");
  60. var view = root.ios.view;
  61. var colorsArray = NSMutableArray.alloc().initWithCapacity(2);
  62. colors.forEach(function(c) {
  63. colorsArray.addObject(interop.types.id(c.ios.CGColor));
  64. });
  65. var gradientLayer = CAGradientLayer.layer();
  66. gradientLayer.colors = colorsArray;
  67. gradientLayer.frame = nativeView.bounds;
  68. nativeView.layer.insertSublayerAtIndex(gradientLayer,0);
  69. }
  70. console.log("In here");
  71. }
  72. exports.linearGradient = linearGradient;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement