cairo学习笔记二(初级入门)
Tofloor
poster avatar
z85525006
deepin
2012-02-20 06:00
Author
歌词显示
  1. #!/usr/bin/env python
  2. # coding:utf-8
  3. import cairo
  4. import gtk
  5. #本篇讲述如何处理文本。
  6. #老男孩
  7. #第一个示例是在 GTK+ 窗口中显示《老男孩》的部分歌词。
  8. #原作者: http://liyanrui.is-programmer.com/2010/4/7/cairo-text.16643.html
  9. class App(object):
  10.     def __init__(self):
  11.         #抛砖引玉,歌词就靠你发挥了.
  12.         window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  13.         window.set_position(gtk.WIN_POS_CENTER)
  14.         window.set_title("老男孩歌词显示")
  15.       
  16.         window.set_position(gtk.WIN_POS_CENTER)
  17.         window.set_app_paintable(True)
  18.       
  19.         window.set_default_size(300, 300)
  20.         window.connect("destroy", lambda w:gtk.main_quit())      
  21.         window.connect_after("expose-event",
  22.                              lambda w,e:self.on_expose_event(w,e))   
  23.         
  24.         self.angle = 0
  25.         self.scale = 1
  26.         self.delta = 0.01
  27.         window.set_opacity(0.9)
  28.         window.set_app_paintable(True)
  29.         window.show_all()
  30.         
  31.     def on_expose_event(self, widget, event):
  32.         
  33.         cr = widget.window.cairo_create()
  34.         
  35.         cr.set_source_rgb(0.1, 0.1, 0.1)
  36.         cr.select_font_face("Purisa",
  37.                             cairo.FONT_SLANT_NORMAL,
  38.                             cairo.FONT_WEIGHT_BOLD)
  39.         #这里设置字体。这个函数接受了三个字体参数的传入,字体的名称(倾斜)、样式与轻重
  40.         cr.set_font_size(23)
  41.         
  42.         cr.move_to(20, 30)
  43.         cr.set_source_rgb(1,0,0)
  44.         cr.show_text("那")
  45.         
  46.         cr.set_source_rgb(0, 0, 0)
  47.         cr.move_to(45,30)
  48.         cr.show_text("是我日夜思念深深爱着的人啊")
  49.         cr.move_to(20, 60)
  50.         cr.show_text("到底我该如何表达 她会接受我吗")
  51.         
  52.         cr.move_to(20, 90)
  53.         cr.set_source_rgb(1, 0,0)
  54.         cr.show_text("也许永远都不会跟她说出那句")
  55.         cr.set_source_rgb(1,1,1)
  56.         cr.move_to(22,90)
  57.         cr.show_text("也许永远都不会跟她说出那句话")
  58.         
  59.         return True
  60. App()
  61. gtk.main()
Copy the Code

一个字一个字显示
  1. #!/usr/bin/env python
  2. # coding:utf-8
  3. #邱海龙就是一个菜鸟,垃圾.滚吧...
  4. import cairo
  5. import gtk
  6. text = ["L", "i", "n", "u", "x", "d", "e"]
  7. timer = True
  8. class App(object):
  9.     def __init__(self):
  10.         
  11.         window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  12.         window.set_position(gtk.WIN_POS_CENTER)
  13.         window.set_title("一个字接一个字... ...")
  14.       
  15.         window.set_position(gtk.WIN_POS_CENTER)
  16.         window.set_app_paintable(True)
  17.       
  18.         window.set_default_size(300, 300)
  19.         window.connect("destroy", lambda w:gtk.main_quit())      
  20.         window.connect_after("expose-event",
  21.                              lambda w,e:self.on_expose_event(w,e))   
  22.         gtk.timeout_add(1000, self.time_handler, window)
  23.         self.count = 0
  24.         window.set_app_paintable(True)
  25.         window.show_all()
  26.    
  27.     def time_handler(self, widget):
  28.         widget.queue_draw()
  29.         if not timer: return False
  30.         return True
  31.    
  32.     def on_expose_event(self, widget, event):
  33.         cr = widget.window.cairo_create()
  34.         
  35.         cr.select_font_face("Courier",
  36.                             cairo.FONT_SLANT_NORMAL,
  37.                             cairo.FONT_WEIGHT_BOLD)
  38.         
  39.         cr.set_font_size(35)
  40.         cr.set_source_rgb(0.2, 0.2, 0.2)
  41.         
  42.         self.x = 0
  43.         for i in range(0,self.count):
  44.             extents = cr.text_extents(text[i])
  45.             print extents
  46.             width = extents[2]
  47.             width += 2
  48.             self.x += width
  49.             cr.move_to(self.x+30, 50)
  50.             cr.show_text(text[i])      
  51.             
  52.             
  53.         self.count += 1
  54.         
  55.         if self.count == 8:
  56.             timer = False
  57.             self.count = 0
  58.             
  59.         return True
  60. App()
  61. gtk.main()
Copy the Code
将上面一个个字显示和上面的例子合体,就能实现那样的 歌词 动过去的....
==================
膨胀

