---
title: "android与j2me移植之clipRect"
author: "fonter"
date: "2009-10-16T11:38:34Z"
tags: []
slug: "android-j2me-cliprect"
draft: false
lastmod: "2009-10-21T15:23:26Z"
id: 492110
url: "https://fonter.iteye.com/blog/492110"
---

j2me移动到android时，你可能需要用到clipRect这个方法，我们用它来截取一定区域，让来看看j2me和android有什么不同吧

 

**Android code:**

```
canvas.save();//保存当前状态
canvas.clipRect(x,y, x+width, y+height)
cavnas.resave();//释放当前状态
```

 

**J2ME code:**

```
int clipX = g.getClipX();
int clipY = g.getClipY();
int clipWidth = g.getClipWidth();
int clipHeight = g.getClipHeight();
g.clipRect(x, y, width, height);
g.setClip(clipX, clipY, clipWidth, clipHeight);//释放当前状态
```

 

比较两段代码，Android比较简洁，要注意的是canvas.clipRect(left, top, right, bottom)，它们的坐标为left, top, right, bottom，所以要加上x,y，这一点在刚开始开发j2me转android时会发现有些区别，也容易忽略，像fillRect，drawRect等方法也和j2me有区别！

 

j2me需用setClip释放当前状态，而android不必！

 

[iWidsets公告](http://fonter.iteye.com/blog/498654)

[android调用平台功能](http://fonter.iteye.com/blog/498636)

[android Httpclient 设置timeout](http://fonter.iteye.com/blog/497309)

[android与j2me移植之clipRect](http://fonter.iteye.com/blog/492110)

[android混淆(Obfuscate)](http://fonter.iteye.com/blog/489728)

[How to destroy an Activity in android](http://fonter.iteye.com/blog/477769)

[android应用程序logcat命令](http://fonter.iteye.com/blog/477112)

[android开玩笑之创建xml文件](http://fonter.iteye.com/blog/477066)

[android和j2me之清屏(clearScreen)](http://fonter.iteye.com/blog/474922)

[android之view获取getWidth()和getHeight()](http://fonter.iteye.com/blog/474900)

[android之创建和读取自定义资源文件](http://fonter.iteye.com/blog/474584)

[android之显示Log](http://fonter.iteye.com/blog/474535)

[androd之绘制文本（FontMetrics）](http://fonter.iteye.com/blog/474526)