Showing posts with label Style. Show all posts
Showing posts with label Style. Show all posts

Thursday, July 7, 2011

Android - Animations

Page Transitions

To override the current page transitions when switching activities you can use the overridePendingTransition method. The code snippet shows an example of using the pre-defined Android animations fade_in and fade_out. We need to provide resource ids to the overridePendingTransition method representing the incoming activity and outgoing activity animations.

 Intent i = new Intent(this, ListViewExampleActivity.class);  
 startActivity(i);  
 overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);  


View Animations

In this example we will make our own custom animation. The easiest way to create your own animation xml file in Eclipse follows:

1) Right click your project in the Package Explorer
2) New -> Android XML File
3) Select "Animation" as your type, this will place your xml into "res/anim"

The following code shows an example of a scale animation. This animation is using the bounce interpolator. This is much like easing functions in Silverlight. This causes the animation to have a bounce effect. The animation defines the from and to scales, starting at 0 and ending at 1. It also moves the X and Y pivots to 50% so that as the element scales up it scales from the center of the object. Finally the duration is set to 500 milliseconds.

 <?xml version="1.0" encoding="utf-8"?>  
 <set xmlns:android="http://schemas.android.com/apk/res/android"  
       android:interpolator="@android:anim/bounce_interpolator">  
   <scale   
       android:fromXScale="0.0" android:toXScale="1.0"   
       android:fromYScale="0.0" android:toYScale="1.0"  
       android:pivotX="50%" android:pivotY="50%"  
       android:fillAfter="true"   
       android:duration="500">  
   </scale>  
 </set>  

To apply this animation use the following code:

 Button animBttn = (Button)findViewById(R.id.AnimationButton);  
 animBttn.setOnClickListener(this);  
       
 Animation bttnAnim = AnimationUtils.loadAnimation(mContext, R.anim.button_animation);  
 animBttn.setAnimation(bttnAnim);  

If you would like to trigger the animation we can use the startAnimation method.

 animBttn.startAnimation(bttnAnim);  


List View Animations

This time we will use a translate animation.

 <?xml version="1.0" encoding="utf-8"?>  
 <set xmlns:android="http://schemas.android.com/apk/res/android"  
       android:interpolator="@android:anim/overshoot_interpolator" >  
      <translate   
           android:fromXDelta="-100%"  
           android:toXDelta="0%"  
           android:duration="500">  
      </translate>  
 </set>  

In order to apply this animation to individual list view items we must also create a layoutAnimation xml file.

 <?xml version="1.0" encoding="utf-8"?>  
 <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"   
                  android:animation="@anim/listitem_animation"/>  

To attach the animation to the ListView we will use the setLayoutAnimation method.

 ListView lv = (ListView)findViewById(R.id.ExampleList);  
 lv.setAdapter(new ArrayAdapter<String>(this, R.layout.listitem, PLANETS));  
       
 //We need to load a LayoutAnimation instead of just an Animation   
 //to set the animation for each individual list item  
 LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(mContext,R.anim.list_layoutcontroller);  
 //This is the delay between each animation as a fraction of the animation duration  
 controller.setDelay(0.25f);  
 //Be sure to set the LayoutAnimation, using setAnimation will set the animation for the entire listview  
 lv.setLayoutAnimation(controller);  

Thursday, May 19, 2011

Android - Full Screen App

By default an Android app has a title bar and allows the user to pull down the task bar while your app is open.


The following code allows you to remove the title bar and hide the task bar. This would be your onCreate method in your activity.

 public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //No title   
        requestWindowFeature(Window.FEATURE_NO_TITLE);   
        //Full screen (no task bar)  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);   
        setContentView(R.layout.main);  
 }  

Result:

Monday, May 16, 2011

Android – Styling the ProgressDialog Bar

This is the default gray and yellow progress bar:








I want my progress bar to look like this:










Steps to accomplish this:


1) Create a layer list xml file in your drawable folder to hold the shapes and gradients for each part of the progress bar.


my_progress_bar.xml
1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
3:  <item android:id="@android:id/background">  
4:    <shape>  
5:      <corners android:radius="5dip" />  
6:      <gradient  
7:          android:startColor="#69A0C6"  
8:          android:endColor="#446F8D"  
9:          android:angle="270"/>  
10:    </shape>  
11:  </item>  
12:  <item android:id="@android:id/secondaryProgress">  
13:    <clip>  
14:      <shape>  
15:        <corners android:radius="5dip" />  
16:        <gradient  
17:          android:startColor="#B963BA"  
18:          android:endColor="#A05AA1"  
19:          android:angle="270" />  
20:      </shape>  
21:    </clip>  
22:  </item>  
23:  <item android:id="@android:id/progress">  
24:    <clip>  
25:      <shape>  
26:        <corners  
27:          android:radius="5dip" />  
28:        <gradient  
29:          android:startColor="#911793"  
30:          android:endColor="#5B115C"  
31:          android:angle="270" />  
32:      </shape>  
33:    </clip>  
34:  </item>  
35:  </layer-list>  
In this example the blue color is the background and the purple is the progress.


2) Set the progress drawable in your Java code.

 dialog.setProgressDrawable(c.getResources().getDrawable(R.drawable.my_progress_bar));