下面这个示例中,我们制造了一种膨胀的效果。这个示例显示了一串在膨胀的居中文本,并且伴有淡出现象。这是很常见的效果,在 flash 动画里经常见到。
  1. #!/usr/bin/env python
  2. # coding:utf-8
  3. import cairo
  4. import gtk
  5. text = ["z", "e", "t", "c", "o", "d", "e"]
  6. timer = True
  7. class App(object):
  8.     def __init__(self):
  9.         
  10.         window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  11.         window.set_position(gtk.WIN_POS_CENTER)
  12.         window.set_title("字体膨胀效果")
  13.       
  14.         window.set_position(gtk.WIN_POS_CENTER)
  15.         window.set_app_paintable(True)
  16.       
  17.         window.set_default_size(300, 300)
  18.         window.connect("destroy", lambda w:gtk.main_quit())      
  19.         window.connect_after("expose-event",
  20.                              lambda w,e:self.on_expose_event(w,e))   
  21.         self.alpha = 1.0
  22.         self.size  = 1
  23.         
  24.         gtk.timeout_add(14, self.time_handler, window)
  25.         
  26.         window.set_app_paintable(True)
  27.         window.show_all()
  28.    
  29.     def on_expose_event(self, widget, event):        
  30.         
  31.         cr = widget.window.cairo_create()
  32.         
  33.         
  34.         #获取窗口中心坐标
  35.         x = widget.allocation.width /2
  36.         y = widget.allocation.height /2
  37.         cr.set_source_rgb(0.5, 0, 0)
  38.         cr.paint()
  39.         #将背景设为暗红色
  40.         cr.select_font_face("Courier",
  41.                             cairo.FONT_SLANT_NORMAL,
  42.                             cairo.FONT_WEIGHT_BOLD)
  43.         
  44.         self.size += 0.8#每论循环,字号增长0.8个单位
  45.         
  46.         if self.size > 20:
  47.             self.alpha -= 0.01
  48.         
  49.         cr.set_font_size(self.size)
  50.         cr.set_source_rgb(1, 1, 1)
  51.         
  52.         extents = cr.text_extents("ZetCode")
  53.         cr.move_to(x-extents[2]/2, y)
  54.         
  55.         #获取文本的路径,并将其设为当前的裁剪域
  56.         cr.text_path("ZetCode")
  57.         cr.clip()
  58.         #绘制当前的路径,添加alpha值,谈出效果
  59.         cr.stroke()
  60.         cr.paint_with_alpha(self.alpha)
  61.       
  62.         if self.alpha <= 0:
  63.             self.alpha = 1.0
  64.             self.size = 1
  65.         return True
  66.    
  67.     def time_handler(self, widget):
  68.         if not timer:
  69.             return False
  70.         widget.queue_draw()
  71.         return True
  72.    
  73. App()
  74. gtk.main()
Copy the Code

在这一篇里,要讲述图像的处理。先是演示如何在 GTK+ 窗口中显示一幅图像,然后再制造一些特效。


图像的显示

在第一个例子里,显示了一幅图像。

  1. #!/usr/bin/env python
  2. # coding:utf-8
  3. import cairo
  4. import gtk
  5. class App(object):
  6.     def __init__(self):
  7.         
  8.         window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  9.         window.set_position(gtk.WIN_POS_CENTER)
  10.         window.set_title("图形的显示")
  11.       
  12.         window.set_position(gtk.WIN_POS_CENTER)
  13.         window.set_app_paintable(True)
  14.         self.image = cairo.ImageSurface.create_from_png("linuxdeepin.png")
  15.         window.set_default_size(300, 300)
  16.         window.connect("destroy", lambda w:gtk.main_quit())      
  17.         window.connect_after("expose-event",
  18.                              lambda w,e:self.on_expose_event(w,e))   
  19.       
  20.         window.set_app_paintable(True)
  21.         
  22.         window.show_all()
  23.    
  24.     def on_expose_event(self, widget, event):        
  25.         cr = widget.window.cairo_create()
  26.         cr.set_source_surface(self.image, 10, 10)
  27.         cr.paint()
  28.         return True
  29. App()
  30. gtk.main()
