2012-03-12 3 views
1
use warnings; 
use Tk; 
use Tk::Animation; 

my $scr = new MainWindow; 

$scr->configure(-background=>"black"); 
$scr->geometry("200x100"); 

my $canvas = $scr->Canvas(-width,200,-height,100,-background=>"black") 
       ->pack(-expand,1,-fill,'both'); 

my $image = $scr->Animation('-format' => 'gif', -file=>"help.gif"); 

$canvas->createImage(50,50, -image=> $image); 
$image->start_animation(500); 

MainLoop; 

이미지를 내 창에서 위아래로 움직여야합니다. 이제이 코드에 무엇을 더 추가해야합니까 ??애니메이션 이미지 표시

답변

0

Tk :: Animation은 Gif 파일의 애니메이션에만 책임이 있습니다. 이 경우 애니메이션은 프레임을 항상 변경한다는 의미입니다. 따라서 이미지 콘텐츠 자체로 움직임이 제한됩니다.

이미지를 캔버스에서 전체적으로 이동하려면 move 메서드를 사용해야합니다. 물론 이것은 gif 애니메이션과 결합 될 수 있습니다.

#!perl 

use strict; 
use warnings; 
use Tk; 
use Tk::Animation; 

my $mw = MainWindow->new(); 
$mw->configure(-background=>"black"); 
$mw->geometry("200x100"); 

my $canvas = $mw->Canvas(
    -width => 200, 
    -height => 100, 
    -background => 'black', 
)->pack(
    -expand => 1, 
    -fill => 'both', 
); 

my $image = $mw->Animation(
    -format => 'gif', 
    -file => 'oi.gif', 
    # please use this one: http://images1.wikia.nocookie.net/vaultarmory/images/2/23/Gif_dancinggir.gif 
); 

# -- clear transparent background while drawing 
$image->set_disposal_method(1); 

my $id_of_image_in_canvas = $canvas->createImage(
    50, 50, 
    -image=> $image, 
); 
$image->start_animation(80); 

# -- store the current mving direction 
my $direction = 'moving2left'; 
$mw->repeat(600, \&move_item_in_canvas); 

$mw->MainLoop(); 
exit(0); 


sub move_item_in_canvas { 
    # -- get current location 
    my ($x1, $y1, $x2, $y2) = $canvas->bbox($id_of_image_in_canvas); 

    # -- compute if to move left or right 
    my $min_left = 0; 
    my $max_right = 200; 
    if($direction eq 'moving2left' && $x1 > $min_left) { 
     # continue moving left 
     $canvas->move($id_of_image_in_canvas, -10, 0); 

    }elsif($direction eq 'moving2left' && $x1 <= $min_left) { 
     # change direction, move to the right 
     $direction = 'moving2right'; 
     $canvas->move($id_of_image_in_canvas, 10, 0); 

    }elsif($direction eq 'moving2right' && $x2 < $max_right) { 
     # move right 
     $canvas->move($id_of_image_in_canvas, 10, 0); 

    }elsif($direction eq 'moving2right' && $x2 >= $max_right){ 
     # change direction, move to the left 
     $direction = 'moving2left'; 
     $canvas->move($id_of_image_in_canvas, -10, 0); 

    }else{ 
     die('Error: don\'t know what to do in this case.'); 
    } 

    return; 
} # /move_item_in_canvas 
: 여기

GIF는 왼쪽에서 오른쪽으로 움직이는 예는