android 实现home键启动并半透明程序背景效果显示全部应用程序
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent" > <activity android:name="com.example.myapp.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
app_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/relaGrid" android:gravity="center_horizontal" android:layout_width="80dp" android:paddingTop="5dp" android:layout_height="100dp"> <ImageView android:id="@+id/ivAppIcon" android:scaleType="fitCenter" android:layout_gravity="center_horizontal|top" android:layout_width="48dp" android:layout_height="48dp" /> <TextView android:id="@+id/tvAppName" android:layout_width="74dp" android:ellipsize="marquee" android:textColor="#FFFF" android:layout_height="50dp" android:gravity="center_horizontal|center_vertical" /> </LinearLayout> |
MainActivity
package com.example.myapp; import java.util.List; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { private List<ResolveInfo> mApps; GridView mGrid; private OnItemClickListener listener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position,long id) { ResolveInfo info = mApps.get(position); //该应用的包名 String pkg = info.activityInfo.packageName; //应用的主activity类 String cls = info.activityInfo.name; ComponentName componet = new ComponentName(pkg, cls); Intent i = new Intent(); i.setComponent(componet); startActivity(i); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loadApps(); setContentView(R.layout.activity_main); mGrid = (GridView)findViewById(R.id.apps_list); mGrid.setBackgroundColor(0xcc00cc00); mGrid.setAdapter(new ImageAdapter(this)); mGrid.setOnItemClickListener(listener); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } private void loadApps(){ Intent mainIntent = new Intent(Intent.ACTION_MAIN,null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); mApps = getPackageManager().queryIntentActivities(mainIntent,0); } private class ImageAdapter extends BaseAdapter { private LayoutInflater mInflater; public ImageAdapter(Context context) { this.mInflater = LayoutInflater.from(context); } /** * 获取当前Adapter的Items数目 */ public int getCount() { return mApps.size(); } /** * 获取相应position的Item */ public Object getItem(int position) { return mApps.get(position); } /** * 获取相应position的Item在List中的row id */ public long getItemId(int position) { return position; } /** * position就是位置从0开始 * convertView是GridView每一项要显示的内容,也就是 R.layout.app_item中所定义的布局 * parent就是父窗体GridView */ public View getView(int position, View convertView, ViewGroup parent) { System.out.println("==Pos:"+position); ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.app_item, null); holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.tvAppName); holder.icon = (ImageView) convertView.findViewById(R.id.ivAppIcon); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } ResolveInfo info = mApps.get(position); //holder.text.setTextColor(android.graphics.Color.BLUE); holder.text.setText(info.activityInfo.loadLabel(getPackageManager())); holder.icon.setImageDrawable(info.activityInfo.loadIcon(getPackageManager())); return convertView; } class ViewHolder { TextView text; ImageView icon; } } public class AppsAdapter extends BaseAdapter { public AppsAdapter() { } public View getView(int position, View convertView, ViewGroup parent) { ImageView i; if (convertView == null) { i = new ImageView(MainActivity.this); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new GridView.LayoutParams(50, 50)); } else { i = (ImageView) convertView; } ResolveInfo info = mApps.get(position); i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager())); return i; } public final int getCount() { return mApps.size(); } public final Object getItem(int position) { return mApps.get(position); } public final long getItemId(int position) { return position; } } } |
#p#分页标题#e#