天堂向右,我依然向左

天下之大,虽离家千里,何处不可往!何事不可为!
生活之路,纵坎坷曲折,当奋斗不息,则精彩纷呈!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

 

Java代码 
  1. @implementation BrightnessController  
  2.   
  3. // MyCreateBitmapContext: Source based on Apple Sample Code  
  4. CGContextRef MyCreateBitmapContext (int pixelsWide,  
  5.                                     int pixelsHigh)  
  6. {  
  7.     CGContextRef    context = NULL;  
  8.     CGColorSpaceRef colorSpace;  
  9.     void *          bitmapData;  
  10.     int             bitmapByteCount;  
  11.     int             bitmapBytesPerRow;  
  12.       
  13.     bitmapBytesPerRow   = (pixelsWide * 4);  
  14.     bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);  
  15.       
  16.     colorSpace = CGColorSpaceCreateDeviceRGB();  
  17.     bitmapData = malloc( bitmapByteCount );  
  18.     if (bitmapData == NULL)  
  19.     {  
  20.         fprintf (stderr, "Memory not allocated!");  
  21.         CGColorSpaceRelease( colorSpace );  
  22.         return NULL;  
  23.     }  
  24.     context = CGBitmapContextCreate (bitmapData,  
  25.                                      pixelsWide,  
  26.                                      pixelsHigh,  
  27.                                      8,  
  28.                                      bitmapBytesPerRow,  
  29.                                      colorSpace,  
  30.                                      kCGImageAlphaPremultipliedLast);  
  31.     if (context== NULL)  
  32.     {  
  33.         free (bitmapData);  
  34.         fprintf (stderr, "Context not created!");  
  35.         return NULL;  
  36.     }  
  37.     CGColorSpaceRelease( colorSpace );  
  38.       
  39.     return context;  
  40. }  
  41.   
  42. // addRoundedRectToPath: Source based on Apple Sample Code  
  43. static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,  
  44.                                  float ovalHeight)  
  45. {  
  46.     float fw, fh;  
  47.     if (ovalWidth == 0 || ovalHeight == 0) {  
  48.         CGContextAddRect(context, rect);  
  49.         return;  
  50.     }  
  51.       
  52.     CGContextSaveGState(context);  
  53.     CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));  
  54.     CGContextScaleCTM(context, ovalWidth, ovalHeight);  
  55.     fw = CGRectGetWidth(rect) / ovalWidth;  
  56.     fh = CGRectGetHeight(rect) / ovalHeight;  
  57.       
  58.     CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner  
  59.     CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);  // Top right corner  
  60.     CGContextAddArcToPoint(context, 0, fh, 0, fh/21); // Top left corner  
  61.     CGContextAddArcToPoint(context, 00, fw/201); // Lower left corner  
  62.     CGContextAddArcToPoint(context, fw, 0, fw, fh/21); // Back to lower right  
  63.       
  64.     CGContextClosePath(context);  
  65.     CGContextRestoreGState(context);  
  66. }  
  67.   
  68. // Build an image tinted at the given percentage  
  69. id createImage(float percentage)  
  70. {  
  71.     CGContextRef context  = MyCreateBitmapContext(3030);  
  72.     addRoundedRectToPath(context, CGRectMake(0.0f, 0.0f, 30.0f, 30.0f), 4.0f, 4.0f);  
  73.     CGFloat gray[4] = {percentage, percentage, percentage, 1.0f};  
  74.     CGContextSetFillColor(context, gray);  
  75.     CGContextFillPath(context);  
  76.     CGImageRef myRef = CGBitmapContextCreateImage (context);  
  77.     free(CGBitmapContextGetData(context));  
  78.     CGContextRelease(context);  
  79.     return [UIImage imageWithCGImage:myRef];  
  80. }  
  81.   
  82.   
  83. #define MAXDEPTH 8  
  84.   
  85. -(BrightnessController *) initWithBrightness: (int) aBrightness  
  86. {  
  87.     self = [super init];  
  88.     brightness = aBrightness;  
  89.     self.title = [NSString stringWithFormat:@"%d%%", brightness * 10];  
  90.     [self.tabBarItem initWithTitle:self.title image:createImage(((float) brightness / 10.0f)) tag:0];  
  91.     return self;  
  92. }  
  93.   
  94. - (void)loadView  
  95. {  
  96.     UIView *contentView = [[UIView alloc] init];  
  97.     float percent = brightness * 0.1;  
  98.     contentView.backgroundColor = [UIColor colorWithRed:percent green:percent blue:percent alpha:1.0];  
  99.     contentView.autoresizesSubviews = YES;  
  100.     contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);  
  101.     self.view = contentView;  
  102.     [contentView release];  
  103. }  
  104.   
  105.   
  106. @end  
posted on 2010-08-24 15:59  老舟  阅读(1013)  评论(0编辑  收藏  举报