Displaying certain parts of a bitmap

Problems with Canvas, OpenGL, etc...

Displaying certain parts of a bitmap

Postby WWaldo » Sat Jun 05, 2010 2:34 am

Hey everyone,

I am in the middle of trying to write an RPG game and I am kinda stuck. If you have played a 2D rpg you know there are usually maps that you travel through to get to little bitmap towns that then take you into the town and all that stuff. I am stuck at the part where I just have a certain part of the map being shown. I figured that I would draw the map, then draw the character and have the loop rerender the screen based on what the coordinates of the character are. This probably doesn't make a lot of sense, so If you need any more info let me know. Thanks in advance for any help.

WWaldo
WWaldo
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Sun Feb 21, 2010 8:25 pm

Top

Re: Displaying certain parts of a bitmap

Postby gskbyte » Sat Jun 05, 2010 1:05 pm

This method of the canvas class may be useful:

http://developer.android.com/reference/ ... s.Paint%29
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Re: Displaying certain parts of a bitmap

Postby ShadowKntSDS » Sat Jun 05, 2010 4:54 pm

One method for such 2d bitmaps is to use a tile based setup (think super mario bros). All the classic SNES rpg's used something similar. It is fairly easy to figure out which tiles are currently on the screen based on the player coord, and then you would only draw those tiles. This can be done in pure 2d using the Canvas class or in 3d using opengl.
ShadowKntSDS
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Apr 28, 2010 10:29 pm

Re: Displaying certain parts of a bitmap

Postby WWaldo » Sun Jun 06, 2010 1:21 pm

Okay,
I think what I need is the rect thing, I can't do the tiles because I an converting a hand drawn image into a bitmap, unless there is another way to figure that out. I was wondering if you had any idea how to stop it from chugging though. I did a sample just to see how it would work, and it worked fine, but it was really slow, and it was only a 60kb file. The just have it drawing the rect of a certain part of the bitmap, so i figured it would be okay, I am in a hurry so I may have missed something, let me know if you need code.
Thanks
WWaldo
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Sun Feb 21, 2010 8:25 pm

Re: Displaying certain parts of a bitmap

Postby gskbyte » Sun Jun 06, 2010 1:23 pm

Do both rects have the same size? How bigger is the Bitmap respect to the canvas?
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Re: Displaying certain parts of a bitmap

Postby WWaldo » Sun Jun 06, 2010 1:43 pm

Hrmmmm, the size is a good question, I will have to check when I get home. I kinda threw it together yesterday. I read somewher that I needed to redefine the bitmap and use surfaceChanged or something, but I couldn't get that to work last night.

The image is 1000x1000 pixels, so I think it would have to be the different rect sizes so Ill check it out later, thanks
WWaldo
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Sun Feb 21, 2010 8:25 pm

Top

Re: Displaying certain parts of a bitmap

Postby gskbyte » Sun Jun 06, 2010 1:55 pm

