最近在制作flash時需要制作一個可以隨意設定寬度的按鈕組件.為了保證按鈕樣式不變形,一般都會采用將按鈕分成幾個mc的方法來制作.但其實,如果對按鈕的動態效果要求不多的話,完全可以使用Bitmap類來制作.下面是將按鈕的一個狀態分解開來.一般的思路是做為三個MC,然后在改變寬度是,動態改變中間mc的寬度就可以了。但是這樣的話,對于元素的制作就比較麻煩.使用bitmap類可以直接將一張圖片分為三個MC后生成.在批量使用時,效率提高了不少.

方法:
復制代碼 代碼如下:
/**
* date : 2007.2.6
* author : Frank
* site : http://www.2solo.net/log
*/
import flash.display.*;
import flash.geom.Rectangle;
import flash.geom.Point;
install_img("mT_over_bmp", 200, 158, 5, bmp_mc);
function install_img(image_url, center_width, face_width, bar_left, tar_mc) {
//image_url:目標圖片路徑
//center_width:整體寬度
//face_width:初始位圖寬度
//bar_left:邊側mc寬度
//tar_mc:所要加載的容器地址
///定義原始
var linkageId:String = image_url;
var myBD:BitmapData = BitmapData.loadBitmap(linkageId);
if (tar_mc == undefined || tar_mc == "") {
tar_mc = this;
}
//bmp_mc.attachBitmap(myBD, this.getNextHighestDepth());
face_width = face_width-2*bar_left;
///新建MC
tar_mc.center_mc.removeMovieClip();
tar_mc.left_mc.removeMovieClip();
tar_mc.right_mc.removeMovieClip();
var center_mc:MovieClip = tar_mc.createEmptyMovieClip("center_mc", tar_mc.getNextHighestDepth());
var left_mc:MovieClip = tar_mc.createEmptyMovieClip("left_mc", tar_mc.getNextHighestDepth());
var right_mc:MovieClip = tar_mc.createEmptyMovieClip("right_mc", tar_mc.getNextHighestDepth());
center_mc._x = bar_left;
left_mc._x = 0;
right_mc._x = center_width-bar_left;
///新建圖片數據
var myBD_C:BitmapData = new BitmapData(face_width, myBD.height, true, 0x00FF0000);
var myBD_L:BitmapData = new BitmapData(bar_left, myBD.height, true, 0x00FF0000);
var myBD_R:BitmapData = new BitmapData(bar_left, myBD.height, true, 0x00FF0000);
///拷貝圖片
myBD_C.copyPixels(myBD, new Rectangle(bar_left, 0, face_width, myBD.height), new Point(0, 0));
myBD_L.copyPixels(myBD, new Rectangle(0, 0, bar_left, myBD.height), new Point(0, 0));
myBD_R.copyPixels(myBD, new Rectangle(myBD.width-bar_left, 0, bar_left, myBD.height), new Point(0, 0));
//加載圖片
center_mc.attachBitmap(myBD_C, this.getNextHighestDepth());
left_mc.attachBitmap(myBD_L, this.getNextHighestDepth());
right_mc.attachBitmap(myBD_R, this.getNextHighestDepth());
///調整距離
center_mc._width = center_width-2*bar_left;
}
stop();