今晚用flash做了一个图片缩放功能,把它拿上来供以后参考!
步骤1:制作四个按钮,分别是刷新按钮,放大按钮,缩小按钮,原始尺寸按钮,排列在场景左上角;
步骤2:创建两个空影片,放到场景中,其中一个命名为pic(用来加载图片),另一个命名为trigger(用来触发为特定影片剪辑实例定义的动作);
步骤3:在场景第一帧添加如下脚本:
//显示图片
function picShow() {
//如果场景存在pic影片
with (_root.pic) {
//取得加载图片大小
_root.picWidth = _width;
_root.picHeight = _height;
}
//调用设置图片大小的方法
_root.picFitToStage();
//调用设置图片位置的方法
_root.picMoveToCenter();
//创建对象
stageListener = new Object();
//当调整swf大小时,重新定位图片大小和位置
stageListener.onResize = function() {
var _l1 = _root;
if (_l1.pic) {
_l1.picFitToStage();
_l1.picMoveToCenter();
}
};
Stage.addListener(stageListener);
}
//设置图片大小
function picFitToStage() {
with (_root.pic) {
_width = _root.picWidth;
_height = _root.picHeight;
//取得场景宽度
sWidth = Stage.width;
//设置导航条宽度为场景宽度
_root.hom._width = sWidth;
//取得场景高度(去掉导航条高度)
sHeight = Stage.height-menuHeight;
//使图片适应场景
if (_width>sWidth || _height>sHeight) {
if (_width/sWidth>=_height/sHeight) {
_height = _height/_width*sWidth;
_width = sWidth;
} else {
_width = _width/_height*sHeight;
_height = sHeight;
}
}
}
}
//刷新
function picRefresh() {
var _l1 = _root;
_l1.picFitToStage();
_l1.picMoveToCenter();
}
function picZoom(ratio) {
with (_root.pic) {
_x = _x-_width*(ratio/2-0.500000);
_y = _y-_height*(ratio/2-0.500000);
_width = _width*ratio;
_height = _height*ratio;
}
}
function picActualSize() {
with (_root.pic) {
_x = _x+(_width-_root.picWidth)/2;
_y = _y+(_height-_root.picHeight)/2;
_width = _root.picWidth;
_height = _root.picHeight;
}
}
//设置图片位置
function picMoveToCenter() {
var _l1 = _root;
_l1.pic._x = (Stage.width-_l1.pic._width)/2;
_l1.pic._y = (Stage.height-_l1.menuHeight-_l1.pic._height)/2+_l1.menuHeight;
}
//设置或检索用于影片剪辑的呈现品质
_quality = "BEST";
//指示 SWF 文件在 Flash Player 内当前的缩放比例
Stage.scaleMode = "noScale";
//指示 SWF 文件在播放器或浏览器中当前的对齐方式
Stage.align = "tl";
Stage.showMenu = false;
//图片链接地址
var picURL;
picURL = "1.jpg";
//picURL = decode(picURL, "3irjklsd8432uisdklvr892348");
//图片宽度
var picWidth = 0;
//图片高度
var picHeight = 0;
//导航条高度
var menuHeight = 30;
var isZooming = false;
//缩放系数
var picZoomRatio = 1;
var lastClickTime = 0;
var curToolTip = "";
//加载图片到影片pic
_root.pic.loadMovie("1.jpg");
步骤4:在各个按钮上添加脚本
1)刷新按钮添加如下脚本:
on (release) {
_root.picRefresh();
}
on (rollOver) {
_root.curToolTip = "刷新";
}
2)放大按钮添加如下脚本:
on (press) {
_root.isZooming = true;
_root.picZoomRatio = 1.100000;
}
on (release) {
_root.isZooming = false;
}
on (releaseOutside) {
//在鼠标指针位于按钮内部的情况下按下按钮,然后将鼠标指针移到该按钮外部并释放鼠标按钮
_root.isZooming = false;
}
on (rollOver) {
_root.curToolTip = "放大";
}
3)缩小按钮添加如下脚本:
on (press) {
_root.isZooming = true;
_root.picZoomRatio = 0.909091;
}
on (release) {
_root.isZooming = false;
}
on (releaseOutside) {
_root.isZooming = false;
}
on (rollOver) {
_root.curToolTip = "缩小";
}
4)原始大小按钮添加如下脚本:
on (release) {
picActualSize();
}
on (rollOver) {
_root.curToolTip = "原始尺寸";
}
步骤5:在swf同目录存放一张jpg图片,命名为1.jpg,现在就可以随意处置这张图片了!
好了,就这么多,下次再继续......