android抽屜實現

今天在手機上實現瞭抽屜效果,其實很簡單,但是效果卻很酷。
 
           首先在layout 下設置xml佈局文件
      
 
 
<?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.aiwalls.com
 
        SlidingDrawer就是重要的抽屜控件 ,handle是抽屜的拖動按鈕,content是抽屜中的內容。
      然後建立chouti的activity類:
 
 
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 = (SlidingDrawer) findViewById(R.id.sliding); 
        iv = (ImageView) findViewById(R.id.imageViewIcon); 
        gv.setAdapter(new GridAdapter()); 
        sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()// 開抽屜 
        { 
            @Override 
            public void onDrawerOpened() { 
                iv.setImageResource(R.drawable.touch_handler);// 響應開抽屜事件 
                                                                // ,把圖片設為向下的 
            } 
        }); 
        sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() { 
            @Override 
            public void onDrawerClosed() { 
                iv.setImageResource(R.drawable.touch_handler);// 響應關抽屜事件 
            } 
        }); 
    } 
 
    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; 
        } 
 
    } 

 
loadApps方法是得到主界面上的圖片和文字。
   然後設置的自定義adapter中去。
 
    看下運行後效果:
 
   
     向上滑動imageview按鈕後:
 
  
   
    為瞭體現更好的效果,可以用兩張滑動圖片,一張朝上的,一張朝下的。根據監聽器做相應的切換

摘自 wangkuifeng0118的專欄

發佈留言