⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 home.java

📁 android 例子中的Home。很有代表意义
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (resolveInfo == null) {            return null;        }        final ApplicationInfo info = new ApplicationInfo();        final ActivityInfo activityInfo = resolveInfo.activityInfo;        info.icon = activityInfo.loadIcon(manager);        if (info.title == null || info.title.length() == 0) {            info.title = activityInfo.loadLabel(manager);        }        if (info.title == null) {            info.title = "";        }        return info;    }    /**     * When the notification that favorites have changed is received, requests     * a favorites list refresh.     */    private void onFavoritesChanged() {        bindFavorites(false);    }    @Override    public boolean dispatchKeyEvent(KeyEvent event) {        if (event.getAction() == KeyEvent.ACTION_DOWN) {            switch (event.getKeyCode()) {                case KeyEvent.KEYCODE_BACK:                    return true;                case KeyEvent.KEYCODE_HOME:                    return true;            }        }        return super.dispatchKeyEvent(event);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        super.onCreateOptionsMenu(menu);        menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)                 .setIcon(R.drawable.ic_menu_gallery)                 .setAlphabeticShortcut('W');        menu.add(0, MENU_SEARCH, 0, R.string.menu_search)                .setIcon(android.R.drawable.ic_search_category_default)                .setAlphabeticShortcut(SearchManager.MENU_KEY);        menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)                .setIcon(R.drawable.ic_menu_preferences)                .setIntent(new Intent(android.provider.Settings.ACTION_SETTINGS));        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {            case MENU_WALLPAPER_SETTINGS:                startWallpaper();                return true;            case MENU_SEARCH:                onSearchRequested();                return true;        }        return super.onOptionsItemSelected(item);    }    private void startWallpaper() {        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);        startActivity(Intent.createChooser(pickWallpaper, getString(R.string.menu_wallpaper)));    }    /**     * Loads the list of installed applications in mApplications.     */    private void loadApplications(boolean isLaunching) {        if (isLaunching && mApplications != null) {            return;        }        PackageManager manager = getPackageManager();        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);        final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);        Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));        if (apps != null) {            final int count = apps.size();            if (mApplications == null) {                mApplications = new ArrayList<ApplicationInfo>(count);            }            mApplications.clear();            for (int i = 0; i < count; i++) {                ApplicationInfo application = new ApplicationInfo();                ResolveInfo info = apps.get(i);                application.title = info.loadLabel(manager);                application.setActivity(new ComponentName(                        info.activityInfo.applicationInfo.packageName,                        info.activityInfo.name),                        Intent.FLAG_ACTIVITY_NEW_TASK                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);                application.icon = info.activityInfo.loadIcon(manager);                mApplications.add(application);            }        }    }    /**     * Shows all of the applications by playing an animation on the grid.     */    private void showApplications(boolean animate) {        if (mBlockAnimation) {            return;        }        mBlockAnimation = true;        mShowApplicationsCheck.toggle();        if (mShowLayoutAnimation == null) {            mShowLayoutAnimation = AnimationUtils.loadLayoutAnimation(                    this, R.anim.show_applications);        }        // This enables a layout animation; if you uncomment this code, you need to        // comment the line mGrid.startAnimation() below//        mGrid.setLayoutAnimationListener(new ShowGrid());//        mGrid.setLayoutAnimation(mShowLayoutAnimation);//        mGrid.startLayoutAnimation();        if (animate) {            mGridEntry.setAnimationListener(new ShowGrid());            mGrid.startAnimation(mGridEntry);        }        mGrid.setVisibility(View.VISIBLE);        if (!animate) {            mBlockAnimation = false;        }        // ViewDebug.startHierarchyTracing("Home", mGrid);    }    /**     * Hides all of the applications by playing an animation on the grid.     */    private void hideApplications() {        if (mBlockAnimation) {            return;        }        mBlockAnimation = true;        mShowApplicationsCheck.toggle();        if (mHideLayoutAnimation == null) {            mHideLayoutAnimation = AnimationUtils.loadLayoutAnimation(                    this, R.anim.hide_applications);        }        mGridExit.setAnimationListener(new HideGrid());        mGrid.startAnimation(mGridExit);        mGrid.setVisibility(View.INVISIBLE);        mShowApplications.requestFocus();        // This enables a layout animation; if you uncomment this code, you need to        // comment the line mGrid.startAnimation() above//        mGrid.setLayoutAnimationListener(new HideGrid());//        mGrid.setLayoutAnimation(mHideLayoutAnimation);//        mGrid.startLayoutAnimation();    }    /**     * Receives intents from other applications to change the wallpaper.     */    private class WallpaperIntentReceiver extends BroadcastReceiver {        @Override        public void onReceive(Context context, Intent intent) {            getWindow().setBackgroundDrawable(getWallpaper());        }    }    /**     * Receives notifications when applications are added/removed.     */    private class ApplicationsIntentReceiver extends BroadcastReceiver {        @Override        public void onReceive(Context context, Intent intent) {            loadApplications(false);            bindApplications();            bindRecents();            bindFavorites(false);        }    }    /**     * Receives notifications whenever the user favorites have changed.     */    private class FavoritesChangeObserver extends ContentObserver {        public FavoritesChangeObserver() {            super(mHandler);        }        @Override        public void onChange(boolean selfChange) {            onFavoritesChanged();        }    }    /**     * GridView adapter to show the list of all installed applications.     */    private class ApplicationsAdapter extends ArrayAdapter<ApplicationInfo> {        private Rect mOldBounds = new Rect();        public ApplicationsAdapter(Context context, ArrayList<ApplicationInfo> apps) {            super(context, 0, apps);        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            final ApplicationInfo info = mApplications.get(position);            if (convertView == null) {                final LayoutInflater inflater = getLayoutInflater();                convertView = inflater.inflate(R.layout.application, parent, false);            }            //final ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);            Drawable icon = info.icon;            if (!info.filtered) {                final Resources resources = getContext().getResources();                int width = (int) resources.getDimension(android.R.dimen.app_icon_size);                int height = (int) resources.getDimension(android.R.dimen.app_icon_size);                final int iconWidth = icon.getIntrinsicWidth();                final int iconHeight = icon.getIntrinsicHeight();                if (icon instanceof PaintDrawable) {                    PaintDrawable painter = (PaintDrawable) icon;                    painter.setIntrinsicWidth(width);                    painter.setIntrinsicHeight(height);                }                if (width > 0 && height > 0 && (width < iconWidth || height < iconHeight)) {                    final float ratio = (float) iconWidth / iconHeight;                    if (iconWidth > iconHeight) {                        height = (int) (width / ratio);                    } else if (iconHeight > iconWidth) {                        width = (int) (height * ratio);                    }                    final Bitmap.Config c =                            icon.getOpacity() != PixelFormat.OPAQUE ?                                Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;                    final Bitmap thumb = Bitmap.createBitmap(width, height, c);                    final Canvas canvas = new Canvas(thumb);                    canvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, 0));                    // Copy the old bounds to restore them later                    // If we were to do oldBounds = icon.getBounds(),                    // the call to setBounds() that follows would                    // change the same instance and we would lose the                    // old bounds                    mOldBounds.set(icon.getBounds());                    icon.setBounds(0, 0, width, height);                    icon.draw(canvas);                    icon.setBounds(mOldBounds);                    icon = info.icon = new BitmapDrawable(thumb);                    info.filtered = true;                }            }            final TextView textView = (TextView) convertView.findViewById(R.id.label);            textView.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);            textView.setText(info.title);            return convertView;        }    }    /**     * Shows and hides the applications grid view.     */    private class ShowApplications implements View.OnClickListener {        public void onClick(View v) {            if (mGrid.getVisibility() != View.VISIBLE) {                showApplications(true);            } else {                hideApplications();            }        }    }    /**     * Hides the applications grid when the layout animation is over.     */    private class HideGrid implements Animation.AnimationListener {        public void onAnimationStart(Animation animation) {        }        public void onAnimationEnd(Animation animation) {            mBlockAnimation = false;        }        public void onAnimationRepeat(Animation animation) {        }    }    /**     * Shows the applications grid when the layout animation is over.     */    private class ShowGrid implements Animation.AnimationListener {        public void onAnimationStart(Animation animation) {        }        public void onAnimationEnd(Animation animation) {            mBlockAnimation = false;            // ViewDebug.stopHierarchyTracing();        }        public void onAnimationRepeat(Animation animation) {        }    }    /**     * Starts the selected activity/application in the grid view.     */    private class ApplicationLauncher implements AdapterView.OnItemClickListener {        public void onItemClick(AdapterView parent, View v, int position, long id) {            ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position);            startActivity(app.intent);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -