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 LayoutAnimation5 extends Activity { 34 @Override onCreate(Bundle savedInstanceState)35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 38 loadApps(); 39 40 setContentView(R.layout.layout_animation_5); 41 GridView grid = (GridView) findViewById(R.id.grid); 42 grid.setAdapter(new AppsAdapter()); 43 } 44 45 private List<ResolveInfo> mApps; 46 loadApps()47 private void loadApps() { 48 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 49 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 50 51 mApps = getPackageManager().queryIntentActivities(mainIntent, 0); 52 } 53 54 public class AppsAdapter extends BaseAdapter { getView(int position, View convertView, ViewGroup parent)55 public View getView(int position, View convertView, ViewGroup parent) { 56 ImageView i = new ImageView(LayoutAnimation5.this); 57 58 ResolveInfo info = mApps.get(position % mApps.size()); 59 60 i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager())); 61 i.setScaleType(ImageView.ScaleType.FIT_CENTER); 62 final int w = (int) (36 * getResources().getDisplayMetrics().density + 0.5f); 63 i.setLayoutParams(new GridView.LayoutParams(w, w)); 64 return i; 65 } 66 67 getCount()68 public final int getCount() { 69 return Math.min(32, mApps.size()); 70 } 71 getItem(int position)72 public final Object getItem(int position) { 73 return mApps.get(position % mApps.size()); 74 } 75 getItemId(int position)76 public final long getItemId(int position) { 77 return position; 78 } 79 } 80 81 } 82