Sunday 12 April 2015

Display Animated Gif Android

 
Step1)put a Gif image in drawable folder 
 
Step2)Make GifViewActivity and GifViewClass as follows

public class GifViewActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GifViewClass(this));
    }

    private class GifViewClass extends View {
        Movie movie;

        GifViewClass(Context context) {
            super(context);
            //Gif image from drawable folder
            movie = Movie.decodeStream(
                    context.getResources().openRawResource(
                            R.drawable.gif_image));
        }
        @Override
        protected void onDraw(Canvas canvas) {   
            if (movie != null) {
                movie.setTime(
                    (int) SystemClock.uptimeMillis() % movie.duration());
                movie.draw(canvas, 0, 0);
                invalidate();
            }
        }
    }
}

Android animation using xml

Step to perform animation in android apps

Step 1) Create XML which define animation
create a xml file which define the animation in  res ⇒ anim ⇒ animation.xml , If you dont have anim folder then create it in res directory

blink.xml
<?xml version="1.0" encoding="utf-8"?>
    <alpha android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="800"
        android:repeatMode="reverse"
        android:repeatCount="infinite"/>
</set>
   
Step 2) Load the animation and set to view in acitvity to see blink effect

public class BlinkActivity extends Activity{
 
    TextView textMessage;
 
    // Animation
    Animation animBlink;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blink);
 
        textMessage = (TextView) findViewById(R.id.textMessage);
               textMessag.setText(Welcome to Android Code Portal);
        // load the animation
        animBlink = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.blink);  
       // start the animation
       textMessage.startAnimation(animBlink);
     
    }
}
 
Some other useful animations as follows
  
1)Blink
blink.xml
<?xml version="1.0" encoding="utf-8"?>
    <alpha android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="800"
        android:repeatMode="reverse"
        android:repeatCount="infinite"/>
</set>
 
2)ZoomIn
zoomin.xml
<?xml version="1.0" encoding="utf-8"?>
    android:fillAfter="true" >
 
    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="3"
        android:toYScale="3" >
    </scale>
 
</set>
 
 3)ZoomOut
zoomout.xml
<?xml version="1.0" encoding="utf-8"?>
    android:fillAfter="true" >
 
    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.5"
        android:toYScale="0.5" >
    </scale>
 
</set>
 
4)FadeIn
fadein.xml
<?xml version="1.0" encoding="utf-8"?>
    android:fillAfter="true" >
 
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />
 
</set> 
 
5)FadeOut
fadeout.xml
<?xml version="1.0" encoding="utf-8"?>
    android:fillAfter="true" >
 
    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />
 
</set>
 
6)Rotate
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
    <rotate android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="700"
        android:repeatMode="restart"
        android:repeatCount="infinite"
        android:interpolator="@android:anim/cycle_interpolator"/>
 
</set>
 
7)Move
 move.xml
<?xml version="1.0" encoding="utf-8"?>
<set
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">
 
   <translate
        android:fromXDelta="0%p"
        android:toXDelta="80%p"
        android:duration="1000" />
</set>
 
8)SlideDown
slidedown.xml
<?xml version="1.0" encoding="utf-8"?>
    android:fillAfter="true">
 
    <scale
        android:duration="800"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXScale="1.0"
        android:toYScale="1.0" />
 
</set>
 
 9)SlideUp
slideup.xml
<?xml version="1.0" encoding="utf-8"?>
    android:fillAfter="true" >
 
    <scale
        android:duration="800"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXScale="1.0"
        android:toYScale="0.0" />
 
</set>
 
10)Bounce
 bounce.xml
<?xml version="1.0" encoding="utf-8"?>
    android:fillAfter="true"
    android:interpolator="@android:anim/bounce_interpolator">
 
    <scale
        android:duration="800"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />
 
</set>
 
 
 

Save Activity State in Android

Save activity state in android apps (for example when we rotate our device portrait to landscape or landscape to portrait the values which we had filled in EditText gone away)

You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putBoolean("BooleanValueKey", true);
  savedInstanceState.putDouble("DoubleValueKey", 1.9);
  savedInstanceState.putInt("IntValueKey", 1);
  savedInstanceState.putString("StringValueKey", "Welcome to Android Code Portal");
  // etc.
}
 
The Bundle is essentially a way of storing a NVP ("Name-Value Pair") map, and it will 
get passed in to onCreate() and also onRestoreInstanceState() where you'd extract 
the values like this: 

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  boolean booleanValue = savedInstanceState.getBoolean("BooleanValueKey");
  double doubleValue = savedInstanceState.getDouble("DoubleValueKey");
  int intValue = savedInstanceState.getInt("IntValueKey");
  String stringValue = savedInstanceState.getString("StringValueKey");
}