Android SDK 不会将文件加载为位图...我该如何解决这个问题?
我一直在阅读 Mario Zechner 所著的《Beginning Android Games》一书,他在 Google 代码中提供了一个示例。
当我运行这个时,它只是关闭了我,并说主要活动已意外关闭。
当我运行 hello world 时,SDK 工作得很好,但我不知道从哪里开始。
Android 2.2 API Level 8 是我选择的设备,因为我的手机就是这样的。
这是我试图运行的代码。
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends Activity {
class RenderView extends View {
Bitmap bob565;
Bitmap bob4444;
Rect dst = new Rect();
public RenderView(Context context) {
super(context);
try {
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("helmet.png");
bob565 = BitmapFactory.decodeStream(inputStream);
inputStream.close();
Log.d("BitmapText",
"bobrgb888.png format: " + bob565.getConfig());
inputStream = assetManager.open("helmet.png");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
bob4444 = BitmapFactory
.decodeStream(inputStream, null, options);
inputStream.close();
Log.d("BitmapText",
"bobargb8888.png format: " + bob4444.getConfig());
} catch (IOException e) {
// silently ignored, bad coder monkey, baaad!
} finally {
// we should really close our input streams here.
}
}
protected void onDraw(Canvas canvas) {
dst.set(50, 50, 350, 350);
canvas.drawBitmap(bob565, null, dst, null);
canvas.drawBitmap(bob4444, 100, 100, null);
invalidate();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new RenderView(this));
}
}
I have been reading the book called "Beginning Android Games" by Mario Zechner and he has an example that is linked here in Google code.
When I ran this it just shutdown on me and said Main Activity has shutdown unexpectedly.
When I ran the hello world the SDK worked just fine, but I am not sure where to start.
Android 2.2 API Level 8 is the Device I have selected since that's what my phone has.
Here is the code I was trying to run.
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends Activity {
class RenderView extends View {
Bitmap bob565;
Bitmap bob4444;
Rect dst = new Rect();
public RenderView(Context context) {
super(context);
try {
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("helmet.png");
bob565 = BitmapFactory.decodeStream(inputStream);
inputStream.close();
Log.d("BitmapText",
"bobrgb888.png format: " + bob565.getConfig());
inputStream = assetManager.open("helmet.png");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
bob4444 = BitmapFactory
.decodeStream(inputStream, null, options);
inputStream.close();
Log.d("BitmapText",
"bobargb8888.png format: " + bob4444.getConfig());
} catch (IOException e) {
// silently ignored, bad coder monkey, baaad!
} finally {
// we should really close our input streams here.
}
}
protected void onDraw(Canvas canvas) {
dst.set(50, 50, 350, 350);
canvas.drawBitmap(bob565, null, dst, null);
canvas.drawBitmap(bob4444, 100, 100, null);
invalidate();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new RenderView(this));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哦天哪,请将
helmet.png
图像放入项目assets
目录中......这段代码完全运行正常!唯一可能存在的问题是
helmet.png 找不到
Oh dear, please put
helmet.png
image into projectassets
directory....This code is totally running fine! The only problem can exist is that
helmet.png not found
AssetManager 方法不起作用。所以我尝试了以下方法,这对我来说确实有效。
RenderView 只是当前类...因此,如果您在 Main.java 下编写此代码,您将编写:
location 只是文件的路径。例如,如果您在 images 文件夹下有一个名为 ball.jpg 的图像,您只需编写:
The AssetManager way did not work. So I tried the following method and this did work for me.
RenderView is just the current class... so if your writing this code under Main.java you would write:
location is just the path to the file. For example if you have a image called ball.jpg under images folder you would just write: