AS截屏的类的使用方法+示例
来源:互联网 发布:js 给日期字段赋值 编辑:程序博客网 时间:2024/05/05 03:41
1.首先先引入两个swc(aether,as3corelib)
http://code.google.com/p/aether/
http://code.google.com/p/as3corelib/
2.代码部分:
package {
import aether.utils.ScreenCapture;
import com.adobe.images.JPGEncoder;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.net.FileReference;
import flash.utils.ByteArray;
[SWF(width=800,height=600,backgroundColor=0xffffff)]
public class PrintScreen extends Sprite
{
private var bitmapData:BitmapData;
public function PrintScreen()
{
//画了一些随机的矢量图
graphics.lineStyle(1);
graphics.drawRect(0,0,100,100);
for(var i:int=0;i<30;i++){
var tempx:Number=Math.random()*400;
var tempy:Number=Math.random()*300;
var color:Number=0xff0000*Math.random();
graphics.lineStyle(1,color);
graphics.lineTo(tempx,tempy);
graphics.endFill();
}
//定义截屏的区域,并保存在bitmapData中
//false是位图是否有transparent项
var region:Rectangle= new Rectangle(0, 0, 100, 100);
bitmapData= ScreenCapture.capture(stage,false, region);
// 一个按钮,用于启动截屏
var saveBtn:MyButton=new MyButton("save",50,30);
saveBtn.x=700;
saveBtn.y=150;
addChild(saveBtn);
saveBtn.addEventListener(MouseEvent.CLICK,save);
}
private function save(e:MouseEvent):void{
var encoder:JPGEncoder = new JPGEncoder(80); //80是quality
var bytes:ByteArray= encoder.encode(bitmapData);
var fr:FileReference = new FileReference();
fr.save(bytes, "screen.jpg");
}
}
}
MyButton类
package {
import flash.display.*
import flash.text.*;
import flash.filters.DropShadowFilter;
public class MyButton extends SimpleButton {
// The text to appear on the button
private var _text:String;
// Save the width and height of the rectangle
private var _width:Number;
private var _height:Number;
public function MyButton( text:String, width:Number, height:Number ) {
// Save the values to use them to create the button states
_text = text;
_width = width;
_height = height;
// Create the button states based on width, height, and text value
upState = createUpState();
overState = createOverState();
downState = createDownState();
hitTestState = upState;
}
// Create the display object for the button's up state
private function createUpState():Sprite {
var sprite:Sprite = new Sprite();
var background:Shape = createdColoredRectangle( 0xcccccc );
var textField:TextField = createTextField( false );
sprite.addChild( background );
sprite.addChild( textField );
return sprite;
}
// Create the display object for the button's up state
private function createOverState():Sprite {
var sprite:Sprite = new Sprite();
var background:Shape = createdColoredRectangle( 0xff0000 );
var textField:TextField = createTextField( false );
sprite.addChild( background );
sprite.addChild( textField );
return sprite;
}
// Create the display object for the button's down state
private function createDownState():Sprite {
var sprite:Sprite = new Sprite();
var background:Shape = createdColoredRectangle( 0xCCCCCC );
var textField:TextField = createTextField( true );
sprite.addChild( background );
sprite.addChild( textField );
return sprite;
}
// Create a rounded rectangle with a specific fill color
private function createdColoredRectangle( color:uint ):Shape {
var rect:Shape = new Shape();
rect.graphics.lineStyle( 1, 0x000000 );
rect.graphics.beginFill( color );
rect.graphics.drawRect( 0, 0, _width, _height);
rect.graphics.endFill();
rect.filters = [ new DropShadowFilter( 2 ) ];
return rect;
}
// Create the text field to display the text of the button
private function createTextField( downState:Boolean ):TextField {
var textField:TextField = new TextField();
textField.text = _text;
textField.width = _width;
// Center the text horizontally
var format:TextFormat = new TextFormat();
format.align = TextFormatAlign.CENTER;
textField.setTextFormat( format );
// Center the text vertically
textField.y = ( _height - textField.textHeight ) / 2;
textField.y -= 2; // Subtract 2 pixels to adjust for offset
// The down state places the text down and to the right
// further than the other states
if ( downState ) {
textField.x += 1;
textField.y += 1;
}
return textField;
}
}
}
http://code.google.com/p/aether/
http://code.google.com/p/as3corelib/
2.代码部分:
package {
import aether.utils.ScreenCapture;
import com.adobe.images.JPGEncoder;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.net.FileReference;
import flash.utils.ByteArray;
[SWF(width=800,height=600,backgroundColor=0xffffff)]
public class PrintScreen extends Sprite
{
private var bitmapData:BitmapData;
public function PrintScreen()
{
//画了一些随机的矢量图
graphics.lineStyle(1);
graphics.drawRect(0,0,100,100);
for(var i:int=0;i<30;i++){
var tempx:Number=Math.random()*400;
var tempy:Number=Math.random()*300;
var color:Number=0xff0000*Math.random();
graphics.lineStyle(1,color);
graphics.lineTo(tempx,tempy);
graphics.endFill();
}
//定义截屏的区域,并保存在bitmapData中
//false是位图是否有transparent项
var region:Rectangle= new Rectangle(0, 0, 100, 100);
bitmapData= ScreenCapture.capture(stage,false, region);
// 一个按钮,用于启动截屏
var saveBtn:MyButton=new MyButton("save",50,30);
saveBtn.x=700;
saveBtn.y=150;
addChild(saveBtn);
saveBtn.addEventListener(MouseEvent.CLICK,save);
}
private function save(e:MouseEvent):void{
var encoder:JPGEncoder = new JPGEncoder(80); //80是quality
var bytes:ByteArray= encoder.encode(bitmapData);
var fr:FileReference = new FileReference();
fr.save(bytes, "screen.jpg");
}
}
}
MyButton类
package {
import flash.display.*
import flash.text.*;
import flash.filters.DropShadowFilter;
public class MyButton extends SimpleButton {
// The text to appear on the button
private var _text:String;
// Save the width and height of the rectangle
private var _width:Number;
private var _height:Number;
public function MyButton( text:String, width:Number, height:Number ) {
// Save the values to use them to create the button states
_text = text;
_width = width;
_height = height;
// Create the button states based on width, height, and text value
upState = createUpState();
overState = createOverState();
downState = createDownState();
hitTestState = upState;
}
// Create the display object for the button's up state
private function createUpState():Sprite {
var sprite:Sprite = new Sprite();
var background:Shape = createdColoredRectangle( 0xcccccc );
var textField:TextField = createTextField( false );
sprite.addChild( background );
sprite.addChild( textField );
return sprite;
}
// Create the display object for the button's up state
private function createOverState():Sprite {
var sprite:Sprite = new Sprite();
var background:Shape = createdColoredRectangle( 0xff0000 );
var textField:TextField = createTextField( false );
sprite.addChild( background );
sprite.addChild( textField );
return sprite;
}
// Create the display object for the button's down state
private function createDownState():Sprite {
var sprite:Sprite = new Sprite();
var background:Shape = createdColoredRectangle( 0xCCCCCC );
var textField:TextField = createTextField( true );
sprite.addChild( background );
sprite.addChild( textField );
return sprite;
}
// Create a rounded rectangle with a specific fill color
private function createdColoredRectangle( color:uint ):Shape {
var rect:Shape = new Shape();
rect.graphics.lineStyle( 1, 0x000000 );
rect.graphics.beginFill( color );
rect.graphics.drawRect( 0, 0, _width, _height);
rect.graphics.endFill();
rect.filters = [ new DropShadowFilter( 2 ) ];
return rect;
}
// Create the text field to display the text of the button
private function createTextField( downState:Boolean ):TextField {
var textField:TextField = new TextField();
textField.text = _text;
textField.width = _width;
// Center the text horizontally
var format:TextFormat = new TextFormat();
format.align = TextFormatAlign.CENTER;
textField.setTextFormat( format );
// Center the text vertically
textField.y = ( _height - textField.textHeight ) / 2;
textField.y -= 2; // Subtract 2 pixels to adjust for offset
// The down state places the text down and to the right
// further than the other states
if ( downState ) {
textField.x += 1;
textField.y += 1;
}
return textField;
}
}
}
- AS截屏的类的使用方法+示例
- 剪贴板的使用方法示例
- GetOpenFilename的使用方法和示例
- UITableView的使用方法和示例
- assert 的使用方法及示例
- udev的使用方法(示例)
- System.Net.Sockets的使用方法示例。
- QtSQL连接查询SQLite3的使用方法示例
- mvc中json格式的使用方法示例
- PHP中CURL_MULTI的使用方法简单示例
- C++ 标准模板vecter的使用方法示例
- QSS的使用方法和示例程序
- PHPExcel的使用方法和代码示例
- caffe的python接口使用方法示例
- oracle with table as 创建临时表的用法示例
- swift 中 as as! as? 的区别
- swift as as?as!的用法
- swift as/as?/as!的区别
- 常用正则表达式大全
- 禁止鼠标多次点击选中div中的文字
- C#实现透明窗体
- 写大论文的一些注意事项
- php防注入
- AS截屏的类的使用方法+示例
- Android 根据屏幕大小设置字体
- 自己弄了个MVC框架 24yt-mvc
- Ubuntu server10.4下NIS服务的配置
- 利用proc 实现内核和用户态交换数据
- 魔术数字游戏
- 强连通分量的三种算法
- 每天读一遍,不久你就会变!
- WEB Servlet细节