Sunday, September 18, 2016

Frame Animation in Android @ Make a Movie Clip

Hi .... Dear All ....  Today I am uploading a superb example of Frame Animation.
In this example i have added multiple images which shows in a Frame by using  Animation.
         Frame animations build up animation from a series of still images that are shown in rapid succession, like a film reel.
To Add frame Animation I have five gif images in my drawable folder.
Also create a xml file named  frame_animation.xml as
<?xml version="1.0" encoding="utf-8"?>

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"

    android:oneshot="false">
    <item android:drawable="@drawable/p1" android:duration="1000" />
    <item android:drawable="@drawable/p2" android:duration="1000" />
    <item android:drawable="@drawable/p3" android:duration="1000" />
    <item android:drawable="@drawable/p4" android:duration="1000" />
    <item android:drawable="@drawable/p5" android:duration="1000" />
</animation-list>
A. The output will be like 
a. 




B. The main layout file is as 
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context="com.exam.ravi.frameanimation.MainActivity">
    <Button        android:id="@+id/startButton"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/start"        />
    <ImageView        android:contentDescription="@string/desc"

        android:id="@+id/animateImage"

        android:layout_width="300dp"

        android:layout_height="300dp"

        android:layout_gravity="center_horizontal"/>

</LinearLayout>
C. Java Code is as 
package com.exam.ravi.frameanimation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    Button button;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.startButton);
        button.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {
                     ImageView imgView = (ImageView)findViewById(R.id.animateImage);
                        imgView.setVisibility(ImageView.VISIBLE);
                        imgView.setBackgroundResource(R.drawable.frame_animation);

                        AnimationDrawable frameAnimation =
                                (AnimationDrawable) imgView.getBackground();

                        if (frameAnimation.isRunning()) {
                            frameAnimation.stop();
                        }
                        else {
                            frameAnimation.stop();
                            frameAnimation.start();
                        }
                    }
                });
    }
} 

Saturday, September 17, 2016

Tween Animation in Android - Mini App

Hi .... Dear All ....  Today I am uploading a superb example of Tween Animation.
In this example i have added multiple buttons which shows you different type of Animation.
Tween Animation is base on different properties like 
• Orientation
• Scale
• Position
• Opacity
on the basis of these attributes Tween animation is categorized as
• Translate Animation
• Rotate Animation
• Alpha Animation
• Scale Animation  
Lets see an example 
A. Main Output screen will be like 


First Button Click 


Second Button Clock 


Third Button Click 


You can check different kind of animation by clicking on buttons
B.   XML Files 
B-1
@ You have to add one xml file to see the rotate effect. for this 
create a new directory in res folder named as anim -> rotateanim.xml is as 
<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:shareInterpolator="true">
    <rotate        android:fromDegrees="0"        android:toDegrees="360"

        android:duration="5000"

             android:pivotX="300"        android:pivotY="300"        />

</set>

The main activity layout file is as 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"    
    android:orientation="vertical"    android:id="@+id/layout"    
         tools:context="com.exam.ravi.animexppt.MainActivity">

    <TextView        android:id="@+id/rotatetext"    
             android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Text Will be Rotated" /> 
          <Button
             android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/button1"

        android:text="Rotate Text"        android:onClick="startAnimation"/>
    <TextView        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/scrolltext"        android:lines="1"

        android:text="Will be scrolled to the Left"

        android:scrollHorizontally="true"/>
    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/button2"        android:text="Animate to Left + Bounce"

        android:onClick="startAnimation"/>
    <TextView        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/fadeout"        android:lines="1"

        android:text="Will be fade out/in"

        android:scrollHorizontally="true"/>
    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"        android:id="@+id/button3"
  
             android:text="Fade Animation"

        android:onClick="startAnimation"/>
    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/button4"

        android:text=" Remove Component"

        android:onClick="startAnimation"/>
</LinearLayout>

C. Java Source code is as 
MainActivity.java    
package com.exam.ravi.animexppt;

