当前位置: 编程技术>移动开发
本页文章导读:
▪【转】兑现自定义布局的Notification 【转】实现自定义布局的Notification
转自:http://blog.csdn.net/chenlong12580/article/details/7099251
实现了自己的notification,需要利用RemoteView来实现自定义布局,这里就来举一个示例,方便理解。
第一.........
▪ 基于Selector的旋钮图片效果 基于Selector的按钮图片效果
图片按钮在获取焦点和失去焦点时,会有不同的显示效果,可以针对每个按钮添加OnFocusListener事件,但还是不方便,而且有太多的java代码,对于后期维护也不是.........
▪ 手势与抚摸 手势与触摸
如果要VIEW支持用户交互,得要设置
self.userInteractionEnabled = YES;
一个简单的UIView例子
@interface DragView : UIImageView
{
CGPoint startLocation;
}
@end
@implementation DragView
- (id) initWithImage: (U.........
[1]【转】兑现自定义布局的Notification
来源: 互联网 发布时间: 2014-02-18
【转】实现自定义布局的Notification
转自:http://blog.csdn.net/chenlong12580/article/details/7099251
实现了自己的notification,需要利用RemoteView来实现自定义布局,这里就来举一个示例,方便理解。
第一步:新建一个工程,命名为cusNotification;
第二步:新建一个布局文件(即自定义的notification的布局文件:custom_notification.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_marginRight="10dp" android:contentDescription="@string/Image" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/image" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/image" android:layout_below="@id/title" /> </RelativeLayout>
第三步:新建上面布局文件中引用到的styyes.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" /> <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" /> </resources>
第四步:修改java源文件,代码如下:
public class CusNotificationActivity extends Activity { private static final int CUSTOM_VIEW_ID = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Notification notification = new Notification(); int icon = R.drawable.ic_launcher; CharSequence tickerText = "Notification01"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); contentView.setImageViewResource(R.id.image, R.drawable.notification_image); contentView.setTextViewText(R.id.title, "Custom notification"); contentView.setTextViewText(R.id.text, "This is a custom layout"); notification.contentView = contentView; Intent notificationIntent = new Intent(this, CusNotificationActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(CusNotificationActivity.this, 0, notificationIntent, 0); notification.contentIntent = contentIntent; String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); mNotificationManager.notify(CUSTOM_VIEW_ID, notification); } }
这里主要是讲解自定义布局notification的实现,并没有做出很炫的效果!就到这吧!
[2] 基于Selector的旋钮图片效果
来源: 互联网 发布时间: 2014-02-18
基于Selector的按钮图片效果
图片按钮在获取焦点和失去焦点时,会有不同的显示效果,可以针对每个按钮添加
OnFocusListener事件,但还是不方便,而且有太多的java代码,对于后期维护也不是很方便!
运用selector就解决了该问题:
首先在drawable目录下创建1_selector.xml文件(名称可以随意取),如果是获取焦点和失去焦点的效果,内容代码如下:
xxxxx处为不同显示的图片。
其次,对按钮进行配置
?????是selector对应xml文件的文件名,在这里对应的是1_selector
然后运行虚拟机,就可以看到效果了
图片按钮在获取焦点和失去焦点时,会有不同的显示效果,可以针对每个按钮添加
OnFocusListener事件,但还是不方便,而且有太多的java代码,对于后期维护也不是很方便!
运用selector就解决了该问题:
首先在drawable目录下创建1_selector.xml文件(名称可以随意取),如果是获取焦点和失去焦点的效果,内容代码如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:drawable="@drawable/xxxxxx"></item> <item android:state_focused="false" android:drawable="@drawable/xxxxxx"></item> </selector>
xxxxx处为不同显示的图片。
其次,对按钮进行配置
<ImageButton android:id="@+id/button1" android:layout_width="105px" android:layout_height="105px" android:layout_x="230px" android:layout_y="595px" android:background="@drawable/????????" />
?????是selector对应xml文件的文件名,在这里对应的是1_selector
然后运行虚拟机,就可以看到效果了
[3] 手势与抚摸
来源: 互联网 发布时间: 2014-02-18
手势与触摸
如果要VIEW支持用户交互,得要设置
self.userInteractionEnabled = YES;
一个简单的UIView例子
@interface DragView : UIImageView { CGPoint startLocation; } @end @implementation DragView - (id) initWithImage: (UIImage *) anImage { if (self = [super initWithImage:anImage]) self.userInteractionEnabled = YES; return self; } - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { // Calculate and store offset, and pop view into front if needed CGPoint pt = [[touches anyObject] locationInView:self]; startLocation = pt; [[self superview] bringSubviewToFront:self]; NSLog(@"begin"); } - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { // Calculate offset CGPoint pt = [[touches anyObject] locationInView:self]; float dx = pt.x - startLocation.x; float dy = pt.y - startLocation.y; CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy); // Bound movement into parent bounds float halfx = CGRectGetMidX(self.bounds); newcenter.x = MAX(halfx, newcenter.x); newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x); float halfy = CGRectGetMidY(self.bounds); newcenter.y = MAX(halfy, newcenter.y); newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y); // Set new location self.center = newcenter; } @end
手动创建一图片的UIImageView
- (UIImage *) createImage { UIColor *color = [UIColor colorWithRed:RANDLEVEL green:RANDLEVEL blue:RANDLEVEL alpha:1.0f]; //设置图片的区域 UIGraphicsBeginImageContext(CGSizeMake(SIDELENGTH, SIDELENGTH)); CGContextRef context = UIGraphicsGetCurrentContext(); // Create a filled ellipse 填充区域 [color setFill]; CGRect rect = CGRectMake(0.0f, 0.0f, SIDELENGTH, SIDELENGTH); CGContextAddEllipseInRect(context, rect); CGContextFillPath(context); // Outline the circle a couple of times CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]); CGContextAddEllipseInRect(context, CGRectInset(rect, INSET_AMT, INSET_AMT)); CGContextStrokePath(context); CGContextAddEllipseInRect(context, CGRectInset(rect, 2*INSET_AMT, 2*INSET_AMT)); CGContextStrokePath(context); UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return theImage; }
判断是否在区域里面 HALFSIDE是圆的半径
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event { CGPoint pt; float HALFSIDE = SIDELENGTH / 2.0f; // normalize with centered origin pt.x = (point.x - HALFSIDE) / HALFSIDE; pt.y = (point.y - HALFSIDE) / HALFSIDE; // x^2 + y^2 = radius float xsquared = pt.x * pt.x; float ysquared = pt.y * pt.y; // If the radius < 1, the point is within the clipped circle if ((xsquared + ysquared) < 1.0) return YES; return NO; }
根据图像的像位判断是否点击到图片,位图上面的触摸
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event { //如果点下去的点不在self.bounds里面直接返回NO if (!CGRectContainsPoint(self.bounds, point)) { return NO; } //如果这个点的中断值 ALPHA小于33%,就可以看做是透明了,但也要和实际情况 return (bytes[alphaOffset(point.x, point.y, self.image.size.width)] > 85); } //计算当前点在数像中的位的位置 //X,Y表示点的坐标,W表示当前VIEW的宽度 NSUInteger alphaOffset(NSUInteger x, NSUInteger y, NSUInteger w){ return y * w * 4 + x * 4 + 0; } //计算图像的位 unsigned char *getBitmapFromImage (UIImage *image) { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); if (colorSpace == NULL) { fprintf(stderr, "Error allocating color space\n"); return NULL; } CGSize size = image.size; // void *bitmapData = malloc(size.width * size.height * 4); unsigned char *bitmapData = calloc(size.width * size.height * 4, 1); // Courtesy of Dirk. Thanks! if (bitmapData == NULL) { fprintf (stderr, "Error: Memory not allocated!"); CGColorSpaceRelease(colorSpace); return NULL; } CGContextRef context = CGBitmapContextCreate (bitmapData, size.width, size.height, 8, size.width * 4, colorSpace, kCGImageAlphaPremultipliedFirst); CGColorSpaceRelease(colorSpace ); if (context == NULL) { fprintf (stderr, "Error: Context not created!"); free (bitmapData); return NULL; } CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height); CGContextDrawImage(context, rect, image.CGImage); unsigned char *data = CGBitmapContextGetData(context); CGContextRelease(context); return data; }
视图持久性和归档
//持久化 - (void) updateDefaults { NSMutableArray *colors = [[NSMutableArray alloc] init]; NSMutableArray *locs = [[NSMutableArray alloc] init]; for (DragView *dv in [[self.view viewWithTag:201] subviews]) { [colors addObject:dv.whichFlower]; [locs addObject:NSStringFromCGRect(dv.frame)]; } [[NSUserDefaults standardUserDefaults] setObject:colors forKey:@"colors"]; [[NSUserDefaults standardUserDefaults] setObject:locs forKey:@"locs"]; [[NSUserDefaults standardUserDefaults] synchronize]; [colors release]; [locs release]; } //删除 [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"colors"]; [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"locs"]; //加载 NSMutableArray *colors = [[NSUserDefaults standardUserDefaults] objectForKey:@"colors"]; NSMutableArray *locs = [[NSUserDefaults standardUserDefaults] objectForKey:@"locs"]; //归档 @interface DragView : UIImageView { CGPoint startLocation; NSString *whichFlower; } @property (retain) NSString *whichFlower; @end @implementation DragView @synthesize whichFlower; //保存 - (void) encodeWithCoder: (NSCoder *)coder { [coder encodeCGRect:self.frame forKey:@"viewFrame"]; [coder encodeObject:self.whichFlower forKey:@"flowerType"]; } //初始化 - (id) initWithCoder: (NSCoder *)coder { [super initWithFrame:CGRectZero]; self.frame = [coder decodeCGRectForKey:@"viewFrame"]; self.whichFlower = [coder decodeObjectForKey:@"flowerType"]; self.image = [UIImage imageNamed:self.whichFlower]; self.userInteractionEnabled = YES; return self; } //保存 NSArray *flowers = [[self.view viewWithTag:201] subviews]; [NSKeyedArchiver archiveRootObject:flowers toFile:DATAPATH]; //加载 NSArray *flowers = [NSKeyedUnarchiver unarchiveObjectWithFile:DATAPATH];
使用NSUndoManager来执行Undo和reDo操作
// Initialize the undo manager for this application self.undoManager = [[NSUndoManager alloc] init]; //设置UNDO次数 [self.undoManager setLevelsOfUndo:999]; //注册 在视图时同注册 [[self.undoManager prepareWithInvocationTarget:self] setPosition:self.center]; //然后是否正在undo while ([self.undoManager isUndoing]); // Don't show the undo button if the undo stack is empty //是否能undo if (!self.undoManager.canUndo) self.navigationItem.leftBarButtonItem = nil; else self.navigationItem.leftBarButtonItem = BARBUTTON(@"Undo", @selector(undo)); //最后再处理undo [self.undoManager undo];
预留
预留
预留
预留
预留
最新技术文章: