Cocos2D-Android-1之源碼詳解:1.Cocos2D

來源:互聯網
上載者:User

標籤:cocos2d

/*

 * Copyright (C) 2007 The Android Open Source Project

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */


package org.cocos2d;


import java.text.Collator;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


import android.app.ListActivity;

import android.content.Intent;

import android.content.pm.PackageManager;

import android.content.pm.ResolveInfo;

import android.os.Bundle;

import android.view.View;

import android.widget.ListView;

import android.widget.SimpleAdapter;


public class Cocos2D extends ListActivity {//這個類用了ListActivity 


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState); //繼承 ListActivity 的 onCreate方法     //以下是設定list裡的適配器

        setListAdapter(new SimpleAdapter(this,//新的簡單適配器,運用了自己的上下文

                (List<Map<String, ?>>)getData("org.cocos2d.tests"),//getData是個方法,getData(String)往下看有方法的聲明

                android.R.layout.simple_list_item_1, new String[]{"title"},

                new int[]{android.R.id.text1}));

        getListView().setTextFilterEnabled(true);//啟用過濾視窗

    }


    protected List<?> getData(String prefix) {//即上述方法

        List<Map<String,?>> myData = new ArrayList<Map<String,?>>();//一個用來盛放map的list,map立盛放String



        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);//意圖指定跳到系統案頭

        mainIntent.addCategory(Intent.CATEGORY_TEST);//供測試使用


        PackageManager pm = getPackageManager();//包控制器,用來獲得現在有的進程

        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);//通過Intent尋找相關的Activity,更準確

        if (null == list)//沒找到那個測試意圖

            return myData;//就結束


        /* 以下是個沒有被使用的方法用來分割字串,用/

        String[] prefixPath;

        if (prefix.equals("")) {

            prefixPath = null;

        } else {

            prefixPath = prefix.split("/");

        }*/

        int len = list.size();//得到list的長度


        // Map<String, Boolean> entries = new HashMap<String, Boolean>();//一個沒有被使用的map,用了雜湊類型的map



        for (int i = 0; i < len; i++) {

            ResolveInfo info = list.get(i);//用來迴圈剛才得到的那個list

            String activityName = info.activityInfo.name;//得到activity的名字

            if (prefix.length() == 0 || activityName.startsWith(prefix)) {//如果要尋找的那個activity的包名長度為0或者與我們要找的那個名字是以這個開頭的

                String[] labelPath = activityName.split("\\.");//把包名用.分開

                String nextLabel = labelPath[labelPath.length - 1];//得到最後一個.後面的名字,即類名

                addItem(myData, nextLabel, activityIntent(//現在把他添加到mydata的集合,這個方法在後面會出現,就是分別把各種資訊用map區分了

                        info.activityInfo.applicationInfo.packageName,

                        info.activityInfo.name));

            }

        }

        Collections.sort(myData, sDisplayNameComparator);//Collections是個很有用的集合,其中有個sort是用來排序的,參數中sDisplayNameComparator是個比較方法

        return myData;

    }

//下面這個定義,乍一看是方法,其實是個欄位,定義一個比較方法的引用

    private final static Comparator<Map<String, ?>> sDisplayNameComparator = new Comparator<Map<String, ?>>() {//對那個引用的實現

        private final Collator collator = Collator.getInstance();//新疆一個集合


        public int compare(Map<String,?> map1, Map<String,?> map2) {//兩個的比較規則

            return collator.compare(map1.get("title"), map2.get("title"));//就是比較他們字元的那種方法,就是從左至右一個一個字母,比ascll碼

        }

    };


    protected Intent activityIntent(String pkg, String componentName) {

        Intent result = new Intent();

        result.setClassName(pkg, componentName);//設定意圖從哪個包到哪個類

        return result;

    }


    protected Intent browserIntent(String path) {

        Intent result = new Intent();

        result.setClass(this, Cocos2D.class);//設定從哪個class到哪

        result.putExtra("org.cocos2d.tests.Path", path);//加一個額外的變數

        return result;

    }


    protected void addItem(List<Map<String,?>> data, String name, Intent intent) {

        Map<String, Object> temp = new HashMap<String, Object>();

        temp.put("title", name);

        temp.put("intent", intent);

        data.add(temp);//對data添加項目

    }


    @Override

    protected void onListItemClick(ListView l, View v, int position, long id) {

        Map<String,?> map = (Map<String,?>) l.getItemAtPosition(position);//得到點擊了哪個包

        Intent intent = (Intent) map.get("intent");//找到那個類全名

        startActivity(intent);//開啟那個類

    }

}


Cocos2D-Android-1之源碼詳解:1.Cocos2D

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.