code for ball animation using SurfaceView

Quickly share your Android Code Snippets here...

code for ball animation using SurfaceView

Postby jihadrh » Tue Aug 03, 2010 7:21 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.demo.resource;
  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.os.Bundle;
  11. import android.view.MotionEvent;
  12. import android.view.SurfaceHolder;
  13. import android.view.SurfaceView;
  14. import android.view.SurfaceHolder.Callback;
  15. import android.widget.LinearLayout;
  16.  
  17. public class MoveAct extends Activity {
  18.        
  19.         @Override
  20.         protected void onCreate(Bundle savedInstanceState) {
  21.                 super.onCreate(savedInstanceState);
  22.                 LinearLayout lLayout = new LinearLayout(getApplicationContext());
  23.                 MyDraw mydraw = new MyDraw(getApplicationContext());
  24.                 lLayout.addView(mydraw);
  25.                 setContentView(lLayout);
  26.         }
  27.         class MyDraw extends SurfaceView implements Callback {
  28.                
  29.                 private MyThread myThread;
  30.                 private Bitmap bmp;
  31.                 private SurfaceHolder holder;
  32.                 private Paint paint;
  33.                 private int x = 50;
  34.                 private int y = 50;            
  35.                 int newX = 50;
  36.                 int newY = 50;
  37.                 int diffX = newX - x;
  38.                 int diffY = newY - y;
  39.                 int incrX = -1;
  40.                 int incrY = -1;
  41.                 public MyDraw(Context context) {
  42.                         super(context);
  43.                         holder = getHolder();
  44.                         holder.addCallback(this);
  45.                         paint = new Paint();           
  46.                         bmp = BitmapFactory.decodeResource(getResources(), R.drawable.spin);
  47.                 }
  48.  
  49.                 public void surfaceChanged(SurfaceHolder holder, int format, int width,
  50.                                 int height) {
  51.                        
  52.                 }
  53.                
  54.                 public void surfaceCreated(SurfaceHolder holder) {
  55.                         myThread = new MyThread(holder, this);
  56.                         myThread.setFlag(true);
  57.                         myThread.start();
  58.                 }
  59.  
  60.                 public void surfaceDestroyed(SurfaceHolder holder) {
  61.                        
  62.                 }
  63.        
  64.                 @Override
  65.                 public boolean onTouchEvent(MotionEvent event) {
  66.                         if(event.getAction() == MotionEvent.ACTION_DOWN) {
  67.                                 newX = (int) event.getX();
  68.                                 newY = (int) event.getY();
  69.                                 diffX = newX - x;
  70.                                 diffY = newY - y;
  71.                         }
  72.                         return true;
  73.                 }
  74.                 @Override
  75.                 protected void onDraw(Canvas canvas) {
  76.                         canvas.drawColor(Color.BLACK);
  77.                        
  78.                         super.onDraw(canvas);                  
  79.                         diffX = newX - x;
  80.                         diffY = newY - y;
  81.                        
  82.                         if(diffX < 0){
  83.                                 incrX = 1;
  84.                         }
  85.                         else if(diffX > 0) {
  86.                                 incrX = 0;
  87.                         }
  88.                         else {
  89.                                 incrX = -1;
  90.                         }
  91.                        
  92.                         if(diffY < 0){
  93.                                 incrY = 1;
  94.                         }
  95.                         else if(diffY > 0){
  96.                                 incrY = 0;
  97.                         }
  98.                         else {
  99.                                 incrY = -1;
  100.                         }
  101.                        
  102.                         if(diffX != 0){
  103.                                 if(incrX == 0){
  104.                                         x++;
  105.                                 }
  106.                                 else if(incrX == 1){
  107.                                         x--;
  108.                                 }
  109.                         }
  110.                         if(diffY != 0){
  111.                                 if(incrY == 0){
  112.                                         y++;
  113.                                 }
  114.                                 else if(incrY == 1){
  115.                                         y--;
  116.                                 }
  117.                         }
  118.                         canvas.drawBitmap(bmp, x, y, paint);
  119.                        
  120.                 }
  121.         }
  122.        
  123.         class MyThread extends Thread {        
  124.                 boolean flag ;
  125.                 SurfaceHolder myHolder;
  126.                 MyDraw myDraw;
  127.                 public MyThread(SurfaceHolder holder , MyDraw drawMain) {
  128.                         myHolder = holder;
  129.                         myDraw = drawMain;
  130.                 }
  131.                
  132.                 public void setFlag (boolean myFlag) {
  133.                         flag = myFlag;
  134.                 }
  135.                 public void run(){
  136.                         Canvas canvas = null;
  137.                         while(flag) {
  138.                                 try {
  139.                                         canvas = myHolder.lockCanvas(null);
  140.                                         myDraw.onDraw(canvas);
  141.                                 }
  142.                                 finally{
  143.                                         if(canvas != null){
  144.                                                 myHolder.unlockCanvasAndPost(canvas);
  145.                                         }
  146.                                 }
  147.                         }
  148.                 }
  149.         }
  150. }
  151.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4
jihadrh
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Aug 03, 2010 7:18 am

Top

Re: code for ball animation using SurfaceView

Postby CrazyJay09 » Sun Aug 15, 2010 11:36 pm

Very helpful, thanks for posting
CrazyJay09
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Aug 04, 2010 3:23 am

Re: code for ball animation using SurfaceView

Postby spring » Mon Aug 16, 2010 6:23 am

good sinppet
spring
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Jan 09, 2010 4:37 am
Location: China

Re: code for ball animation using SurfaceView

Postby rosebeat » Thu Sep 09, 2010 6:53 am

Hey
this is the first nice code that ran in my android device without any issue.
Thank you so much
:)
rosebeat
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Sep 08, 2010 6:06 am

Re: code for ball animation using SurfaceView

Postby DKDiveDude » Mon Dec 13, 2010 6:30 pm

Funny I get two errors in Eclipse after having created a new project;

1) import android.view.<span class="posthilit">SurfaceView</span>;
"Syntax error on tokens, Simplename expected instead"
I eliminated the "span class" for now to bypass

2) bmp = BitmapFactory.decodeResource(getResources(), R.drawable.spin);
"spin cannot be resolved or is not a field

I am really new to Android and Java so please someone provide a helping hint, thanks!
DKDiveDude
Junior Developer
Junior Developer
 
Posts: 18
Joined: Mon Dec 13, 2010 2:40 pm

Re: code for ball animation using SurfaceView

Postby DKDiveDude » Mon Dec 13, 2010 6:47 pm

DKDiveDude wrote:Funny I get two errors in Eclipse after having created a new project;

1) import android.view.<span class="posthilit">SurfaceView</span>;
"Syntax error on tokens, Simplename expected instead"
I eliminated the "span class" for now to bypass

2) bmp = BitmapFactory.decodeResource(getResources(), R.drawable.spin);
"spin cannot be resolved or is not a field

I am really new to Android and Java so please someone provide a helping hint, thanks!



Ok forget about the first error as it seemed that the java code view contained part of the underlying HTML.

Now just want to find out about the "spin" error
DKDiveDude
Junior Developer
Junior Developer
 
Posts: 18
Joined: Mon Dec 13, 2010 2:40 pm

Top

Re: code for ball animation using SurfaceView

Postby blundell » Wed Dec 15, 2010 10:01 am

It's looking for an image file in res/drawable/ called spin.png probably, just add one.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 7 guests