This code may be useful for you (I'm using it and it draws a 1000x1000 bitmap on canvas in 2ms, on an HTC Tattoo):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.gskbyte.splineTest.background;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Paint;
  8. import android.graphics.Rect;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11.  
  12. public class BackgroundView extends View
  13. {
  14.     protected Bitmap mBg;
  15.     protected int mBgWidth, mBgHeight;
  16.     protected int mCanvasWidth, mCanvasHeight;
  17.     protected final Paint mPaint = new Paint();
  18.     protected final Rect mOrgRect = new Rect(),
  19.                          mDstRect = new Rect();
  20.     private int mLastX, mLastY;
  21.    
  22.     protected void init(Bitmap bitmap)
  23.     {
  24.         if(bitmap!=null)
  25.         {
  26.             mBg = bitmap;
  27.            
  28.             mBgWidth = mBg.getWidth();
  29.             mBgHeight = mBg.getHeight();
  30.         }
  31.     }
  32.    
  33.     protected BackgroundView(Context context)
  34.     {
  35.         super(context);
  36.         mBg = null;
  37.         mBgWidth = 0;
  38.         mBgHeight = 0;
  39.         mCanvasWidth = 0;
  40.         mCanvasHeight = 0;
  41.     }
  42.    
  43.     public BackgroundView(Context context, int iconId)
  44.     {
  45.         super(context);
  46.         init( BitmapFactory.decodeResource(context.getResources(), iconId) );  
  47.     }
  48.    
  49.     public BackgroundView(Context context, Bitmap bitmap)
  50.     {
  51.         super(context);
  52.         init(bitmap);
  53.     }
  54.    
  55.     public void recycle()
  56.     {
  57.         mBg.recycle();
  58.     }
  59.    
  60.     public void setCanvasSize(int width, int height)
  61.     {
  62.         mCanvasWidth  = width;
  63.         mCanvasHeight = height;
  64.        
  65.         mOrgRect.left = 0;
  66.         mOrgRect.top = 0;
  67.         if(mBgWidth>width){
  68.             mOrgRect.right = width;
  69.             mDstRect.left  = 0;
  70.             mDstRect.right = width;
  71.         } else {
  72.             mOrgRect.right = mBgWidth;
  73.            
  74.             mDstRect.left = (width-mBgWidth)/2;
  75.             mDstRect.right = mDstRect.left+mBgWidth;
  76.         }
  77.        
  78.         if(mBgHeight>height){
  79.             mOrgRect.bottom = height;
  80.             mDstRect.top    = 0;
  81.             mDstRect.bottom = height;
  82.         } else {
  83.             mOrgRect.bottom = mBgHeight;
  84.            
  85.             mDstRect.top = (height-mBgHeight)/2;
  86.             mDstRect.bottom = mDstRect.top+mBgHeight;
  87.         }
  88.     }
  89.    
  90.     public void onDraw(Canvas canvas)
  91.     {
  92.         if(mCanvasHeight != canvas.getHeight())
  93.             setCanvasSize(canvas.getWidth(), canvas.getHeight());
  94.         canvas.drawBitmap(mBg, mOrgRect, mDstRect, mPaint);
  95.     }
  96.    
  97.     public boolean onTouchEvent(MotionEvent event)
  98.     {
  99.         int x = (int) event.getX();
  100.         int y = (int) event.getY();
  101.         int action = event.getAction();
  102.        
  103.         switch(action){
  104.         case MotionEvent.ACTION_DOWN:
  105.             mLastX = x;
  106.             mLastY = y;
  107.             break;
  108.         case MotionEvent.ACTION_MOVE:
  109.             mOrgRect.left += (mLastX-x);
  110.             if(mOrgRect.left<0)
  111.                 mOrgRect.left=0;
  112.             else if (mOrgRect.left>mBgWidth-mCanvasWidth)
  113.                 mOrgRect.left=mBgWidth-mCanvasWidth;
  114.             mOrgRect.right = mOrgRect.left + mCanvasWidth;
  115.            
  116.             mOrgRect.top += (mLastY-y);
  117.             if(mOrgRect.top<0)
  118.                 mOrgRect.top=0;
  119.             else if (mOrgRect.top>mBgHeight-mCanvasHeight)
  120.                 mOrgRect.top=mBgHeight-mCanvasHeight;
  121.             mOrgRect.bottom = mOrgRect.top + mCanvasHeight;
  122.  
  123.             mLastX = x;
  124.             mLastY = y;
  125.            
  126.             invalidate();
  127.             break;
  128.         }
  129.         return true;
  130.     }
  131. }
  132.  
Parsed in 0.046 seconds, using GeSHi 1.0.8.4
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Re: Displaying certain parts of a bitmap

Postby WWaldo » Mon Jun 07, 2010 3:23 am

I tried implementing what you suggested, took me a while to figure out that I needed to initialize my bitmap somewhere, and once I did that I kept getting a black screen. I am not sure if I screwed up something with the rects or what. I'll leave my code here, maybe someone can find what I did wrong while I look. Thanks for the code before btw, it made a lot more sense than my, update inside the onDraw way of doing it XD.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.Waldev.Game3;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.graphics.Canvas;
  8. import android.graphics.Paint;
  9. import android.graphics.Rect;
  10. import android.os.Bundle;
  11. import android.view.Display;
  12. import android.view.MotionEvent;
  13. import android.view.SurfaceHolder;
  14. import android.view.SurfaceView;
  15. import android.view.Window;
  16. import android.view.WindowManager;
  17.  
  18. public class superSpecialAwesome extends Activity {
  19.     /** Called when the activity is first created. */
  20.     @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         final Window win = getWindow();
  23.                 super.onCreate(savedInstanceState);
  24.                 requestWindowFeature(Window.FEATURE_NO_TITLE);
  25.                 win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  26.         setContentView(new panel(this));
  27.     }
  28.     class panel extends SurfaceView implements SurfaceHolder.Callback{
  29.         //same as game engine I guess
  30.         private awesomeThread thread;
  31.         private int checkState = 0;
  32.         private Bitmap bG;
  33.         private int bGHeight, bGWidth;
  34.         private int canvasHeight, canvasWidth;
  35.         private final Paint paint = new Paint();
  36.         private final Rect rectBitmap = new Rect();
  37.         private final Rect rect = new Rect();
  38.  
  39.         private Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  40.         //private int width = display.getWidth();
  41.         //private int height = display.getHeight();
  42.         private int changeOfX = 0;
  43.         private int changeOfY = 0;
  44.        
  45.         public panel(Context context) {
  46.                 super(context);
  47.             bG = BitmapFactory.decodeResource(getResources(), R.drawable.background);
  48.             bGWidth = 0;
  49.             bGHeight = 0;
  50.             canvasWidth = 0;
  51.             canvasHeight = 0;
  52.          
  53.                 getHolder().addCallback(this);
  54.                 thread = new awesomeThread(getHolder(), this);
  55.                 setFocusable(true);
  56.                
  57.    
  58.         }
  59.        
  60.         public panel(Context context, int iconId)
  61.         {
  62.             super(context);
  63.             init(BitmapFactory.decodeResource(context.getResources(), iconId) );  
  64.         }
  65.        
  66.         public panel(Context context, Bitmap bitmap){
  67.                 super(context);
  68.             init(bitmap);
  69.         }
  70.        
  71.         public void recycle(){
  72.                 bG.recycle();
  73.         }
  74.        
  75.         protected void init(Bitmap bitmap){
  76.             if(bitmap!=null){
  77.                 bG = bitmap;
  78.                
  79.                 bGWidth = bG.getWidth();
  80.                 bGHeight = bG.getHeight();
  81.             }
  82.         }
  83.        
  84.         public void setCanvasSize(int width, int height)
  85.         {
  86.             canvasWidth  = width;
  87.             canvasHeight = height;
  88.            
  89.             rectBitmap.left = 0;
  90.             rectBitmap.top = 0;
  91.             if(bGWidth>width){
  92.                 rectBitmap.right = width;
  93.                 rect.left  = 0;
  94.                 rect.right = width;
  95.             } else {
  96.                 rectBitmap.right = bGWidth;
  97.                
  98.                 rect.left = (width-bGWidth)/2;
  99.                 rect.right = rect.left+bGWidth;
  100.             }
  101.            
  102.             if(bGHeight>height){
  103.                 rectBitmap.bottom = height;
  104.                 rect.top    = 0;
  105.                 rect.bottom = height;
  106.             } else {
  107.                 rectBitmap.bottom = bGHeight;
  108.                
  109.                 rect.top = (height-bGHeight)/2;
  110.                 rect.bottom = rect.top+bGHeight;
  111.             }
  112.         }
  113.  
  114.        
  115.         @Override
  116.         public void onDraw(Canvas canvas){
  117.                 if(canvasHeight != display.getHeight()){
  118.                 setCanvasSize(display.getWidth(), display.getHeight());
  119.                 }
  120.             canvas.drawBitmap(bG, rectBitmap, rect, paint);
  121.         }
  122.        
  123.         public boolean onTouchEvent(MotionEvent event){
  124.                  int x = (int) event.getX();
  125.                 int y = (int) event.getY();
  126.                 int action = event.getAction();
  127.                
  128.                 switch(action){
  129.                 case MotionEvent.ACTION_DOWN:
  130.                     changeOfX = x;
  131.                     changeOfY = y;
  132.                     break;
  133.                 case MotionEvent.ACTION_MOVE:
  134.                     rectBitmap.left += (changeOfX-x);
  135.                     if(rectBitmap.left<0)
  136.                         rectBitmap.left=0;
  137.                     else if (rectBitmap.left>bGWidth-canvasWidth)
  138.                         rectBitmap.left=bGWidth-canvasWidth;
  139.                     rectBitmap.right = rectBitmap.left + canvasWidth;
  140.                    
  141.                     rectBitmap.top += (changeOfY-y);
  142.                     if(rectBitmap.top<0)
  143.                         rectBitmap.top=0;
  144.                     else if (rectBitmap.top>bGHeight-canvasHeight)
  145.                         rectBitmap.top=bGHeight-canvasHeight;
  146.                     rectBitmap.bottom = rectBitmap.top + canvasHeight;
  147.          
  148.                     changeOfX = x;
  149.                     changeOfY = y;
  150.                    
  151.                     invalidate();
  152.                     break;
  153.                 }
  154.                 return true;
  155.         }
  156.        
  157.         @Override
  158.         public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  159.                 // TODO Auto-generated method stub
  160.                
  161.         }
  162.  
  163.         public void surfaceCreated(SurfaceHolder holder) {
  164.                 thread.setRunning(true);
  165.                 thread.start();
  166.         }
  167.  
  168.         @Override
  169.         public void surfaceDestroyed(SurfaceHolder holder) {
  170.                 // simply copied from sample application LunarLander:
  171.                 // we have to tell thread to shut down & wait for it to finish, or else
  172.                 // it might touch the Surface after we return and explode
  173.                 boolean retry = true;
  174.                 thread.setRunning(false);
  175.                 while (retry) {
  176.                         try {
  177.                                 thread.join();
  178.                                 retry = false;
  179.                         } catch (InterruptedException e) {
  180.                                 // we will try it again and again...
  181.                         }
  182.                 }
  183.  
  184.     }
  185.     }
  186.  
  187. }