import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity 
          implements Animation.AnimationListener {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
   public void startAnimation(View view)
   {
      switch(view.getId())
      {
          case R.id.button1:
          {
              Animation animation1 = AnimationUtils.loadAnimation(this, 
                                          R.anim.rotateanim);

              animation1.setAnimationListener(this);
              View animatedView1 = findViewById(R.id.rotatetext);
              animatedView1.startAnimation(animation1);
              break;
          }
          case R.id.button2:
          {
              Paint paint = new Paint();
              TextView animatedView2 = (TextView) findViewById(R.id.scrolltext);
              float measureTextCenter= paint.measureText(animatedView2.
                                            getText().toString());
              Animation animation2 =new TranslateAnimation(0f,
                                         -measureTextCenter,0.0f,0.0f);
                   animation2.setDuration(5000);
              animation2.setInterpolator(new BounceInterpolator());
              animatedView2.startAnimation(animation2);
              break;
          }
          case R.id.button3:
          {
              TextView animatedView3 = (TextView) findViewById(R.id.fadeout);
              float from =  1.0f;
              float to =0.0f;
              if(animatedView3.getVisibility()==View.INVISIBLE)
              {
                  from=to;
                  to=1.0f;
              }
              Animation animation3 =new AlphaAnimation(from,to);
              animation3.setDuration(5000);
              animation3.setAnimationListener(this);
              animatedView3.startAnimation(animation3);
              break;
          }
          case R.id.button4:
          {
              ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
              Animation animation4 =new AlphaAnimation(0.0f,1.0f);
              animation4.setDuration(5000);
              LayoutAnimationController controller = new 
                                          LayoutAnimationController(animation4,0);
              layout.setLayoutAnimation(controller);
              View button = findViewById(R.id.button3);
              try              {
                   if(button!=null)
                       layout.removeView(button);
              }
              catch (Exception ex)
              {
                  ex.printStackTrace();
              }
              break;
          }
          default: break;
      }
   }


    @Override

    public void onAnimationStart(Animation animation) {

    }

    @Override

    public void onAnimationEnd(Animation animation) {

    }

    @Override

    public void onAnimationRepeat(Animation animation) {

    }
}

Animation/ Interpolator in Android - Bouncing Ball

Hi .... Dear All ....  Today I am uploading a superb example of Bouncing Ball.
In this example i use Bouncing Interpolator to add animation.

Interpolator 

• Interpolation is rate of change of the animation.
• The animation framework needs to know how to calculate the current state of the View or Object at any instant of time.
• Interpolators define this property that lets animation classes calculate the state of a view.
• Interpolators are animation defined in XML.
• Animation effects can be repeated, accelerated, decelerated, bounced etc.
• Every animation needs an interpolator
Interpolators are separate classes that control animations

A. The output will be 


B.  XML file 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context="com.exam.ravi.animexppt.Main2Activity">
    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:id="@+id/the_button"

        android:text="Bounce Ball" ></Button>

    <ImageView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/imageView"

        android:layout_gravity="center_horizontal"

        android:src="@drawable/ball" />

 </LinearLayout>

C. Java Code 

package com.exam.ravi.animexppt;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;


public class Main2Activity extends AppCompatActivity  implements View.OnClickListener 
{
    Button button;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        button = (Button) findViewById(R.id.the_button);
        button.setOnClickListener(this);
    }

    @Override

    public void onClick(View v) {
        ImageView  img  = (ImageView) findViewById(R.id.imageView);
        img.clearAnimation();
        TranslateAnimation translation;
        translation = new TranslateAnimation(0f, 0f, 0f,600f);
        translation.setDuration(2000);
        translation.setFillAfter(true);
        translation.setInterpolator(new BounceInterpolator());
        img.startAnimation(translation);
    }

}

Friday, September 2, 2016

Bound Service - Committed with someone


Hi .... Dear All ....  Today I am uploading a superb example of Bound Service .

 A bound service is the server in a client-server interface.

 A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC).

 A bound service typically lives only while it serves another application component and does not run in the background indefinitely.

Bound services are created as sub-classes of the Android Service class and must, at a minimum, implement the onBind() method. 

This method returns an IBinder object that defines the programming interface that clients can use to interact with the service. 

The bindService() method returns immediately without a value, but when the Android system creates the connection between the client and service, it calls onServiceConnected() on the ServiceConnection, to deliver the IBinder that the client can use to communicate with the service.

Lets see an example
A. Implement the Binder

public class BindServ extends Service
{
   
private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder
    {
        BindServ getMyServ()
        {
           
return BindServ.this;
        }
    }

   
@Nullable
    @Override
   
public IBinder onBind(Intent intent) {
       
return mBinder;
    }

    public void dis()
    {
        Toast.makeText(
this,"Hello finally Married",Toast.LENGTH_LONG).show();
    }
}

B. Preapre the Client

    public class BindingActivity extends AppCompatActivity {

    BindServ mService;

    boolean mBound = false;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.chapservices1);

    }



    @Override

    protected void onStart() {

        super.onStart();

        Intent intent = new Intent(this,BindServ.class);

        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

    }



    @Override

    protected void onStop() {

        super.onStop();

        if(mBound)

        {

            unbindService(mConnection);

            mBound=false;

        }

    }

    public void startMyService(View view)

    {

        if(mBound)

        {

              mService.dis();

        }

    }

    private ServiceConnection mConnection = new ServiceConnection() {

        @Override

        public void onServiceConnected(ComponentName name, IBinder service) {

            BindServ.LocalBinder binder = (BindServ.LocalBinder)service;

            mService = binder.getMyServ();

            mBound=true;

        }



        @Override

        public void onServiceDisconnected(ComponentName name) {

            mBound=false;

        }

    };

}

C. Output will be like