1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.view;
18 
19 import com.example.android.apis.R;
20 
21 import android.app.Activity;
22 import android.content.Intent;
23 import android.content.pm.ResolveInfo;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.BaseAdapter;
28 import android.widget.GridView;
29 import android.widget.ImageView;
30 
31 import java.util.List;
32 
33 public class LayoutAnimation4 extends Activity {
34     @Override
onCreate(Bundle savedInstanceState)35     public void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37 
38         loadApps();
39 
40         setContentView(R.layout.layout_animation_4);
41         GridView grid = (GridView) findViewById(R.id.grid);
42         grid.setAdapter(new AppsAdapter());
43 
44     }
45 
46     private List<ResolveInfo> mApps;
47 
loadApps()48     private void loadApps() {
49         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
50         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
51 
52         mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
53     }
54 
55     public class AppsAdapter extends BaseAdapter {
getView(int position, View convertView, ViewGroup parent)56         public View getView(int position, View convertView, ViewGroup parent) {
57             ImageView i = new ImageView(LayoutAnimation4.this);
58 
59             ResolveInfo info = mApps.get(position % mApps.size());
60 
61             i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
62             i.setScaleType(ImageView.ScaleType.FIT_CENTER);
63             final int w = (int) (36 * getResources().getDisplayMetrics().density + 0.5f);
64             i.setLayoutParams(new GridView.LayoutParams(w, w));
65             return i;
66         }
67 
68 
getCount()69         public final int getCount() {
70             return Math.min(32, mApps.size());
71         }
72 
getItem(int position)73         public final Object getItem(int position) {
74             return mApps.get(position % mApps.size());
75         }
76 
getItemId(int position)77         public final long getItemId(int position) {
78             return position;
79         }
80     }
81 }
82