AndroidDev 板


LINE

我想在某个 custom view 里进行绘图,而且不想让绘图超出 view 的边界,所以我在 layout file 中对此 view 的 parent ViewGroup 设定 android:clipChildren="true",这招有用,但遇到 canvas.clipPath() 指令就破功了,不知如何解决? 相关档案有3个:TestActivity.java,TestView.java,test.xml (layout)。 主要内容如下: [TestActivity.java] public class TestActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); } } [TestView.java] public class TestView extends View { public TestView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { // build a triangle Path object Path pie = new Path(); pie.moveTo(-50, 50); pie.lineTo(360, 50); pie.lineTo(150, 400); pie.close(); // build a paint brush Paint paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(20); paint.setColor(Color.RED); // draw green triangle and red line canvas.save(); canvas.clipPath(pie, Region.Op.REPLACE); canvas.drawColor(Color.GREEN); canvas.drawLine(-30, 100, 330, 100, paint); canvas.restore(); // draw another blue line paint.setColor(Color.BLUE); canvas.drawLine(-30, 250, 330, 250, paint); } } [test.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" android:clipChildren="true" android:background="#cccccc"> <RelativeLayout android:layout_width="200dp" android:layout_height="350dp" android:background="#ffffff" android:layout_centerInParent="true" android:clipChildren="true"> <com.example.TestView android:id="@+id/testView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffdd" android:layout_centerInParent="true"/> </RelativeLayout> </RelativeLayout> 执行画面像这样:http://ppt.cc/q7Vn TestView 的背景是淡黄色,它的 parent 有设定 android:clipChildren="true", 这的确发挥了作用,便得蓝线在超出 TestView 的边界以外的部分不会被画出来。 但红线就不同了,由於 canvas.clipPath() 挖出一块超出 TestView 的边界的 三角形区域,使得之後的填绿色以及画红线都超出了 TestView 的边界。 想当然尔的解法是,在建立 Path 的时候要小心别超出 TestView 的边界, 或者将 Path 和 TestView 的范围进行 Region.Op.INTERSECT, 但由於种种原因,我无法这麽做,不知有什麽方法可以在呼叫 canvas.clipPath() 之後可以随意绘图而不超出 view 的边界? 感谢。 --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 218.187.81.101
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/AndroidDev/M.1399656834.A.124.html
1F:推 tac0wu:有趣 来研究一下 05/10 12:49
2F:推 tac0wu:你不能仰赖parent the clip来做intersect clip 你必须要自 05/10 14:07
3F:推 tac0wu:http://pastebin.com/L1aftPEp 05/10 14:09
4F:→ tac0wu:在没看android source 下的不负责猜测是 因为你内层的 05/10 14:10
5F:→ tac0wu:clip Region已经把parent给你的clip给override掉了 05/10 14:10
6F:→ tac0wu:所以你内层要自己做一下intersect clip region 05/10 14:11
7F:→ tac0wu:如何做 就看我附上code的第二段 05/10 14:12
8F:推 tac0wu:如果要贴code还是用一下置底贴code的网站 05/10 14:16
9F:→ csixty:感谢。你的code很有效。 ^__^ 05/10 18:14
10F:→ csixty:原来,Path可以被Region来约束啊。 05/10 18:16
11F:推 wanilly:小心用clipPath 因为HW加速下performance不怎麽好 05/11 15:39







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP