欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

在Android应用中显示图片代码,,package cn.o

来源: javaer 分享于  点击 11336 次 点评:176

在Android应用中显示图片代码,,package cn.o


package cn.outofmemory.android;import cn.outofmemory.android.model.Droid;import android.app.Activity;import android.content.Context;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.util.Log;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;public class MainGamePanel extends SurfaceView implements  SurfaceHolder.Callback { private static final String TAG = MainGamePanel.class.getSimpleName(); private MainThread thread; private Droid droid; public MainGamePanel(Context context) {  super(context);  // adding the callback (this) to the surface holder to intercept events  getHolder().addCallback(this);  // create droid and load bitmap  droid = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.droid_1), 50, 50);  // create the game loop thread  thread = new MainThread(getHolder(), this);  // make the GamePanel focusable so it can handle events  setFocusable(true); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width,   int height) { } @Override public void surfaceCreated(SurfaceHolder holder) {  // at this point the surface is created and  // we can safely start the game loop  thread.setRunning(true);  thread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) {  Log.d(TAG, "Surface is being destroyed");  // tell the thread to shut down and wait for it to finish  // this is a clean shutdown  boolean retry = true;  while (retry) {   try {    thread.join();    retry = false;   } catch (InterruptedException e) {    // try again shutting down the thread   }  }  Log.d(TAG, "Thread was shut down cleanly"); } @Override public boolean onTouchEvent(MotionEvent event) {  if (event.getAction() == MotionEvent.ACTION_DOWN) {   // delegating event handling to the droid   droid.handleActionDown((int)event.getX(), (int)event.getY());   // check if in the lower part of the screen we exit   if (event.getY() > getHeight() - 50) {    thread.setRunning(false);    ((Activity)getContext()).finish();   } else {    Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());   }  } if (event.getAction() == MotionEvent.ACTION_MOVE) {   // the gestures   if (droid.isTouched()) {    // the droid was picked up and is being dragged    droid.setX((int)event.getX());    droid.setY((int)event.getY());   }  } if (event.getAction() == MotionEvent.ACTION_UP) {   // touch was released   if (droid.isTouched()) {    droid.setTouched(false);   }  }  return true; } @Override protected void onDraw(Canvas canvas) {  // fills the canvas with black  canvas.drawColor(Color.BLACK);  droid.draw(canvas); }}
package cn.outofmemory.android.model;import android.graphics.Bitmap;import android.graphics.Canvas;import android.view.MotionEvent;public class Droid { private Bitmap bitmap; // the actual bitmap private int x;   // the X coordinate private int y;   // the Y coordinate private boolean touched; // if droid is touched/picked up public Droid(Bitmap bitmap, int x, int y) {  this.bitmap = bitmap;  this.x = x;  this.y = y; } public Bitmap getBitmap() {  return bitmap; } public void setBitmap(Bitmap bitmap) {  this.bitmap = bitmap; } public int getX() {  return x; } public void setX(int x) {  this.x = x; } public int getY() {  return y; } public void setY(int y) {  this.y = y; } public boolean isTouched() {  return touched; } public void setTouched(boolean touched) {  this.touched = touched; } public void draw(Canvas canvas) {  canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y - (bitmap.getHeight() / 2), null); } public void handleActionDown(int eventX, int eventY) {  if (eventX >= (x - bitmap.getWidth() / 2) && (eventX <= (x + bitmap.getWidth()/2))) {   if (eventY >= (y - bitmap.getHeight() / 2) && (y <= (y + bitmap.getHeight() / 2))) {    // droid touched    setTouched(true);   } else {    setTouched(false);   }  } else {   setTouched(false);  } }}
public void run() {  Canvas canvas;  Log.d(TAG, "Starting game loop");  while (running) {   canvas = null;   // try locking the canvas for exclusive pixel editing on the surface   try {    canvas = this.surfaceHolder.lockCanvas();    synchronized (surfaceHolder) {     // update game state     // draws the canvas on the panel     this.gamePanel.onDraw(canvas);    }   } finally {    // in case of an exception the surface is not left in    // an inconsistent state    if (canvas != null) {     surfaceHolder.unlockCanvasAndPost(canvas);    }   } // end finally  } }
相关栏目:

用户点评