zoukankan      html  css  js  c++  java
  • iPhone 画圆角矩形

     

     

    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  
  • 相关阅读:
    Linux运维之监控CPU和内存的日志工具
    Linux磁盘缓存的有趣实验
    Linux运维之内存分析2
    Linux运维之内存分析
    使用kubectl create 和 kubectl apply创建资源对象的区别
    Docker学习:Image的本地存储结构
    Docker 空间使用分析与清理
    HeidiSQL、Navicat、mysql命令和source命令导入sql脚本的速度比较
    Centos 7.2天兔(Lepus 3.8)数据库监控系统部署
    MegaCli 监控raid状态
  • 原文地址:https://www.cnblogs.com/KiloNet/p/1807346.html
Copyright © 2011-2022 走看看