Perl 显示动画图像

Perl 显示动画图像,perl,perltk,Perl,Perltk,我希望图像在我的窗口中上下移动。现在我应该在代码中添加更多内容吗???Tk::Animation只负责Gif文件的动画。在本例中,动画意味着始终更改帧。因此,移动仅限于图像内容本身 如果要在画布上整体移动图像,必须使用move方法。当然,这可以与gif动画结合使用 下面是一个gif从左向右移动的示例: use warnings; use Tk; use Tk::Animation; my $scr = new MainWindow; $scr->configure(-backgroun

我希望图像在我的窗口中上下移动。现在我应该在代码中添加更多内容吗???

Tk::Animation只负责Gif文件的动画。在本例中,动画意味着始终更改帧。因此,移动仅限于图像内容本身

如果要在画布上整体移动图像,必须使用move方法。当然,这可以与gif动画结合使用

下面是一个gif从左向右移动的示例:

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;
#!perl
严格使用;
使用警告;
使用传统知识;
使用Tk::动画;
my$mw=main窗口->新建();
$mw->configure(-background=>“black”);
$mw->geometry(“200x100”);
我的$canvas=$mw->canvas(
-宽度=>200,
-高度=>100,
-背景=>“黑色”,
)->打包(
-expand=>1,
-fill=>“两者”,
);
my$image=$mw->动画(
-格式=>'gif',
-文件=>'oi.gif',
#请用这个:http://images1.wikia.nocookie.net/vaultarmory/images/2/23/Gif_dancinggir.gif
);
#--绘图时背景清晰透明
$image->set\u disposition\u方法(1);
我的$id\u在\u画布中的\u图像\u=$canvas->createImage(
50, 50,
-image=>$image,
);
$image->start_动画(80);
#--存储当前多视图方向
我的$direction='moving2left';
$mw->重复(600,\&在画布中移动项目);
$mw->MainLoop();
出口(0);
子移动\u画布中的\u项目\u{
#--获取当前位置
my($x1,$y1,$x2,$y2)=$canvas->bbox($id\u of_canvas中的_图像_);
#--计算是向左还是向右移动
my$min_left=0;
我的$max_right=200;
if($direction eq'moving2left'&&$x1>$min\u left){
#继续向左移动
$canvas->move($canvas中图像的id\u,-10,0);
}elsif($direction eq'moving2left'&&$x1 move($id\u图像在画布中的位置,10,0);
}elsif($x2<$max\u right)方向均衡器'moving2right'和&$x2<$max\u right){
#右移
$canvas->move($canvas中图像的id\u,10,0);
}elsif($方向均衡器'moving2right'和&$x2>=$max\u right){
#改变方向,向左移动
$direction='moving2left';
$canvas->move($canvas中图像的id\u,-10,0);
}否则{
死亡('错误:不知道在这种情况下该怎么办');
}
回来
}#/move_画布中的项目
#!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