当前位置: 编程技术>移动开发
本页文章导读:
▪关于canvas的save()及restore()步骤 关于canvas的save()及restore()方法
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.........
▪ UIPopoverController的施用 UIPopoverController的使用
UIPopoverController只能在ipad设备上面使用;作用是用于显示临时内容,特点是总是显示在当前视图最前端,当单击界面的其他地方时自动消失。UIViewController* vCtrl = [[UIVie.........
▪ IPAD 拖动事例(也支持鼠标) IPAD 拖动例子(也支持鼠标)
<html><head> <title>iPad Touch Test</title> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"> <script src="http://ajax.googleapis.com/ajax/libs.........
[1]关于canvas的save()及restore()步骤
来源: 互联网 发布时间: 2014-02-18
关于canvas的save()及restore()方法
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.SurfaceView; public class MyView1 extends SurfaceView { private Bitmap mBitmap; private Paint mPaint; public MyView1(Context context, AttributeSet attrs) { super(context, attrs); mPaint = new Paint(); this.initBitmap(); } private void initBitmap() { mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); } @Override public void draw(Canvas canvas) { super.draw(canvas); mPaint.setColor(Color.BLUE); canvas.drawRect(100, 200, 200, 300, mPaint); canvas.save(); canvas.rotate(45); mPaint.setColor(Color.RED); canvas.drawRect(150, 10, 200, 60, mPaint); canvas.restore(); mPaint.setColor(Color.GREEN); canvas.drawRect(200, 10, 250, 100, mPaint); } }
上图代码绘制出的view如下图:
如掉save和restore方法(38和42行),意味着绿色的图形也受到38和42行之间,对canvas操作的代码影响,也会旋转,如下图:
[2] UIPopoverController的施用
来源: 互联网 发布时间: 2014-02-18
UIPopoverController的使用
UIPopoverController只能在ipad设备上面使用;作用是用于显示临时内容,特点是总是显示在当前视图最前端,当单击界面的其他地方时自动消失。
UIViewController* vCtrl = [[UIViewController alloc] init]; UIPopoverController*popController = [[UIPopoverController alloc] initWithContentViewController:vCtrl]; popController.popoverContentSize = CGSizeMake(320,480); CGrect rect =CGRectMake(0,0,2,2); [popController presentPopoverFromRect:rect inView:aView permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
在使用该控制器的时候,下面几个步骤是必须的:
1.UIPopoverController该控制器的内容必须由一个控制器提供;提供方式有三:
- (id)initWithContentViewController:(UIViewController *)viewController
@property (nonatomic, retain) UIViewController *contentViewController
- (void)setContentViewController:(UIViewController *)viewController animated:(BOOL)animated
2.设置箭头方向:
@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection
3.设置内容大小:
@property (nonatomic) CGSize popoverContentSize
备注:
<1>.如果从一个导航按钮处呈现,使用:
presentPopoverFromBarButtonItem:permittedArrowDirections:animated:;
如果要从一个视图出呈现,使用:
presentPopoverFromRect:inView:permittedArrowDirections:animated:
<2>.如果设备旋转以后,位置定位错误需要在父视图控制器的下面方法里面重新定位:
didRotateFromInterfaceOrientation:(在这个方法体里面重新设置rect)
然后再次调用:
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
例子:
TodoViewController *contentViewController = [[TodoViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:contentViewController];
navigationController.contentSizeForViewInPopover = CGSizeMake(100, 100); //内容大小
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
popover.popoverContentSize = CGSizeMake(300, 300); //弹出窗口大小,如果屏幕画不下,会挤小的。这个值默认是320x1100
CGRect popoverRect = CGRectMake(200, 700, 10, 10);
[popover presentPopoverFromRect:popoverRect //popoverRect的中心点是用来画箭头的,如果中心点如果出了屏幕,系统会优化到窗口边缘
inView:self.view //上面的矩形坐标是以这个view为参考的
permittedArrowDirections:UIPopoverArrowDirectionDown //箭头方向
animated:YES];
[contentViewController release];
[navigationController release];
//最佳实践,使用哪个view做参考,就以哪个view的bounds送进去就好了,箭头自动指向这个view的中心
UIPopoverController只能在ipad设备上面使用;作用是用于显示临时内容,特点是总是显示在当前视图最前端,当单击界面的其他地方时自动消失。
UIViewController* vCtrl = [[UIViewController alloc] init]; UIPopoverController*popController = [[UIPopoverController alloc] initWithContentViewController:vCtrl]; popController.popoverContentSize = CGSizeMake(320,480); CGrect rect =CGRectMake(0,0,2,2); [popController presentPopoverFromRect:rect inView:aView permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
在使用该控制器的时候,下面几个步骤是必须的:
1.UIPopoverController该控制器的内容必须由一个控制器提供;提供方式有三:
- (id)initWithContentViewController:(UIViewController *)viewController
@property (nonatomic, retain) UIViewController *contentViewController
- (void)setContentViewController:(UIViewController *)viewController animated:(BOOL)animated
2.设置箭头方向:
@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection
3.设置内容大小:
@property (nonatomic) CGSize popoverContentSize
备注:
<1>.如果从一个导航按钮处呈现,使用:
presentPopoverFromBarButtonItem:permittedArrowDirections:animated:;
如果要从一个视图出呈现,使用:
presentPopoverFromRect:inView:permittedArrowDirections:animated:
<2>.如果设备旋转以后,位置定位错误需要在父视图控制器的下面方法里面重新定位:
didRotateFromInterfaceOrientation:(在这个方法体里面重新设置rect)
然后再次调用:
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
例子:
TodoViewController *contentViewController = [[TodoViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:contentViewController];
navigationController.contentSizeForViewInPopover = CGSizeMake(100, 100); //内容大小
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
popover.popoverContentSize = CGSizeMake(300, 300); //弹出窗口大小,如果屏幕画不下,会挤小的。这个值默认是320x1100
CGRect popoverRect = CGRectMake(200, 700, 10, 10);
[popover presentPopoverFromRect:popoverRect //popoverRect的中心点是用来画箭头的,如果中心点如果出了屏幕,系统会优化到窗口边缘
inView:self.view //上面的矩形坐标是以这个view为参考的
permittedArrowDirections:UIPopoverArrowDirectionDown //箭头方向
animated:YES];
[contentViewController release];
[navigationController release];
//最佳实践,使用哪个view做参考,就以哪个view的bounds送进去就好了,箭头自动指向这个view的中心
[3] IPAD 拖动事例(也支持鼠标)
来源: 互联网 发布时间: 2014-02-18
IPAD 拖动例子(也支持鼠标)
<html>
<head>
<title>iPad Touch Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<div id="trace"></div>
<div id="test">
<div onmousemove="flip(event);" >Blue</div>
</div>
</body>
</html>
<script language=javascript >
$.fn.draggable = function(action) {
var offset = null;
//touch drag
var start = function(e) {
var orig = e.originalEvent;
var pos = $(this).position();
offset = {
x: orig.changedTouches[0].pageX - pos.left,
y: orig.changedTouches[0].pageY - pos.top
};
};
var move = function(e) {
e.preventDefault();
var orig = e.originalEvent;
$(this).css({
top: orig.changedTouches[0].pageY - offset.y,
left: orig.changedTouches[0].pageX - offset.x
});
};
// mouse drag
o = document.getElementById($(this).attr("id"));
o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
o.orig_index = o.style.zIndex;
var drag = function(a)
{
this.style.cursor = "move";
var d=document;
if(!a)a=window.event;
var x = a.clientX+d.body.scrollLeft-o.offsetLeft;
var y = a.clientY+d.body.scrollTop-o.offsetTop;
if(o.setCapture)
o.setCapture();
else if(window.captureEvents)
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
d.onmousemove = function(a)
{
if(!a)a=window.event;
o.style.left = a.clientX+document.body.scrollLeft-x;
o.style.top = a.clientY+document.body.scrollTop-y;
o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
}
d.onmouseup = function()
{
if(o.releaseCapture)
o.releaseCapture();
else if(window.captureEvents)
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
d.onmousemove = null;
d.onmouseup = null;
d.ondragstart = null;
d.onselectstart = null;
d.onselect = null;
o.style.cursor = "normal";
o.style.zIndex = o.orig_index;
}
}
if (action == "disabled")
{
this.unbind("touchstart", start);
this.unbind("touchmove", move);
this.unbind("mousedown", drag);
o.style.cursor = "normal";
}
else
{
this.bind("touchstart", start);
this.bind("touchmove", move);
this.bind("mousedown", drag);
}
};
function flip(e)
{
$("body").css("background","blue");
e.stopPropagation();
}
$("#test").draggable();
function trace(msg)
{
$("#trace").append( "<br>" + msg) ;
}
</script>
<html>
<head>
<title>iPad Touch Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<div id="trace"></div>
<div id="test">
<div onmousemove="flip(event);" >Blue</div>
</div>
</body>
</html>
<script language=javascript >
$.fn.draggable = function(action) {
var offset = null;
//touch drag
var start = function(e) {
var orig = e.originalEvent;
var pos = $(this).position();
offset = {
x: orig.changedTouches[0].pageX - pos.left,
y: orig.changedTouches[0].pageY - pos.top
};
};
var move = function(e) {
e.preventDefault();
var orig = e.originalEvent;
$(this).css({
top: orig.changedTouches[0].pageY - offset.y,
left: orig.changedTouches[0].pageX - offset.x
});
};
// mouse drag
o = document.getElementById($(this).attr("id"));
o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
o.orig_index = o.style.zIndex;
var drag = function(a)
{
this.style.cursor = "move";
var d=document;
if(!a)a=window.event;
var x = a.clientX+d.body.scrollLeft-o.offsetLeft;
var y = a.clientY+d.body.scrollTop-o.offsetTop;
if(o.setCapture)
o.setCapture();
else if(window.captureEvents)
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
d.onmousemove = function(a)
{
if(!a)a=window.event;
o.style.left = a.clientX+document.body.scrollLeft-x;
o.style.top = a.clientY+document.body.scrollTop-y;
o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
}
d.onmouseup = function()
{
if(o.releaseCapture)
o.releaseCapture();
else if(window.captureEvents)
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
d.onmousemove = null;
d.onmouseup = null;
d.ondragstart = null;
d.onselectstart = null;
d.onselect = null;
o.style.cursor = "normal";
o.style.zIndex = o.orig_index;
}
}
if (action == "disabled")
{
this.unbind("touchstart", start);
this.unbind("touchmove", move);
this.unbind("mousedown", drag);
o.style.cursor = "normal";
}
else
{
this.bind("touchstart", start);
this.bind("touchmove", move);
this.bind("mousedown", drag);
}
};
function flip(e)
{
$("body").css("background","blue");
e.stopPropagation();
}
$("#test").draggable();
function trace(msg)
{
$("#trace").append( "<br>" + msg) ;
}
</script>
最新技术文章: