问题描述
如何在屏幕上特定的大小和位置绘制PNG图片?
调整图像大小
公共恩codeDIMAGE sizeImage(恩codeDIMAGE形象,诠释的宽度,
INT高度){
恩codeDIMAGE结果= NULL; INT currentWidthFixed32 = Fixed32.toFP(image.getWidth());
INT currentHeightFixed32 = Fixed32.toFP(image.getHeight()); INT requiredWidthFixed32 = Fixed32.toFP(宽);
INT requiredHeightFixed32 = Fixed32.toFP(高度); INT scaleXFixed32 = Fixed32.div(currentWidthFixed32,
requiredWidthFixed32);
INT scaleYFixed32 = Fixed32.div(currentHeightFixed32,
requiredHeightFixed32); 结果= image.scaleImage32(scaleXFixed32,scaleYFixed32);
返回结果;
}
这个功能将在下面的code使用。
您只需画图像
让漆在一个表中的方式9图像时,图像的大小不同,但我们将其调整为80×80,并给他们的利润率10像素。
假设你在你的项目资源有9 PNG图像。
- 加载图像
- 调整图像
- 在每个油漆事件Paint图像,某个位置内
code:
类血肌酐扩展MainScreen {
INT mImgWidth = 80;
INT mImgHeight = 80;
INT mImgMargin = 10;
串的文件名[] = {1.png,2.png,3.png,4.png,5.png,
6.png,7.png,8.png,9.png};
恩codeDIMAGE [] mImages; 公共SCR(){
超();
prepareImages();
} 私人无效prepareImages(){
mImages =新恩codeDIMAGE [fileNames.length]
的for(int i = 0; I< fileNames.length;我++){
恩codeDIMAGE图像= EN codeDIMAGE
.getEn codedImageResource(文件名[I]);
mImages [I] = sizeImage(图像,mImgWidth,mImgHeight);
}
} 保护无效漆(图形图像){
paintImages(图形);
super.paint(图形);
} 私人无效paintImages(图形图像){
INT scrWidth = Display.getWidth();
INT列= scrWidth /(mImgWidth + 2 * mImgMargin);
INT行= mImages.length /列
+(mImages.length%列大于0?1:0);
的for(int i = 0; I<行;我++){
对于(INT J = 0; J<列; J ++){
INT POSX = j的*(mImgWidth + 2 * mImgMargin)+ mImgMargin;
INT波西= I *(mImgHeight + 2 * mImgMargin)+ mImgMargin;
恩codeDIMAGE图像= mImages [我*列+ J]。
graphics.drawImage(POSX,波西,mImgWidth,mImgHeight,
图像,0,0,0);
}
}
}
}
您只需画图片 - 优化
看看血肌酐paint()方法。在每次刷新时图像的整个表重新粉刷,这意味着,每次油漆9的drawImage调用。如果我们只是采取什么这个表的shapshot和涂料使用它()方法?
类ScrOpt扩展MainScreen {
INT mScrWidth = Display.getWidth();
INT mScrHeight = Display.getHeight();
INT mImgWidth = 80;
INT mImgHeight = 80;
INT mImgMargin = 10;
串的文件名[] = {1.png,2.png,3.png,4.png,5.png,
6.png,7.png,8.png,9.png};
恩codeDIMAGE [] mImages;
位图mImgTable; 公共ScrOpt(){
超();
prepareImages();
mImgTable = paintImages();
} 私人无效prepareImages(){
mImages =新恩codeDIMAGE [fileNames.length]
的for(int i = 0; I< fileNames.length;我++){
恩codeDIMAGE图像= EN codeDIMAGE
.getEn codedImageResource(文件名[I]);
mImages [I] = sizeImage(图像,mImgWidth,mImgHeight);
}
} 私人位图paintImages(){
位图的结果=新位图(mScrWidth,mScrHeight);
图形图形=新的图形(结果);
INT scrWidth = mScrWidth;
INT列= scrWidth /(mImgWidth + 2 * mImgMargin);
INT行= mImages.length /列
+(mImages.length%列大于0?1:0);
的for(int i = 0; I<行;我++){
对于(INT J = 0; J<列; J ++){
INT POSX = j的*(mImgWidth + 2 * mImgMargin)+ mImgMargin;
INT波西= I *(mImgHeight + 2 * mImgMargin)+ mImgMargin;
恩codeDIMAGE图像= mImages [我*列+ J]。
graphics.drawImage(POSX,波西,mImgWidth,mImgHeight,图像,0,
0,0);
}
}
返回结果;
} 保护无效漆(图形图像){
super.paint(图形);
graphics.drawBitmap(0,0,mScrWidth,mScrHeight,mImgTable,0,0);
}
}
您可以更otimize它,using paintBackground()方法
使用BitmapField
所有上述是关于使用直接画图像屏幕Graphics.有时它伟大的 - 当你想显示一些动画或背景图片。但是,如果你想要保持标准UI用户体验,并使用图像作为一个字段?
海峡办法就是BitmapField
类ScrBmpField扩展MainScreen {
INT mImgWidth = 80;
INT mImgHeight = 80;
INT mImgMargin = 10;
串的文件名[] = {1.png,2.png,3.png,4.png,5.png,
6.png,7.png,8.png,9.png};
BitmapField [] mBmpFields; 公共ScrBmpField(){
超(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
prepareBmpFields();
} 私人无效prepareBmpFields(){
mBmpFields =新BitmapField [fileNames.length]
的for(int i = 0; I< fileNames.length;我++){
恩codeDIMAGE图像= EN codeDIMAGE
.getEn codedImageResource(文件名[I]);
图像= sizeImage(图像,mImgWidth,mImgHeight);
mBmpFields [I] =
新BitmapField(image.getBitmap(),可聚焦| FIELD_HCENTER);
mBmpFields [I] .setMargin(mImgMargin,mImgMargin,
mImgMargin,mImgMargin);
添加(mBmpFields [I]);
}
}
}
使用BitmapField - 自定义布局
要设置管理器中的BitmapFields的自定义位置,可以实现与自定义布局 经理
类ScrLayout扩展MainScreen {
INT mScrWidth = Display.getWidth();
INT mScrHeight = Display.getHeight();
INT mImgWidth = 80;
INT mImgHeight = 80;
INT mImgMargin = 10;
串的文件名[] = {1.png,2.png,3.png,4.png,5.png,
6.png,7.png,8.png,9.png};
BitmapField [] mBmpFields; 公共ScrLayout(){
超(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
prepareBmpFields();
} 私人无效prepareBmpFields(){
LayoutManager的经理=新的LayoutManager();
加(经理);
mBmpFields =新BitmapField [fileNames.length]
的for(int i = 0; I< fileNames.length;我++){
恩codeDIMAGE图像= EN codeDIMAGE
.getEn codedImageResource(文件名[I]);
图像= sizeImage(图像,mImgWidth,mImgHeight);
mBmpFields [I] =
新BitmapField(image.getBitmap(),可聚焦);
manager.add(mBmpFields [I]);
}
} 类的LayoutManager扩展了VerticalFieldManager {
公众的LayoutManager(){
超(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
} 保护无效sublayout(INT宽度,高度INT){
INT列= mScrWidth /(mImgWidth + 2 * mImgMargin);
的for(int i = 0,J = 0; I< mBmpFields.length;我++){
INT POSX = j的*(mImgWidth + 2 * mImgMargin)+ mImgMargin;
INT波西= I *(mImgHeight + 2 * mImgMargin)+ mImgMargin;
场场= mBmpFields [I]
layoutChild(场,mImgWidth,mImgHeight);
setPositionChild(场,POSX,铭文); J =(J ==列 - 1)? 0:J + 1;
}
setExtent(mScrWidth,mScrHeight);
}
公众诠释的get preferredWidth(){
返回mScrWidth;
}
公众诠释的get preferredHeight(){
返回mScrHeight;
}
}
}
How to draw png images with specific size and position on the screen?
Resize image
public EncodedImage sizeImage(EncodedImage image, int width,
int height) {
EncodedImage result = null;
int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
int requiredWidthFixed32 = Fixed32.toFP(width);
int requiredHeightFixed32 = Fixed32.toFP(height);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
requiredWidthFixed32);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32,
requiredHeightFixed32);
result = image.scaleImage32(scaleXFixed32, scaleYFixed32);
return result;
}
This function will be used in the code below.
Simply painting images
Assuming you have 9 png images in your project resources.
class Scr extends MainScreen {
int mImgWidth = 80;
int mImgHeight = 80;
int mImgMargin = 10;
String fileNames[] = { "1.png", "2.png", "3.png", "4.png", "5.png",
"6.png", "7.png", "8.png", "9.png" };
EncodedImage[] mImages;
public Scr() {
super();
prepareImages();
}
private void prepareImages() {
mImages = new EncodedImage[fileNames.length];
for (int i = 0; i < fileNames.length; i++) {
EncodedImage image = EncodedImage
.getEncodedImageResource(fileNames[i]);
mImages[i] = sizeImage(image, mImgWidth, mImgHeight);
}
}
protected void paint(Graphics graphics) {
paintImages(graphics);
super.paint(graphics);
}
private void paintImages(Graphics graphics) {
int scrWidth = Display.getWidth();
int columns = scrWidth / (mImgWidth + 2 * mImgMargin);
int rows = mImages.length / columns
+ (mImages.length % columns > 0 ? 1 : 0);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin;
int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin;
EncodedImage image = mImages[i * columns + j];
graphics.drawImage(posX, posY, mImgWidth, mImgHeight,
image, 0, 0, 0);
}
}
}
}
Simply painting images - optimization
class ScrOpt extends MainScreen {
int mScrWidth = Display.getWidth();
int mScrHeight = Display.getHeight();
int mImgWidth = 80;
int mImgHeight = 80;
int mImgMargin = 10;
String fileNames[] = { "1.png", "2.png", "3.png", "4.png", "5.png",
"6.png", "7.png", "8.png", "9.png" };
EncodedImage[] mImages;
Bitmap mImgTable;
public ScrOpt() {
super();
prepareImages();
mImgTable = paintImages();
}
private void prepareImages() {
mImages = new EncodedImage[fileNames.length];
for (int i = 0; i < fileNames.length; i++) {
EncodedImage image = EncodedImage
.getEncodedImageResource(fileNames[i]);
mImages[i] = sizeImage(image, mImgWidth, mImgHeight);
}
}
private Bitmap paintImages() {
Bitmap result = new Bitmap(mScrWidth, mScrHeight);
Graphics graphics = new Graphics(result);
int scrWidth = mScrWidth;
int columns = scrWidth / (mImgWidth + 2 * mImgMargin);
int rows = mImages.length / columns
+ (mImages.length % columns > 0 ? 1 : 0);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin;
int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin;
EncodedImage image = mImages[i * columns + j];
graphics.drawImage(posX, posY, mImgWidth, mImgHeight, image, 0,
0, 0);
}
}
return result;
}
protected void paint(Graphics graphics) {
super.paint(graphics);
graphics.drawBitmap(0, 0, mScrWidth, mScrHeight, mImgTable, 0, 0);
}
}
You can otimize it even more, using paintBackground() method
Using BitmapField
All above is about painting images directly to screen using Graphics. Sometimes its great - when you want to display some animation or background image. But what if you want to keep standard UI user experience, and use images as a fields?
The strait way is a BitmapField
class ScrBmpField extends MainScreen {
int mImgWidth = 80;
int mImgHeight = 80;
int mImgMargin = 10;
String fileNames[] = { "1.png", "2.png", "3.png", "4.png", "5.png",
"6.png", "7.png", "8.png", "9.png" };
BitmapField[] mBmpFields;
public ScrBmpField() {
super(VERTICAL_SCROLL|VERTICAL_SCROLLBAR);
prepareBmpFields();
}
private void prepareBmpFields() {
mBmpFields = new BitmapField[fileNames.length];
for (int i = 0; i < fileNames.length; i++) {
EncodedImage image = EncodedImage
.getEncodedImageResource(fileNames[i]);
image = sizeImage(image, mImgWidth, mImgHeight);
mBmpFields[i] =
new BitmapField(image.getBitmap(), FOCUSABLE|FIELD_HCENTER);
mBmpFields[i].setMargin(mImgMargin, mImgMargin,
mImgMargin, mImgMargin);
add(mBmpFields[i]);
}
}
}
Using BitmapField - custom layout
To set a custom positions of BitmapFields within manager, you can implement manager with custom layout:
class ScrLayout extends MainScreen {
int mScrWidth = Display.getWidth();
int mScrHeight = Display.getHeight();
int mImgWidth = 80;
int mImgHeight = 80;
int mImgMargin = 10;
String fileNames[] = { "1.png", "2.png", "3.png", "4.png", "5.png",
"6.png", "7.png", "8.png", "9.png" };
BitmapField[] mBmpFields;
public ScrLayout() {
super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
prepareBmpFields();
}
private void prepareBmpFields() {
LayoutManager manager = new LayoutManager();
add(manager);
mBmpFields = new BitmapField[fileNames.length];
for (int i = 0; i < fileNames.length; i++) {
EncodedImage image = EncodedImage
.getEncodedImageResource(fileNames[i]);
image = sizeImage(image, mImgWidth, mImgHeight);
mBmpFields[i] =
new BitmapField(image.getBitmap(), FOCUSABLE);
manager.add(mBmpFields[i]);
}
}
class LayoutManager extends VerticalFieldManager {
public LayoutManager() {
super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
}
protected void sublayout(int width, int height) {
int columns = mScrWidth / (mImgWidth + 2 * mImgMargin);
for (int i = 0, j = 0; i < mBmpFields.length; i++) {
int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin;
int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin;
Field field = mBmpFields[i];
layoutChild(field, mImgWidth, mImgHeight);
setPositionChild(field, posX, posY);
j = (j == columns - 1) ? 0 : j + 1;
}
setExtent(mScrWidth, mScrHeight);
}
public int getPreferredWidth() {
return mScrWidth;
}
public int getPreferredHeight() {
return mScrHeight;
}
}
}
这篇关于黑莓手机 - 在屏幕上绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!