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.
http://code.google.com/p/beginning-android-games/source/browse/trunk/ch04-android-basics/src/com/badlogic/androidgames/BitmapTest.java
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));
}
}
Oh dear, please put helmet.png
image into project assets
directory....
This code is totally running fine! The only problem can exist is that helmet.png not found
The AssetManager way did not work. So I tried the following method and this did work for me.
public Bitmap getImage(String location) {
InputStream is = RenderView.class.getResourceAsStream(location);
return BitmapFactory.decodeStream(is);
}
RenderView is just the current class... so if your writing this code under Main.java you would write:
Main.class.getResourcesAsStream();
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:
String location = "images/ball.png";
精彩评论