Parsed in 0.055 seconds, using GeSHi 1.0.8.4


Edit:
I figured out that the rectangles aren't being defined correctly, so I will have to mess with that tomorrow, and screw around with the scrolling stuff too
WWaldo
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Sun Feb 21, 2010 8:25 pm

Re: Displaying certain parts of a bitmap

Postby gskbyte » Mon Jun 07, 2010 12:51 pm

Well, the code I passed you yesterday was a View, you can set it this way:
Code: Select all
public void onCreate(Bundle savedInstanceState) {
    BackgroundView view = new BackgroundView(this, R.drawable.bg)
    setContentView(view).
}


I have a modified version extending a SurfaceView, if you can't find where your version fails I'll post the code when I arrive home. Good luck!
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Re: Displaying certain parts of a bitmap

Postby WWaldo » Mon Jun 07, 2010 9:54 pm

Okay, I was messing with it earlier, got the bitmap to show up, but the scrolling isn't working. I tried something a bit different, made my onTouchEvent turn on or off a boolean and store some variables and then I have a updateBitmapDim to update the display rectangle for the bitmap based on the new coordinates with respect to the place where ACTION_DOWN happened. I am not sure if I did something wrong or if what I want to do just won't work, so I will leave my code.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.Waldev.Game3;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.graphics.Canvas;
  8. import android.graphics.Color;
  9. import android.graphics.Paint;
  10. import android.graphics.Rect;
  11. import android.os.Bundle;
  12. import android.view.Display;
  13. import android.view.MotionEvent;
  14. import android.view.SurfaceHolder;
  15. import android.view.SurfaceView;
  16. import android.view.Window;
  17. import android.view.WindowManager;
  18.  
  19. public class superSpecialAwesome extends Activity {
  20.     /** Called when the activity is first created. */
  21.     @Override
  22.     public void onCreate(Bundle savedInstanceState) {
  23.         final Window win = getWindow();
  24.                 super.onCreate(savedInstanceState);
  25.                 requestWindowFeature(Window.FEATURE_NO_TITLE);
  26.                 win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  27.         setContentView(new panel(this));
  28.     }
  29.     class panel extends SurfaceView implements SurfaceHolder.Callback{
  30.         //same as game engine I guess
  31.         private awesomeThread thread;
  32.         private int checkState = 0;
  33.         private Bitmap bG = BitmapFactory.decodeResource(getResources(), R.drawable.background);;
  34.         private final Paint paint = new Paint();
  35.         private final Rect rectBitmap = new Rect();
  36.         private final Rect rect = new Rect();
  37.         private boolean movement = false;
  38.  
  39.         private Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  40.         private int bitmapLeft = 0;
  41.         private int bitmapTop = 0;
  42.         private int bitmapRight = display.getWidth();
  43.         private int bitmapBottom = display.getHeight();
  44.         private int x = 0;
  45.         private int y = 0;
  46.         private int xDown = 0;
  47.         private int yDown = 0;
  48.        
  49.         public panel(Context context) {
  50.                 super(context);
  51.          
  52.                 getHolder().addCallback(this);
  53.                 thread = new awesomeThread(getHolder(), this);
  54.                 setFocusable(true);
  55.                
  56.    
  57.         }
  58.  
  59.         public void setCanvasSize(int width, int height){
  60.             rect.top = 0;
  61.             rect.left = 0;
  62.             rect.bottom = height;
  63.             rect.right = width;
  64.            
  65.         }
  66.         public void updateBitmapDim(){
  67.                 if(movement){
  68.                         if(x > xDown){
  69.                                 bitmapLeft = bitmapLeft+10;
  70.                                 bitmapRight = bitmapRight+10;
  71.                         }
  72.                         else if(x < xDown){
  73.                                 bitmapLeft = bitmapLeft-10;
  74.                                 bitmapRight = bitmapRight-10;
  75.                         }
  76.                         if(y > yDown){
  77.                                 bitmapTop = bitmapTop+10;
  78.                                 bitmapBottom = bitmapBottom+10;
  79.                         }
  80.                         else if(y < yDown){
  81.                                 bitmapTop = bitmapTop-10;
  82.                                 bitmapBottom = bitmapBottom-10;
  83.                         }
  84.                 }
  85.                 rectBitmap.top = bitmapTop;
  86.             rectBitmap.left = bitmapLeft;
  87.             rectBitmap.bottom = bitmapBottom;
  88.             rectBitmap.right = bitmapRight;
  89.         }
  90.  
  91.        
  92.         @Override
  93.         public void onDraw(Canvas canvas){
  94.                 if(rect.bottom != display.getHeight()){
  95.                         setCanvasSize(display.getWidth(), display.getHeight());
  96.                 }
  97.                 updateBitmapDim();
  98.             canvas.drawBitmap(bG, rectBitmap, rect, paint);
  99.         }
  100.        
  101.         public boolean onTouchEvent(MotionEvent event){
  102.                 int action = event.getAction();
  103.                 x = (int) event.getX();
  104.                 y = (int) event.getY();
  105.                
  106.                 switch(action){
  107.                 case(MotionEvent.ACTION_DOWN):
  108.                         xDown = (int) event.getX();
  109.                         yDown = (int) event.getY();
  110.                         movement = true;
  111.                
  112.                 case(MotionEvent.ACTION_UP):
  113.                         movement = false;
  114.                 }
  115.                 return true;
  116.                
  117.         }
  118.        
  119.         @Override
  120.         public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  121.                 // TODO Auto-generated method stub
  122.                
  123.         }
  124.  
  125.         public void surfaceCreated(SurfaceHolder holder) {
  126.                 thread.setRunning(true);
  127.                 thread.start();
  128.         }
  129.  
  130.         @Override
  131.         public void surfaceDestroyed(SurfaceHolder holder) {
  132.                 // simply copied from sample application LunarLander:
  133.                 // we have to tell thread to shut down & wait for it to finish, or else
  134.                 // it might touch the Surface after we return and explode
  135.                 boolean retry = true;
  136.                 thread.setRunning(false);
  137.                 while (retry) {
  138.                         try {
  139.                                 thread.join();
  140.                                 retry = false;
  141.                         } catch (InterruptedException e) {
  142.                                 // we will try it again and again...
  143.                         }
  144.                 }
  145.  
  146.     }
  147.     }
  148.  
  149. }
