当前位置:  编程技术>移动开发
本页文章导读:
    ▪Create a mutable Bit地图        Create a mutable Bitmap Bitmap photo = BitmapFactory.decodeByteArray(data, 0,data.length);Bitmap mutablePhoto = Bitmap.createScaledBitmap(photo, 480, 320,false);photo.recycle(); ......
    ▪ 施用BinCompiler将资源文件打包成二进制文件        使用BinCompiler将资源文件打包成二进制文件   转载 http://yarin.iteye.com/blog/546923# 文章分类:移动开发 关键字: bincompiler、bin文件、资源打包、j2me 版权申明:http://yarin.iteye.com/blog/453262     .........
    ▪ 自个儿写的一个图片展示       自己写的一个图片展示 MediaComponentView.h // // MediaComponentView.h // MediaComponent // // Created by Jason Wang on 10/7/10. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> @interface Me.........

[1]Create a mutable Bit地图
    来源: 互联网  发布时间: 2014-02-18
Create a mutable Bitmap
Bitmap photo = BitmapFactory.decodeByteArray(data, 0,data.length);

Bitmap mutablePhoto = Bitmap.createScaledBitmap(photo, 480, 320,false);

photo.recycle();

    
[2] 施用BinCompiler将资源文件打包成二进制文件
    来源: 互联网  发布时间: 2014-02-18
使用BinCompiler将资源文件打包成二进制文件

 

转载 http://yarin.iteye.com/blog/546923#
文章分类:移动开发 关键字: bincompiler、bin文件、资源打包、j2me

版权申明:http://yarin.iteye.com/blog/453262  

 

在开发游戏时,总是要使用很多的资源文件,比如:图片、音乐等。而我们经常会遇到一些商业游戏中都看不到这些资源文件,那是因为商业游戏,一般都会将这些资源文件打包成二进制的文件,然后在程序中读取,并使用。这样的游戏看上去更显得专业一些,本文我们就来学习一个最简单的将资源文件打包成二进制文件的方法——使用BinCompiler将资源文件打包成二进制文件。

 

所需工具:BinCompiler(见附件)

 

运行“BinCompiler.exe”,指定要打包的资源文件的路径,和输出二进制文件的路径,如下图所示。


点击create按钮,即在我们制定的位置产生一个bin文件,当然在这个bin文件所在目录还会产生一个index.txt文件。我们在程序中读取这些资源时,需要使用这个index.txt。index.txt文件如下所示:

