Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 在画布上设置图像列表的动画_Image_Animation_Racket - Fatal编程技术网

Image 在画布上设置图像列表的动画

Image 在画布上设置图像列表的动画,image,animation,racket,Image,Animation,Racket,我尝试使用以下代码在画布上显示图像列表。但是,在运行整个“for”循环并在帧中显示最终效果之前,将显示一个空帧,并且不会显示任何图像 #lang racket/gui (require 2htdp/image) (define frame (new frame% [label "Example"] [width 500] [height 500])) (send frame s

我尝试使用以下代码在画布上显示图像列表。但是,在运行整个“for”循环并在帧中显示最终效果之前,将显示一个空帧,并且不会显示任何图像

#lang racket/gui
(require 2htdp/image)

(define frame (new frame%
                   [label "Example"]
                   [width  500]
                   [height 500]))

(send frame show #t) 
; (sleep 1)    ; tried this to allow time for frame to show properly; does not help; 

(new canvas% [parent frame]
     [paint-callback
      (lambda (canvas dc)
        (for ((i imglist))      ; imglist is a list of images to be displayed @ 1/second.
          (send dc clear)
          (send dc draw-bitmap 
                     (image->bitmap i) 
                     20 20)
          ; (send dc flush)     ; this statement also does not help;
          (sleep 1)             ; to show animation effect from list of images;
          ))])
图像->位图功能来自:;发件人:


问题在哪里?如何解决?

绘制回调旨在快速更新画布,然后返回。 当它返回时,系统知道画布已更新

一种方法是:1)引入一个参数,用于保存当前显示的图像。2) 进行
绘制回调
绘制当前图像。3) 创建一个单独的线程,每秒更改当前图像

注意:下面我在
image->bitmap
中为宽度和高度添加了+1。圆圈的边缘被切掉了

#lang racket/gui
(require 2htdp/image)

(define images (list (circle 30 "outline" "red")
                     (circle 20 "outline" "red")
                     (circle 10 "outline" "red")
                     (circle  5 "outline" "red")))

(define current-image (make-parameter (first images)))

(define (image->bitmap image)   
  (let* ([width  (+ (image-width  image) 1)]
         [height (+ (image-height image) 1)]
         [bm     (make-bitmap width height)]
         [dc     (send bm make-dc)])
    (send dc clear)
    (send image draw dc 0 0 0 0 width height 0 0 #f)
    bm))

(define frame (new frame%
                   [label "Example"]
                   [width  500]
                   [height 500]))

(define canvas (new canvas% [parent frame]
                    [paint-callback
                     (lambda (canvas dc)
                       (send dc clear)
                       (send dc draw-bitmap (image->bitmap (current-image)) 20 20))]))

(send frame show #t) 

(thread (λ ()
          (let loop ([is images])
            (cond
              [(null? is) (loop images)]
              [else       (current-image (first is))
                          (send canvas on-paint)
                          (sleep 1)
                          (loop (rest is))]))))
以下工作:

(define images (list (circle 30 "outline" "red")
                     (circle 20 "outline" "red")
                     (circle 10 "outline" "red")
                     (circle  5 "outline" "red")))

(define (image->bitmap image)  
  (let* ([width (image-width image)]
         [height (image-height image)]
         [bm (make-bitmap width height)]
         [dc (send bm make-dc)])
    (send dc clear)
    (send image draw dc 0 0 0 0 width height 0 0 #f)
    bm))

(define frame (new frame% [label "Frame"] [width 300] [height 300]))
(define canvas (new canvas% [parent frame]))
(define dc (send canvas get-dc))

(send frame show #t)
(sleep/yield 1)

(let loop ()  
  (for ((i images))
    (send dc clear)
    (send dc draw-bitmap
          (image->bitmap i)
          20 20)
    (sleep 0.5))
  (loop))

但是,显示动画的帧不会关闭,但必须从IDE停止。

是的,它工作得很好。我发现我也可以使用“(send canvas get dc)”来获取dc对象,然后在其上以无休止的循环绘制位图,但是帧关闭按钮不起作用,必须从DrRacket IDE停止该过程。使用线程的解决方案没有这样的问题。
(define images (list (circle 30 "outline" "red")
                     (circle 20 "outline" "red")
                     (circle 10 "outline" "red")
                     (circle  5 "outline" "red")))

(define (image->bitmap image)  
  (let* ([width (image-width image)]
         [height (image-height image)]
         [bm (make-bitmap width height)]
         [dc (send bm make-dc)])
    (send dc clear)
    (send image draw dc 0 0 0 0 width height 0 0 #f)
    bm))

(define frame (new frame% [label "Frame"] [width 300] [height 300]))
(define canvas (new canvas% [parent frame]))
(define dc (send canvas get-dc))

(send frame show #t)
(sleep/yield 1)

(let loop ()  
  (for ((i images))
    (send dc clear)
    (send dc draw-bitmap
          (image->bitmap i)
          20 20)
    (sleep 0.5))
  (loop))