Parsed in 0.053 seconds, using GeSHi 1.0.8.4
WWaldo
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Sun Feb 21, 2010 8:25 pm

Re: Displaying certain parts of a bitmap

Postby gskbyte » Mon Jun 07, 2010 10:03 pm

Sure it doesn't work? I just tested with this code and everything went perfect :S:

Code: Select all
package org.gskbyte.splineTest;

import org.gskbyte.splineTest.background.BackgroundView;

import android.app.Activity;
import android.os.Bundle;

public class SplineTest extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        BackgroundView v = new BackgroundView(this, R.drawable.suelo);
        setContentView(v);
    }
}
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Re: Displaying certain parts of a bitmap

Postby WWaldo » Mon Jun 07, 2010 10:19 pm

You used my code with the backgroundview part in the onCreate? Its part of view isn't it? I hate to ask but can you post the code that you used? You've helped me a lot, so thanks again
WWaldo
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Sun Feb 21, 2010 8:25 pm

Re: Displaying certain parts of a bitmap

Postby gskbyte » Mon Jun 07, 2010 10:23 pm

No I tried the code I just posted. I'm going to try the code you posted two entries before, I'll tell you now.
gskbyte
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Tue Jan 12, 2010 1:51 pm

Re: Displaying certain parts of a bitmap