Java代码
  • FName   Index   Pos Size   
  • A_04.png     0     0     4141   
  • A_03.png     1     4145      3802   
  • A_02.png     2     7951      3813   
  • A_01.png     3     11768     3959   
  • FName	Index	Pos	Size
    A_04.png	0	0	4141
    A_03.png	1	4145	3802
    A_02.png	2	7951	3813
    A_01.png	3	11768	3959

    接下来我们可以使用BinReader.java文件中的两个方法来读取这些资源文件了。

    代码清单:BinReader.java

    Java代码
  • /*******************************************************************************  
  •  * Reads a file from the BIN file and return data as a byte buffer  
  •  *******************************************************************************/   
  • public   byte [] readFile(String binfile,  int  pos)   
  • {   
  •      byte  buffer[];   
  •      int  len;   
  •   
  •      try  {   
  •   
  •         InputStream is = Class.getClass().getResourceAsStream( "/"  + binfile);   
  •   
  •         is.skip(pos);   
  •   
  •         len  = (is.read() &  0xFF ) <<  24 ;   
  •         len  |= (is.read()  0xFF ) <<  16 ;   
  •         len  |= (is.read() &  0xFF ) <<  8 ;   
  •         len  |= (is.read() &  0xFF );   
  •   
  •         buffer =  new   byte [len];   
  •        
  •         is.read(buffer,  0 , buffer.length);   
  •   
  •         is.close();   
  •         is =  null ;   
  •        
  •         System.gc();   
  •     }  catch  (Exception e) {   
  •         buffer =  null ;   
  •         e.printStackTrace();   
  •         System.gc();   
  •          return   null ;   
  •     }   
  •   
  •      return  buffer;   
  • }   
  •   
  • /*******************************************************************************  
  •  * Reads a file from the BIN file and return data as an Image  
  •  *******************************************************************************/   
  • public  Image readImage(String binfile,  long  pos)   
  • {   
  •      byte  buffer[];   
  •      long  len;   
  •   
  •      try  {   
  •         InputStream is = Class.getClass().getResourceAsStream( "/"  + binfile);   
  •            
  •         is.skip(pos);   
  •            
  •         len  = (is.read() &  0xFF ) <<  24 ;   
  •         len  |= (is.read()  0xFF ) <<  16 ;   
  •         len  |= (is.read() &  0xFF ) <<  8 ;   
  •         len  |= (is.read() &  0xFF );   
  •   
  •         buffer =  new   byte [len];   
  •        
  •         is.read(buffer,  0 , buffer.length);   
  •   
  •         is.close();   
  •         is =  null ;   
  •        
  •         System.gc();   
  •     }  catch  (Exception e) {   
  •         buffer =  null ;   
  •         e.printStackTrace();   
  •         System.gc();   
  •          return   null ;   
  •     }   
  •   
  •      return  Image.createImage(buffer,  0 , buffer.length);   
  • }  
  • /*******************************************************************************
     * Reads a file from the BIN file and return data as a byte buffer
     *******************************************************************************/
    public byte[] readFile(String binfile, int pos)
    {
    	byte buffer[];
    	int len;
    
    	try {
    
    		InputStream is = Class.getClass().getResourceAsStream("/" + binfile);
    
    		is.skip(pos);
    
    		len  = (is.read() & 0xFF) << 24;
    		len  |= (is.read() 0xFF) << 16;
    		len  |= (is.read() & 0xFF) << 8;
    		len  |= (is.read() & 0xFF);
    
    		buffer = new byte[len];
    	
    		is.read(buffer, 0, buffer.length);
    
    		is.close();
    		is = null;
    	
    		System.gc();
    	} catch (Exception e) {
    		buffer = null;
    		e.printStackTrace();
    		System.gc();
    		return null;
    	}
    
    	return buffer;
    }
    
    /*******************************************************************************
     * Reads a file from the BIN file and return data as an Image
     *******************************************************************************/
    public Image readImage(String binfile, long pos)
    {
    	byte buffer[];
    	long len;
    
    	try {
    		InputStream is = Class.getClass().getResourceAsStream("/" + binfile);
    		
    		is.skip(pos);
    		
    		len  = (is.read() & 0xFF) << 24;
    		len  |= (is.read() 0xFF) << 16;
    		len  |= (is.read() & 0xFF) << 8;
    		len  |= (is.read() & 0xFF);
    
    		buffer = new byte[len];
    	
    		is.read(buffer, 0, buffer.length);
    
    		is.close();
    		is = null;
    	
    		System.gc();
    	} catch (Exception e) {
    		buffer = null;
    		e.printStackTrace();
    		System.gc();
    		return null;
    	}
    
    	return Image.createImage(buffer, 0, buffer.length);
    }

    可以看出,这两个方法都只需要传入bin文件名和图片对应的pos,pos值就在我们上面所说的index.txt文件中去找对应的就可以了。 

    例如我们读取一个图片

    Java代码
  • Image image = readimage( "images.bin" ,  0 );  
  • Image image = readimage("images.bin", 0);

    可以看出,我们传入的pos的值是0,对应index.txt中就应该是A_04.png。 

    很简单,可以直接使用就是了。谢谢大家支持。


        
    [3] 自个儿写的一个图片展示
        来源: 互联网  发布时间: 2014-02-18
    自己写的一个图片展示
    MediaComponentView.h

    //
    //  MediaComponentView.h
    //  MediaComponent
    //
    //  Created by Jason Wang on 10/7/10.
    //  Copyright 2010 __MyCompanyName__. All rights reserved.
    //
    #import <UIKit/UIKit.h>
    
    
    @interface MediaComponentView : UIView <UITableViewDelegate,UITableViewDataSource> {
    	NSArray *images;
    	
    	NSUInteger index;
    	
    	CGRect imageViewRect;
    	
    	//UITableView *_tableView;
    }
    
    @property(nonatomic,readonly) NSUInteger index; 
    
    - (id)initWithFrame:(CGRect)frame images:(NSArray *)imagesAry;
    - (void)show;
    
    @end


    MediaComponentView.m

    //
    //  MediaComponentView.m
    //  MediaComponent
    //
    //  Created by Jason Wang on 10/7/10.
    //  Copyright 2010 __MyCompanyName__. All rights reserved.
    //
    
    #define MAINVIEW_PER 0.75
    #define LISTVIEW_PER 0.25
    #define TABLE_GAP_HEIGHT_PER 0.1
    
    #define MAIN_GAP_WIDTH 3
    #define IMAGE_GAP_WIDTH 8
    
    
    #define TABLE_HEIGHT 0.8
    
    #import "MediaComponentView.h"
    #import "MediaComponentCell.h"
    
    @interface MediaComponentView()
    - (void)showMainImage:(NSInteger)_index;
    - (void)showNewImage:(id)sender;
    @end
    
    
    @implementation MediaComponentView
    
    @synthesize index;
    
    - (id)initWithFrame:(CGRect)frame images:(NSArray *)imagesAry{
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code here.
    		images = [[NSArray alloc] initWithArray:imagesAry];
    		index = 0;
        }
        return self;
    }
    
    - (void)drawRect:(CGRect)dirtyRect {
        // Drawing code here.
    	
    }
    
    - (void)show {
    	CGRect rect = self.frame;
    	self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
    	if ([images count] > 0) {
    		index = 0;
    		
    		//Get the first image in images arry
    		UIImage *image = [UIImage imageNamed:[[images objectAtIndex:index] objectForKey:@"image"]];
    		NSString *comment = [[images objectAtIndex:index] objectForKey:@"comment"];
    		
    		UIView *mainImageView = [[UIView alloc] initWithFrame:CGRectMake(MAIN_GAP_WIDTH, MAIN_GAP_WIDTH, rect.size.width - MAIN_GAP_WIDTH * 2 , rect.size.height - MAIN_GAP_WIDTH * 2)];
    		mainImageView.backgroundColor = [UIColor whiteColor];
    		CGRect mainImageViewRect = mainImageView.frame;
    		mainImageView.tag = 1;
    		
    		//init the UIImageView
    		UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    		//set the tag to 1, in order to get the UIImageView later
    		imageView.tag = 2;
    		imageView.contentMode = UIViewContentModeScaleAspectFit; 
    		imageView.frame = CGRectMake(IMAGE_GAP_WIDTH, IMAGE_GAP_WIDTH, mainImageViewRect.size.width - IMAGE_GAP_WIDTH * 2, mainImageViewRect.size.height - IMAGE_GAP_WIDTH * 2);
    		imageViewRect = imageView.frame;
    		
    		if ([images count] > 1) {
    			mainImageViewRect = CGRectMake(MAIN_GAP_WIDTH, MAIN_GAP_WIDTH, rect.size.width * MAINVIEW_PER - MAIN_GAP_WIDTH, rect.size.height - MAIN_GAP_WIDTH * 2);
    			imageViewRect = CGRectMake(IMAGE_GAP_WIDTH, IMAGE_GAP_WIDTH, mainImageViewRect.size.width - IMAGE_GAP_WIDTH * 2, mainImageViewRect.size.height - IMAGE_GAP_WIDTH * 2);
    			
    			UIView *listView = [[UIView alloc] initWithFrame:CGRectMake(rect.size.width * MAINVIEW_PER + IMAGE_GAP_WIDTH, MAIN_GAP_WIDTH, rect.size.width * LISTVIEW_PER - IMAGE_GAP_WIDTH * 2, rect.size.height - MAIN_GAP_WIDTH * 2)];
    			listView.backgroundColor = [UIColor clearColor];
    			CGRect listViewRect = listView.frame;
    			
    			UITableView *_tableView = [[UITableView alloc] initWithFrame:CGRectMake(IMAGE_GAP_WIDTH, listViewRect.size.height * TABLE_GAP_HEIGHT_PER, listViewRect.size.width - IMAGE_GAP_WIDTH * 2, listViewRect.size.height * (1 - TABLE_GAP_HEIGHT_PER * 2)) style:UITableViewStylePlain];
    			_tableView.backgroundColor = [UIColor blackColor];	
    			_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    			_tableView.dataSource = self;
    			_tableView.delegate = self;
    			
    			[listView addSubview:_tableView];
    			[_tableView release]; 
    			
    			[self addSubview:listView];
    			[listView release];
    		}
    		
    		if ((comment && ![comment isEqualToString:@""]) || [images count] > 1) {
    			imageViewRect = CGRectMake(imageViewRect.origin.x, imageViewRect.origin.y, imageViewRect.size.width, imageViewRect.size.height * 7 / 8 - IMAGE_GAP_WIDTH);
    			
    			CGRect commentViewRect = CGRectMake(imageViewRect.origin.x, imageViewRect.origin.y + imageViewRect.size.height + IMAGE_GAP_WIDTH, imageViewRect.size.width, imageViewRect.size.height / 8);
    			
    			UITextView * textView = [[UITextView alloc] initWithFrame:commentViewRect];
    			textView.textAlignment = UITextAlignmentCenter;
    			textView.text = comment;
    			textView.editable = NO;
    			textView.backgroundColor = [UIColor clearColor];
    			textView.font = [UIFont systemFontOfSize:16];
    			textView.tag = 3;
    			[mainImageView addSubview:textView];
    			[textView release];
    		}
    		mainImageView.frame = mainImageViewRect;
    		imageView.frame= imageViewRect;
    		
    		[mainImageView addSubview:imageView];
    		[imageView release];
    		
    		[self addSubview:mainImageView];
    		[mainImageView release];
    	}
    }
    
    - (void)showMainImage:(NSInteger)_index {
    	UIView *mainImageView = [self viewWithTag:1];
    	UIImageView *imageView = (UIImageView *)[mainImageView viewWithTag:2];
    	imageViewRect = imageView.frame;
    	[UIView beginAnimations:nil context:NULL];
    	[UIView setAnimationDuration:0.5f];
    	[UIView setAnimationDelegate:self];
    	[UIView setAnimationDidStopSelector:@selector(showNewImage:)];
    	imageView.frame = CGRectMake(imageViewRect.origin.x, imageViewRect.origin.y, imageViewRect.size.width, 0);
    	[UIView commitAnimations];
    }
    
    - (void)showNewImage:(id)sender {
    	UIView *mainImageView = [self viewWithTag:1];
    	UITextView *textView = (UITextView *)[mainImageView viewWithTag:3];
    	textView.text = [[images objectAtIndex:index] objectForKey:@"comment"];
    	UIImageView *imageView = (UIImageView *)[mainImageView viewWithTag:2];
    	NSString *imagePath = [[images objectAtIndex:index] objectForKey:@"image"];
    	[imageView setImage:[UIImage imageNamed:imagePath]];
    	[UIView beginAnimations:nil context:NULL];
    	[UIView setAnimationDuration:0.5f];
    	imageView.frame = imageViewRect;
    	[UIView commitAnimations];
    }
    
    #pragma mark UITableView
    
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    	MediaComponentCell *cell = (MediaComponentCell *)[tableView cellForRowAtIndexPath:indexPath];
    	[cell deSelectedNow];
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    	return self.frame.size.width * LISTVIEW_PER - IMAGE_GAP_WIDTH * 4;
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    	return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
    	return [images count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    	static NSString * CellIdentifier = @"Cell";
    	MediaComponentCell *cell = (MediaComponentCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    	if (cell == nil) {
    		cell = [[[MediaComponentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    	}
    	[cell setData:[[images objectAtIndex:indexPath.row] objectForKey:@"image"] index:indexPath.row];
    	cell.selectionStyle = UITableViewCellSelectionStyleNone;
    	cell.delegateView = self;
    	return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    	if (index == indexPath.row) return;
    	index = indexPath.row;
    	MediaComponentCell *cell = (MediaComponentCell *)[tableView cellForRowAtIndexPath:indexPath];
    	[cell selectedNow];
    	if (index != 0) {
    		MediaComponentCell *firstCell = (MediaComponentCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    		[firstCell deSelectedNow]; 
    	}
    	[self showMainImage:indexPath.row];
    }
    
    - (void)dealloc {
    	[images release];
    	[super dealloc];
    }
    
    @end
    


    MediaComponentCell.h

    //
    //  MediaComponentCell.h
    //  MediaComponent
    //
    //  Created by Jason Wang on 10/7/10.
    //  Copyright 2010 __MyCompanyName__. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    
    @interface MediaComponentCell : UITableViewCell {
    	NSString *imageFile;
    	
    	NSUInteger cellAtIndex;
    	
    	id delegateView;
    }
    
    @property(nonatomic,assign) id delegateView;
    
    - (void)setData:(NSString *)imagePath index:(NSUInteger)_index;
    
    - (void)selectedNow;
    
    - (void)deSelectedNow;
    
    @end


    MediaComponentCell.m

    //
    //  MediaComponentCell.m
    //  MediaComponent
    //
    //  Created by Jason Wang on 10/7/10.
    //  Copyright 2010 __MyCompanyName__. All rights reserved.
    //
    
    #import "MediaComponentCell.h"
    #import "MediaComponentView.h"
    
    @interface MediaComponentCell()
    - (UIImage *)convertImageToGrayScale:(UIImage *)image;
    @end
    
    
    
    @implementation MediaComponentCell
    
    @synthesize delegateView;
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
        if ((self = [super initWithStyle:style  reuseIdentifier:reuseIdentifier])) {
            // Initialization code
    		
        }
        return self;
    }
    
    - (void)layoutSubviews {
    	[super layoutSubviews];
    	UIImageView *iView = (UIImageView *)[self.contentView viewWithTag:11];
    	
    	CGRect contentRect = self.contentView.frame;
    	iView.frame = CGRectMake(contentRect.origin.x + 2, contentRect.origin.y + 2, contentRect.size.width - 4, contentRect.size.height - 4);
    }
    
    - (void)setData:(NSString *)imagePath index:(NSUInteger)_index {
    	UIView *contentView = self.contentView;
    	cellAtIndex = _index;
    	if (imageFile) {
    		[imageFile release];
    		imageFile = nil;
    	}
    	imageFile = [[NSString alloc] initWithString:imagePath];
    	UIImage *mainImage = [UIImage imageNamed:imageFile];
    	MediaComponentView *view = (MediaComponentView *)delegateView;
    	if (view.index != cellAtIndex) {
    		mainImage = [self convertImageToGrayScale:mainImage];
    	}
    	UIImageView *iView = (UIImageView *)[contentView viewWithTag:11];
    	if (!iView) {
    		iView = [[UIImageView alloc] initWithImage:mainImage];
    		iView.contentMode = UIViewContentModeScaleAspectFit;
    		iView.tag = 11;
    		[contentView addSubview:iView];
    		[iView release];
    	} else {
    		[iView setImage:mainImage];
    	}
    
    }
    
    - (void)selectedNow {
    	UIImage *mainImage = [UIImage imageNamed:imageFile];
    	UIImageView *iView = (UIImageView *)[self.contentView viewWithTag:11];
    	[iView setImage:mainImage];
    }
    
    - (void)deSelectedNow {
    	UIImage *origionalImage = [UIImage imageNamed:imageFile];
    	UIImage *mainImage = [self convertImageToGrayScale:origionalImage];
    	UIImageView *iView = (UIImageView *)[self.contentView viewWithTag:11];
    	[iView setImage:mainImage];
    }
    
    - (UIImage *)convertImageToGrayScale:(UIImage *)image {
    	CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
    	
    	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    	
    	CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
    	
    	CGContextDrawImage(context, imageRect, [image CGImage]);
    	
    	CGImageRef imageRef = CGBitmapContextCreateImage(context);
    	
    	UIImage *newImage = [UIImage imageWithCGImage:imageRef];
    	
    	CGColorSpaceRelease(colorSpace);
    	CGContextRelease(context);
    	CFRelease(imageRef);
    	
    	return newImage;
    }
    
    
    - (void)dealloc {
    	[imageFile release]; 
        [super dealloc];
    }
    
    
    @end
    

        
    最新技术文章:
    ▪Android开发之登录验证实例教程
    ▪Android开发之注册登录方法示例
    ▪Android获取手机SIM卡运营商信息的方法
    ▪Android实现将已发送的短信写入短信数据库的...
    ▪Android发送短信功能代码
    ▪Android根据电话号码获得联系人头像实例代码
    ▪Android中GPS定位的用法实例
    ▪Android实现退出时关闭所有Activity的方法
    ▪Android实现文件的分割和组装
    ▪Android录音应用实例教程
    ▪Android双击返回键退出程序的实现方法
    ▪Android实现侦听电池状态显示、电量及充电动...
    ▪Android获取当前已连接的wifi信号强度的方法
    ▪Android实现动态显示或隐藏密码输入框的内容
    ▪根据USER-AGENT判断手机类型并跳转到相应的app...
    ▪Android Touch事件分发过程详解
    ▪Android中实现为TextView添加多个可点击的文本
    ▪Android程序设计之AIDL实例详解
    ▪Android显式启动与隐式启动Activity的区别介绍
    ▪Android按钮单击事件的四种常用写法总结
    ▪Android消息处理机制Looper和Handler详解
    ▪Android实现Back功能代码片段总结
    ▪Android实用的代码片段 常用代码总结
    ▪Android实现弹出键盘的方法
    ▪Android中通过view方式获取当前Activity的屏幕截...
    ▪Android显式启动与隐式启动Activity的区别介绍 iis7站长之家
    ▪Android提高之多方向抽屉实现方法
    ▪Android提高之MediaPlayer播放网络音频的实现方法...
    ▪Android提高之MediaPlayer播放网络视频的实现方法...
    ▪Android提高之手游转电视游戏的模拟操控
     


    站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,