Today, the drawer effect is achieved on the mobile phone. It is actually very simple, but the effect is very cool.
First, set the xml layout file under layout.
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<SlidingDrawer
Android: id = "@ + id/sliding"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: content = "@ + id/allApps"
Android: handle = "@ + id/imageViewIcon"
Android: orientation = "vertical">
<GridView
Android: id = "@ + id/allApps"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/bk"
Android: columnWidth = "60dp"
Android: gravity = "center"
Android: horizontalSpacing = "10dp"
Android: numColumns = "auto_fit"
Android: padding = "10dp"
Android: stretchMode = "columnWidth"
Android: verticalSpacing = "10dp"/>
<ImageView
Android: id = "@ + id/imageViewIcon"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/touch_handler"/>
</SlidingDrawer>
</RelativeLayout> www.2cto.com
SlidingDrawer is an important drawer control, handle is the drag button of the drawer, and content is the content in the drawer.
Then create the chouti activity class:
Import android. app. Activity;
Import android. content. Intent;
Import android. content. pm. ResolveInfo;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. GridView;
Import android. widget. ImageView;
Import android. widget. SlidingDrawer;
Public class Chouti extends Activity {
Private GridView gv;
Private SlidingDrawer sd;
Private ImageView iv;
Private List <ResolveInfo> apps;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. slidingdrawer );
LoadApps ();
Gv = (GridView) findViewById (R. id. allApps );
Sd = (s0000ingdrawer) findViewById (R. id. sliding );
Iv = (ImageView) findViewById (R. id. imageViewIcon );
Gv. setAdapter (new GridAdapter ());
Sd. setOnDrawerOpenListener (new s0000ingdrawer. OnDrawerOpenListener () // open the drawer
{
@ Override
Public void onDrawerOpened (){
Iv. setImageResource (R. drawable. touch_handler); // responds to Drawer Opening events
// Set the image to the lower
}
});
Sd. setOnDrawerCloseListener (new SlidingDrawer. OnDrawerCloseListener (){
@ Override
Public void onDrawerClosed (){
Iv. setImageResource (R. drawable. touch_handler); // responds to the close drawer event
}
});
}
Private void loadApps (){
Intent intent = new Intent (Intent. ACTION_MAIN, null );
Intent. addCategory (Intent. CATEGORY_LAUNCHER );
Apps = getPackageManager (). queryIntentActivities (intent, 0 );
}
Public class GridAdapter extends BaseAdapter {
Public GridAdapter (){
}
Public int getCount (){
// TODO Auto-generated method stub
Return apps. size ();
}
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return apps. get (position );
}
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
Public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
ImageView imageView = null;
If (convertView = null ){
ImageView = new ImageView (Chouti. this );
ImageView. setScaleType (ImageView. ScaleType. FIT_CENTER );
ImageView. setLayoutParams (new GridView. LayoutParams (50, 50 ));
} Else {
ImageView = (ImageView) convertView;
}
ResolveInfo ri = apps. get (position );
ImageView. setImageDrawable (ri. activityInfo
. LoadIcon (getPackageManager ()));
Return imageView;
}
}
}
The loadApps method is used to obtain images and text on the main interface.
Then go to the custom adapter.
Check the running effect:
After sliding the imageview button up:
To achieve better results, you can use two slide images, one facing up and the other facing down. Switch Based on listener
From the wangkuifeng0118 Column