Postby WWaldo » Mon Jun 07, 2010 10:32 pm

Oh, I was trying to implement yours over mine, so thats probably why it wasn't working. Worse comes to worse I'll just use yours in total.
WWaldo
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Sun Feb 21, 2010 8:25 pm

Re: Displaying certain parts of a bitmap

Postby WWaldo » Mon Jun 07, 2010 11:16 pm

Okay, I finally got this code to work, I think it was never changing the x and ys or something. It looked laggy but i think that was just the emulator being stupid. And it was, just checked on my phone, and once I get it going at a specific frame rate I guess it will look smoother.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.Waldev.Game3;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.graphics.Canvas;
  8. import android.graphics.Paint;
  9. import android.graphics.Rect;
  10. import android.os.Bundle;
  11. import android.view.Display;
  12. import android.view.MotionEvent;
  13. import android.view.SurfaceHolder;
  14. import android.view.SurfaceView;
  15. import android.view.Window;
  16. import android.view.WindowManager;
  17.  
  18. public class superSpecialAwesome extends Activity {
  19.     /** Called when the activity is first created. */
  20.     @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         final Window win = getWindow();
  23.                 super.onCreate(savedInstanceState);
  24.                
  25.                 requestWindowFeature(Window.FEATURE_NO_TITLE);
  26.                 win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  27.         setContentView(new panel(this));
  28.     }
  29.     class panel extends SurfaceView implements SurfaceHolder.Callback{
  30.         //same as game engine I guess
  31.         private awesomeThread thread;
  32.         private int checkState = 0;
  33.         private Bitmap bG = BitmapFactory.decodeResource(getResources(), R.drawable.background);;
  34.         private final Paint paint = new Paint();
  35.         private final Rect rectBitmap = new Rect();
  36.         private final Rect rect = new Rect();
  37.         private boolean movement = false;
  38.  
  39.         private Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  40.         private int bitmapLeft = 0;
  41.         private int bitmapTop = 0;
  42.         private int bitmapRight = display.getWidth();
  43.         private int bitmapBottom = display.getHeight();
  44.         private int x = 0;
  45.         private int y = 0;
  46.         private int xDown = 0;
  47.         private int yDown = 0;
  48.        
  49.         public panel(Context context) {
  50.                 super(context);
  51.          
  52.                 getHolder().addCallback(this);
  53.                 thread = new awesomeThread(getHolder(), this);
  54.                 setFocusable(true);
  55.                
  56.    
  57.         }
  58.  
  59.         public void setCanvasSize(int width, int height){
  60.             rect.top = 0;
  61.             rect.left = 0;
  62.             rect.bottom = height;
  63.             rect.right = width;
  64.            
  65.             rectBitmap.top = bitmapTop;
  66.             rectBitmap.left = bitmapLeft;
  67.             rectBitmap.bottom = bitmapBottom;
  68.             rectBitmap.right = bitmapRight;
  69.            
  70.         }
  71.  
  72.        
  73.         @Override
  74.         public void onDraw(Canvas canvas){
  75.                 if(rect.bottom != display.getHeight()){
  76.                         setCanvasSize(display.getWidth(), display.getHeight());
  77.                 }
  78.             canvas.drawBitmap(bG, rectBitmap, rect, paint);
  79.         }
  80.        
  81.         public boolean onTouchEvent(MotionEvent event){
  82.                 int action = event.getAction();
  83.                 x = (int) event.getX();
  84.                         y = (int) event.getY();
  85.                
  86.                 switch(action){
  87.                 case(MotionEvent.ACTION_DOWN):
  88.                         xDown = x;
  89.                         yDown = y;
  90.                        
  91.                 case(MotionEvent.ACTION_MOVE):
  92.  
  93.                                 if(x > xDown){
  94.                                         bitmapLeft = bitmapLeft+10;
  95.                                         bitmapRight = bitmapRight+10;
  96.                                 }
  97.                                 else if(x < xDown){
  98.                                         bitmapLeft = bitmapLeft-10;
  99.                                         bitmapRight = bitmapRight-10;
  100.                                 }
  101.                                 if(y > yDown){
  102.                                         bitmapTop = bitmapTop+10;
  103.                                         bitmapBottom = bitmapBottom+10;
  104.                                 }
  105.                                 else if(y < yDown){
  106.                                         bitmapTop = bitmapTop-10;
  107.                                         bitmapBottom = bitmapBottom-10;
  108.                         }
  109.                         rectBitmap.top = bitmapTop;
  110.                     rectBitmap.left = bitmapLeft;
  111.                     rectBitmap.bottom = bitmapBottom;
  112.                     rectBitmap.right = bitmapRight;
  113.                 }
  114.                
  115.                 return true;
  116.                
  117.         }
  118.        
  119.         @Override
  120.         public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  121.                 // TODO Auto-generated method stub
  122.                
  123.         }
  124.  
  125.         public void surfaceCreated(SurfaceHolder holder) {
  126.                 thread.setRunning(true);
  127.                 thread.start();
  128.         }
  129.  
  130.         @Override
  131.         public void surfaceDestroyed(SurfaceHolder holder) {
  132.                 // simply copied from sample application LunarLander:
  133.                 // we have to tell thread to shut down & wait for it to finish, or else
  134.                 // it might touch the Surface after we return and explode
  135.                 boolean retry = true;
  136.                 thread.setRunning(false);
  137.                 while (retry) {
  138.                         try {
  139.                                 thread.join();
  140.                                 retry = false;
  141.                         } catch (InterruptedException e) {
  142.                                 // we will try it again and again...
  143.                         }
  144.                 }
  145.  
  146.     }
  147.     }
  148.  
  149. }
Parsed in 0.053 seconds, using GeSHi 1.0.8.4



Thanks for the help
WWaldo
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Sun Feb 21, 2010 8:25 pm

Top
Next

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 3 guests