.:: Jasa Membuat Aplikasi Website,Desktop,Android Order Now..!! | | Order Now..!! Jasa Membuat Project Arduino,Robotic,Print 3D ::.

Android Animation App - Bouncing Ball

0 komentar


بِسْــــــــــــــــمِ اﷲِالرَّحْمَنِ اارَّحِيم
bismillaahirrahmaanirrahiim

السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
Assalamu'alaikum warahmatullahi wabarakatuh

So looking for play with animation stuff.. good!! you are on right place  :)
today we are going to learn how to create simple android animation app.
so in this application we will create Background View and Ball programatically which will jump.

let's began the code, first of all we need to create simple android app having single Activity
name is :
BouncingBallActivity.java


  1. package com.mytest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6.   
  7. public class BouncingBallActivity extends Activity {  
  8.     /** Called when the activity is first created. */  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         //setContentView(R.layout.main);  
  13.           
  14.         View bouncingBallView = new BouncingBallView(this);  
  15.         setContentView(bouncingBallView);  
  16.     }  
  17. }  

Now we need to create a View programatically [we will not using any xml file]
here we go..
BouncingBallView.java
  1.     
  2. package com.mytest;  
  3.     
  4. import android.content.Context;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.Paint;  
  8. import android.graphics.RectF;  
  9. import android.view.KeyEvent;  
  10. import android.view.View;  
  11.      
  12. public class BouncingBallView extends View {  
  13.    private int xMin = 0;          // This view's bounds  
  14.    private int xMax;  
  15.    private int yMin = 0;  
  16.    private int yMax;  
  17.    private float ballRadius = 40// Ball's radius  
  18.    private float ballX = ballRadius + 20;  // Ball's center (x,y)  
  19.    private float ballY = ballRadius + 40;  
  20.    private float ballSpeedX = 5;  // Ball's speed (x,y)  
  21.    private float ballSpeedY = 3;  
  22.    private RectF ballBounds;      // Needed for Canvas.drawOval  
  23.    private Paint paint;           // The paint (e.g. style, color) used for drawing  
  24.      
  25.    // Constructor  
  26.    public BouncingBallView(Context context) {  
  27.       super(context);  
  28.       ballBounds = new RectF();  
  29.       paint = new Paint();  
  30.         
  31.       //to enable keypad  
  32.       this.setFocusable(true);  
  33.       this.requestFocus();  
  34.    }  
  35.     
  36.    // Called back to draw the view. Also called by invalidate().  
  37.    @Override  
  38.    protected void onDraw(Canvas canvas) {  
  39.       // Draw the ball  
  40.       ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);  
  41.       paint.setColor(Color.GREEN);  
  42.       canvas.drawOval(ballBounds, paint);  
  43.           
  44.       // Update the position of the ball, including collision detection and reaction.  
  45.       update();  
  46.     
  47.       // Delay  
  48.       try {    
  49.          Thread.sleep(60);    
  50.       } catch (InterruptedException e) { }  
  51.         
  52.       invalidate();  // Force a re-draw  
  53.    }  
  54.      
  55.    // Detect collision and update the position of the ball.  
  56.    private void update() {  
  57.       // Get new (x,y) position  
  58.      // ballX += ballSpeedX;  
  59.       ballY += ballSpeedY;  
  60.       // Detect collision and react  
  61.       if (ballX + ballRadius > xMax) {  
  62.          ballSpeedX = -ballSpeedX;  
  63.          ballX = xMax-ballRadius;  
  64.       } else if (ballX - ballRadius < xMin) {  
  65.          ballSpeedX = -ballSpeedX;  
  66.          ballX = xMin+ballRadius;  
  67.       }  
  68.       if (ballY + ballRadius > yMax) {  
  69.          ballSpeedY = -ballSpeedY;  
  70.          ballY = yMax - ballRadius;  
  71.       } else if (ballY - ballRadius < yMin) {  
  72.          ballSpeedY = -ballSpeedY;  
  73.          ballY = yMin + ballRadius;  
  74.       }  
  75.    }  
  76.      
  77.    // Called back when the view is first created or its size changes.  
  78.    @Override  
  79.    public void onSizeChanged(int w, int h, int oldW, int oldH) {  
  80.       // Set the movement bounds for the ball  
  81.       xMax = w-1;  
  82.       yMax = h-1;  
  83.    }  
  84.      
  85.    // key-up event handler  
  86.    @Override  
  87.    public boolean onKeyUp(int keyCode, KeyEvent event) {  
  88.       switch (keyCode) {  
  89.          case KeyEvent.KEYCODE_DPAD_RIGHT: // Increase rightward speed  
  90.             ballSpeedX++;  
  91.             break;  
  92.          case KeyEvent.KEYCODE_DPAD_LEFT:  // Increase leftward speed  
  93.             ballSpeedX--;  
  94.             break;  
  95.          case KeyEvent.KEYCODE_DPAD_UP:    // Increase upward speed  
  96.             ballSpeedY--;  
  97.             break;  
  98.          case KeyEvent.KEYCODE_DPAD_DOWN:  // Increase downward speed  
  99.             ballSpeedY++;  
  100.             break;  
  101.          case KeyEvent.KEYCODE_DPAD_CENTER: // Stop  
  102.             ballSpeedX = 0;  
  103.             ballSpeedY = 0;  
  104.             break;  
  105.          case KeyEvent.KEYCODE_A:    // Zoom in  
  106.             // Max radius is about 90% of half of the smaller dimension  
  107.             float maxRadius = (xMax > yMax) ? yMax / 2 * 0.9f  : xMax / 2 * 0.9f;  
  108.             if (ballRadius < maxRadius) {  
  109.                ballRadius *= 1.05;   // Increase radius by 5%  
  110.             }  
  111.             break;  
  112.          case KeyEvent.KEYCODE_Z:    // Zoom out  
  113.             if (ballRadius > 20) {   // Minimum radius  
  114.                ballRadius *= 0.95;   // Decrease radius by 5%  
  115.             }  
  116.             break;  
  117.       }  
  118.       return true;  // Event handled  
  119.    }  
  120.      
  121.      
  122. }  