Copy the Code
  1. [/code]linuxdeepin.png
  2. ======================================================
  3. 垂帘效果(Blind Down)
  4. 在下面的代码示例中,要垂帘显示图片,就像拉下窗帘的那种效果。
  5. [code]
  6. #!/usr/bin/env python
  7. # coding:utf-8
  8. import cairo
  9. import gtk
  10. timer = True
  11. class App(object):
  12.     def __init__(self):
  13.         
  14.         window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  15.         window.set_position(gtk.WIN_POS_CENTER)
  16.         window.set_title("垂帘效果")
  17.       
  18.         window.set_position(gtk.WIN_POS_CENTER)
  19.         window.set_app_paintable(True)
  20.         #图片
  21.         self.image = cairo.ImageSurface.create_from_png("linuxdeepin.png")
  22.         gtk.timeout_add(15, self.time_handler, window)
  23.         
  24.         self.angle = 0
  25.         self.image_width = 0
  26.         self.image_height = 0
  27.         self.w = 0
  28.         self.h = 0
  29.         
  30.         window.set_default_size(300, 300)
  31.         window.connect("destroy",
  32.                        lambda w:gtk.main_quit())
  33.         window.connect_after("expose-event",
  34.                              lambda w,e:self.on_expose_event(w,e))   
  35.         window.set_app_paintable(True)
  36.         window.show_all()
  37.    
  38.     def on_expose_event(self, widget, event):        
  39.         cr = widget.window.cairo_create()
  40.         width, height = widget.get_size()
  41.         
  42.         self.image_width = self.image.get_width()
  43.         self.image_height = self.image.get_height()
  44.         
  45.         self.w = self.image_width
  46.         #创建一个图像外观,并通过它构造那个于图形相关联的cairo环境
  47.         surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
  48.                                      self.image_width,
  49.                                      self.image_height)
  50.         
  51.         ic = cairo.Context(surface)
  52.         #在初始的空图形中绘制一个矩形,它在循环显示h+1,
  53.         #采用这种方式创建的图像在后面要作为蒙板来用
  54.         ic.rectangle(0, 0, self.w, self.h)
  55.         ic.fill()
  56.         
  57.         self.h += 1
  58.         #slef.h和图片高度相等的时候,呵呵呵呵,你知道的.
  59.         if self.h == self.image_height:
  60.             self.h = 1
  61.             #timer = False
  62.         
  63.         #深度图片被设置为要绘制的源,并采用surface的alpha通道作为
  64.         #蒙板来绘制这个源
  65.         cr.set_source_surface(self.image, 10, 10)
  66.         cr.mask_surface(surface, 10, 10)
  67.         
  68.         # cr,ic
  69.         # 声明两个 cairo环境, 一个与gtkWindow相关联,另一个和图片相关联
  70.         #蒙板 就是在Linux图标在撒上一层细沙
  71.         #细沙把底图遮盖住,它就相当于蒙版
  72.         #如果你想把底图的一部分显现出来,就可以用手指(相当于使用蒙版时的画笔,且前景为黑)把细沙划去一些,如果想再把显出来的一部分盖住则又可以在上面撒上一层沙子(相当于前景色设为白色,用画笔涂沫)t
  73.         return True
  74.    
  75.     def time_handler(self, widget):
  76.         if not timer:
  77.             return False
  78.         widget.queue_draw()
  79.         return True
  80.         
  81. App()
  82. gtk.main()
Copy the Code

百叶窗效果
  1. #!/usr/bin/env python
  2. # coding:utf-8
  3. import cairo
  4. import gtk
  5. timer = True
  6. class App(object):
  7.     def __init__(self):
  8.         
  9.         window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  10.         window.set_position(gtk.WIN_POS_CENTER)
  11.         window.set_title("百叶窗口")
  12.       
  13.         window.set_app_paintable(True)
  14.         #图片
  15.         self.image = cairo.ImageSurface.create_from_png("linuxdeepin.png")
  16.         gtk.timeout_add(400, self.time_handler, window)
  17.         
  18.         self.angle = 0
  19.         self.image_width = 0
  20.         self.image_height = 0
  21.         self.w = 0
  22.         self.h = 0
  23.         self.count = 0
  24.         window.set_default_size(300, 300)
  25.         window.connect("destroy",
  26.                        lambda w:gtk.main_quit())
  27.         window.connect_after("expose-event",
  28.                              lambda w,e:self.on_expose_event(w,e))   
  29.         window.show_all()
  30.    
  31.     #百叶窗口
  32.     def on_expose_event(self, widget, event):        
  33.         cr = widget.window.cairo_create()
  34.         width, height = widget.get_size()
  35.         
  36.         surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
  37.                                      self.image_width,
  38.                                      self.image_height)
  39.         
  40.         self.image_width = self.image.get_width()
  41.         self.image_height =self.image.get_height()
  42.         
  43.         self.w = self.image_width
  44.         
  45.         ic = cairo.Context(surface)
  46.         
  47.         for i in range(0, self.image_height+1,7):
  48.             for j in range(0, self.count):
  49.                 ic.move_to(0, i+j)
  50.                 ic.line_to(self.w, i+j)
  51.                
  52.         ic.stroke()
  53.         self.count += 1
  54.         if self.count == 8: self.count = 0
  55.         
  56.         cr.set_source_surface(self.image, 10, 10)
  57.         cr.mask_surface(surface, 10, 10)
  58.         #这个 百叶原理很简单.
  59.         # 就是在蒙板上 每间隔 7 ,就从0 到 图片宽度.高度是i+j.
  60.         #然后每400毫秒 就加1,慢慢的就形成了 百叶效果t
  61.         return True
  62.    
  63.     def time_handler(self, widget):
  64.         if not timer:
  65.             return False
  66.         widget.queue_draw()
  67.         return True
  68.         
  69. App()
  70. gtk.main()
Copy the Code
Reply Favorite View the author
All Replies

No replies yet