zoukankan      html  css  js  c++  java
  • Android Launcher研究(一)图文详解手把手教你在Windows环境下下载Android源码(Launcher为例)

    大家好,这篇文章我将教大家如何在Windows环境下下载Android源码,Android 自2007年11月5号发布以来,发展速度如此之快,和它走开源的路是分不开的。我们在开发中有什么不明白不清楚的,直接把Android 源码下下来研究就可以了,看源代码将会让你提升很快!

    在这之前大家先熟悉俩个代码版本管理工具SVN,以及Git。

    SVN(Windows环境下最常用的):

    svn(subversion)是近年来崛起的版本管理工具,是cvs的接班人。目前,绝大多数开源软件都使用svn作为代码版本管理软件。

    Git:

    Git 是用于 Linux 内核开发的版本控制工具。与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持,使源代码的发布和交流极其方便。 Git 的速度很快,这对于诸如 Linux kernel 这样的大项目来说自然很重要。 Git 最为出色的是它的合并跟踪(merge tracing)能力。

    而Google Android的项目是基于Git进行版本管理的,所以经常在Linux环境下开发的人,就不用我多说了,而大都数在Windows环境开发的人,就比较陌生了。那下面我就手把手教你如何在Windows环境下Git Android源码。

    第一步:Msysgit工具的下载(这个是Google为Windows环境下开发的Git客户端程序):

    http://code.google.com/p/msysgit/ 下载地址如图:

    第二步:安装Msysgit软件(一直默认到底),如下图:

    第三步:建立一个文件夹用来存放Git下来的Android 源码(我这里以G:/Android Src)为例,如下图:

    第四步:右击Android Src文件夹选择Git Bash出现如下图所示:

    第五步:查找我们要下载源代码的地址。Android的源代码是放在下面地址下:

    http://android.git.kernel.org/

    这里以Launcher为例,我们Ctrl + F查找:输入Launcher如下图所示:

    点击链接进入另一个页面:

    第六步在Git Bash端输入如下命令,命令格式(如上图图示)

    git clone git://android.git.kernel.org/platform/packages/apps/Launcher.git

    在Receiving Objects: 100%时候,我们在G:/Android Src/文件夹下多一个工程Launcher,这正是我们所要的,如下图所示:

    Ok通过以上的步骤我们就把Android Launcher的源代码拿到手了,下面就剩下你研究了!

    还没完。。。。。。。。。。。。。。

    大家好,我今天给大家简单讲一下Launcher里如何列出所有安装的应用的,我们点击Launcher的抽屉(SlidingDrawer)就会 出现所有安装的应用列表(当然Widget,Launcher本身除外).并且点击应用图标进入 相关应用。我这里就先简单的用一个GridView来显示应用。

    老样子我还会写一个简单的Demo让大家理解。大家跟着我的步骤来。

    第一步:新建一个Android工程命名为:Launcher.

    第二步:修改main.xml布局文件,代码如下(只有一个GridView这里):

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7.     <GridView  
    8.         android:id="@+id/allapps"  
    9.         android:layout_width="fill_parent"  
    10.         android:layout_height="wrap_content"  
    11.     />  
    12. </LinearLayout>  

    第三步:新建一个application_layout.xml布局文件,用来定义应用的图标和标题,代码如下:

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7.     <ImageView  
    8.         android:id="@+id/app_icon"  
    9.         android:layout_width="wrap_content"  
    10.         android:layout_height="wrap_content"  
    11.     />  
    12.     <TextView  
    13.         android:id="@+id/app_title"  
    14.         android:layout_width="wrap_content"  
    15.         android:layout_height="wrap_content"  
    16.     />  
    17. </LinearLayout>  

    第四步:也就是核心了,修改Launcher.java代码如下:

    1. package com.tutor.launcher;  
    2. import java.util.Collections;  
    3. import java.util.List;  
    4. import android.app.Activity;  
    5. import android.content.ComponentName;  
    6. import android.content.Context;  
    7. import android.content.Intent;  
    8. import android.content.pm.PackageManager;  
    9. import android.content.pm.ResolveInfo;  
    10. import android.os.Bundle;  
    11. import android.view.LayoutInflater;  
    12. import android.view.View;  
    13. import android.view.ViewGroup;  
    14. import android.widget.AdapterView;  
    15. import android.widget.BaseAdapter;  
    16. import android.widget.GridView;  
    17. import android.widget.ImageView;  
    18. import android.widget.TextView;  
    19. import android.widget.AdapterView.OnItemClickListener;  
    20. public class Launcher extends Activity implements OnItemClickListener{  
    21.      
    22.     private GridView mGridView;  
    23.     private Context mContext;  
    24.     private PackageManager mPackageManager;  
    25.     private List<ResolveInfo> mAllApps;  
    26.       
    27.     public void onCreate(Bundle savedInstanceState) {  
    28.         super.onCreate(savedInstanceState);  
    29.         setContentView(R.layout.main);  
    30.           
    31.         setupViews();  
    32.           
    33.     }  
    34.       
    35.     public void setupViews(){  
    36.         mContext = Launcher.this;  
    37.         mPackageManager = getPackageManager();  
    38.         mGridView = (GridView)findViewById(R.id.allapps);  
    39.         bindAllApps();  
    40.           
    41.         mGridView.setAdapter(new GridItemAdapter(mContext, mAllApps));  
    42.         mGridView.setNumColumns(4);  
    43.         mGridView.setOnItemClickListener(this);  
    44.     }  
    45.       
    46.     public void bindAllApps(){  
    47.         //这里是关键哦,我们平时写的应用总有一个activity申明成这两个属性  
    48.         //也就是应用的入口  
    49.         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
    50.         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
    51.         //符合上面条件的全部查出来,并且排序  
    52.         mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);  
    53.         Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));  
    54.     }  
    55.       
    56.       
    57.       
    58.     //gridview点击事件,点击进入相关应用  
    59.     @Override  
    60.     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
    61.         // TODO Auto-generated method stub  
    62.         ResolveInfo res = mAllApps.get(position);  
    63.         //该应用的包名和主Activity  
    64.         String pkg = res.activityInfo.packageName;  
    65.         String cls = res.activityInfo.name;  
    66.           
    67.         ComponentName componet = new ComponentName(pkg, cls);  
    68.           
    69.         Intent i = new Intent();  
    70.         i.setComponent(componet);  
    71.         startActivity(i);  
    72.     }  
    73.       
    74.     //不明白BaseAdapter的用法 我高手进阶里有  
    75.     private class GridItemAdapter extends BaseAdapter{  
    76.         private Context context;  
    77.         private List<ResolveInfo> resInfo;  
    78.           
    79.         //构造函数  
    80.         public GridItemAdapter(Context c,List<ResolveInfo> res){  
    81.             context = c;  
    82.             resInfo = res;  
    83.         }  
    84.         @Override  
    85.         public int getCount() {  
    86.             // TODO Auto-generated method stub  
    87.             return resInfo.size();  
    88.         }  
    89.         @Override  
    90.         public Object getItem(int position) {  
    91.             // TODO Auto-generated method stub  
    92.             return null;  
    93.         }  
    94.         @Override  
    95.         public long getItemId(int position) {  
    96.             // TODO Auto-generated method stub  
    97.             return 0;  
    98.         }  
    99.         @Override  
    100.         public View getView(int position, View convertView, ViewGroup parent) {  
    101.               
    102.             //不明白LayoutInflater的我android高手进阶里有  
    103.             convertView = LayoutInflater.from(context)  
    104.             .inflate(R.layout.application_layout, null);  
    105.               
    106.             ImageView app_icon = (ImageView)convertView.findViewById(R.id.app_icon);  
    107.             TextView app_tilte = (TextView)convertView.findViewById(R.id.app_title);  
    108.               
    109.             ResolveInfo res = resInfo.get(position);  
    110.             app_icon.setImageDrawable(res.loadIcon(mPackageManager));  
    111.             app_tilte.setText(res.loadLabel(mPackageManager).toString());  
    112.             return convertView;  
    113.         }  
    114.           
    115.     }  
    116. }  

    第五步:运行以上工程,得到的效果图如下:

    图1.列出所有安装的应用.

    图2.点击aQQ应用进入到相应的应用里。

    OK,今天就写到这里,大家有什么不明白的地方可 以留言。thx~

    Android Launcher研究(四)-----------桌面应用快捷方式的开发!

    大家好,今天我给大家分享的是Launcher桌面快捷图标的开发,我们都知道快捷图标有两部分组成,一部分是应用的图标,另一部分就是应用的名 称。其实Launcher中的快捷图标只是继承了TextView控件,重绘了一下,将背景弄成浅灰色(具体是什么颜色我也不知道)的椭圆背景,显示的文 字颜色则是白色。TextView有android:drawableTop;drawableBottom(上下左右我这里就不全写出来了)属性,用来 显示应用的图标。

    废话不多说了,直接上例子,大家一步一步来,多敲敲代码,成长快一点。

    第一步:新建一个Android工程,命名为ApplicationDemo.如下图:

    第二步:在values目录下新建colors.xml文件,定义一些要用的颜色,代码如下:

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <color name="white">#FFFFFF</color>  
    4.     <color name="black">#000000</color>       
    5.     <color name="bubble_dark_background">#B2191919</color>  
    6. </resources>  

    第三步:也就是重点了,新建一个BubbleTextView类,继承TextView,代码如下:

    1. package com.tutor.application;  
    2. import android.content.Context;  
    3. import android.graphics.Canvas;  
    4. import android.graphics.Paint;  
    5. import android.graphics.RectF;  
    6. import android.text.Layout;  
    7. import android.util.AttributeSet;  
    8. import android.widget.TextView;  
    9. public class BubbleTextView extends TextView {  
    10.     private static final int CORNER_RADIUS = 8;  
    11.     private static final int PADDING_H = 5;  
    12.     private static final int PADDING_V = 1;  
    13.     private final RectF mRect = new RectF();  
    14.     private Paint mPaint;  
    15.     public BubbleTextView(Context context) {  
    16.         super(context);  
    17.         init();  
    18.     }  
    19.     public BubbleTextView(Context context, AttributeSet attrs) {  
    20.         super(context, attrs);  
    21.         init();  
    22.     }  
    23.     public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {  
    24.         super(context, attrs, defStyle);  
    25.         init();  
    26.     }  
    27.     private void init() {  
    28.         setFocusable(true);  
    29.         // We need extra padding below to prevent the bubble being cut.  
    30.         setPadding(PADDING_H, 0, PADDING_H, PADDING_V);  
    31.         mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
    32.         mPaint.setColor(getContext().getResources()  
    33.                 .getColor(R.color.bubble_dark_background));  
    34.     }  
    35.     @Override  
    36.     protected void drawableStateChanged() {  
    37.         invalidate();  
    38.         super.drawableStateChanged();  
    39.     }  
    40.     @Override  
    41.     public void draw(Canvas canvas) {  
    42.         final Layout layout = getLayout();  
    43.         final RectF rect = mRect;  
    44.         final int left = getCompoundPaddingLeft();  
    45.         final int top = getExtendedPaddingTop();  
    46.         rect.set(left + layout.getLineLeft(0) - PADDING_H,  
    47.                  top + layout.getLineTop(0) - PADDING_V,  
    48.                  Math.min(left + layout.getLineRight(0) + PADDING_H,  
    49.                           getScrollX() + getRight() - getLeft()),  
    50.                  top + layout.getLineBottom(0) + PADDING_V);  
    51.         canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, mPaint);  
    52.         super.draw(canvas);  
    53.     }  
    54. }  

    第四步:修改main.xml布局文件,代码如下:

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7.     <TextView    
    8.         android:layout_width="wrap_content"   
    9.         android:layout_height="wrap_content"   
    10.         android:drawableTop="@drawable/icon"  
    11.         android:text="ApplicationDemo"  
    12.         android:textColor="@color/black"  
    13.         />  
    14.     <com.tutor.application.BubbleTextView  
    15.         android:layout_width="wrap_content"   
    16.         android:layout_height="wrap_content"  
    17.         android:drawableTop="@drawable/icon"   
    18.         android:textColor="@color/white"  
    19.         android:text="ApplicationDemo"  
    20.     />  
    21. </LinearLayout>  

    第五步:修改AndroidManifest.xml文件,注意这里我们在Activity里增加了一个透明的样式,Launcher其实就是透明的Activity。

    代码如下(第8行代码):

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.       package="com.tutor.application"  
    4.       android:versionCode="1"  
    5.       android:versionName="1.0">  
    6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
    7.         <activity android:name=".ApplicationDemo"  
    8.                   android:theme="@android:style/Theme.Wallpaper.NoTitleBar"  
    9.                   android:label="@string/app_name">  
    10.             <intent-filter>  
    11.                 <action android:name="android.intent.action.MAIN" />  
    12.                 <category android:name="android.intent.category.LAUNCHER" />  
    13.             </intent-filter>  
    14.         </activity>  
    15.     </application>  
    16.     <uses-sdk android:minSdkVersion="7" />  
    17. </manifest>   

    第六步:运行上述工程,查看效果如下:

    将android:drawableLeft修改为android:drawableTop,效果如下:

    Ok~大功告成,收工睡觉!!!

  • 相关阅读:
    ASP.NET的底层体系1
    MVC路由解析---IgnoreRoute
    HTTP协议详解(三)
    HTTP协议详解(二)
    HTTP协议详解(一)
    优化MySQL服务器
    知识管理系列---3.程序实现
    Linux上PHP加入环境变量
    php-fpm 服务
    centos 服务器编译安装apache+php
  • 原文地址:https://www.cnblogs.com/carbs/p/2571197.html
Copyright © 2011-2022 走看看