at last our manifest file should look like this..
AndroidManifest.xml

  1.     
  2.   
  3. <manifest android:versioncode="1" android:versionname="1.0" package="com.mytest" xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <uses-sdk android:minsdkversion="8">  
  5.   
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:label="@string/app_name" android:name=".BouncingBallActivity">  
  8.             <intent-filter>  
  9.                 <action android:name="android.intent.action.MAIN">  
  10.                 <category android:name="android.intent.category.LAUNCHER">  
  11.             </category></action></intent-filter>  
  12.         </activity>  
  13.   
  14.     </application>  
  15. </uses-sdk></manifest>  

output is like this

we have done great job on ma B'day!!   :D

cheers!!

I'd love to here your thoughts!

Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
NB :: Bila Sobat tertarik Ingin membuat software, membeli software, membeli source code, membeli hardware elektronika untuk kepentingan Perusahaan maupun Tugas Akhir (TA/SKRIPSI), Insyaallah Saya siap membantu, untuk Respon Cepat dapat menghubungi kami, melalui :

No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com


atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :

ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِين
Alhamdulilah hirobil alamin

وَ السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
wassalamualaikum warahmatullahi wabarakatuh


Artikel Android Animation App - Bouncing Ball , Diterbitkan oleh scodeaplikasi pada Minggu, 23 Desember 2012. Semoga artikel ini dapat menambah wawasan Anda. Website ini dipost dari beberapa sumber, bisa cek disini sumber, Sobat diperbolehkan mengcopy paste / menyebar luaskan artikel ini, karena segala yang dipost di public adalah milik public. Bila Sobat tertarik Ingin membuat software, membeli software, membeli source code ,Dengan Cara menghubungi saya Ke Email: Fajarudinsidik@gmail.com, atau No Hp/WA : (fajar) : 085369237896, (puat) : 085267792168.

Tawk.to