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

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

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

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

view plain copy to clipboard print ?
  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <resources>
  3. <colorname="white" >#FFFFFF</color>
  4. <colorname="black" ># 000000 </color>
  5. <colorname="bubble_dark_background" >#B2191919</color>
  6. </resources>

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

view plain copy to clipboard print ?
  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 RectFmRect= new RectF();
  14. private PaintmPaint;
  15. public BubbleTextView(Contextcontext){
  16. super (context);
  17. init();
  18. }
  19. public BubbleTextView(Contextcontext,AttributeSetattrs){
  20. super (context,attrs);
  21. init();
  22. }
  23. public BubbleTextView(Contextcontext,AttributeSetattrs, int defStyle){
  24. super (context,attrs,defStyle);
  25. init();
  26. }
  27. private void init(){
  28. setFocusable(true );
  29. //Weneedextrapaddingbelowtopreventthebubblebeingcut.
  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(Canvascanvas){
  42. final Layoutlayout=getLayout();
  43. final RectFrect=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布局文件,代码如下:

view plain copy to clipboard print ?
  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <LinearLayoutxmlns: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行代码):

view plain copy to clipboard print ?
  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package = "com.tutor.application"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <applicationandroid:icon="@drawable/icon" android:label= "@string/app_name" >
  7. <activityandroid:name=".ApplicationDemo"
  8. android:theme="@android:style/Theme.Wallpaper.NoTitleBar"
  9. android:label="@string/app_name" >
  10. <intent-filter>
  11. <actionandroid:name="android.intent.action.MAIN" />
  12. <categoryandroid:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. </application>
  16. <uses-sdkandroid:minSdkVersion="7" />
  17. </manifest>

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

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

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

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右
  3. 【Android】说做就做:带图标的list item
  4. 第一次使用Android(安卓)Studio时你应该知道的一切配置
  5. 把SVN项目存到码云,然后下载到Android(安卓)Studio进行代码改写
  6. 配置Windows下Android的NDK环境:安装,更新cygwin...
  7. 给Android的应用换个图标(桌面快捷图标)sShortcut
  8. Android(安卓)数字签名及命令行启动执行APK
  9. Android(安卓)应用广告过滤几种方式

随机推荐

  1. Android防止内存溢出浅析
  2. Android与IOS异同点对比(1)------ 显示
  3. 在Android中创建和使用数据库
  4. Android(安卓)release版本apk添加数字签
  5. Android(安卓)JNI 机制
  6. Android时区问题
  7. Android调用系统自带的文件管理器进行文
  8. 移动互联网的新宠:Android之缤纷世界
  9. Android仿腾讯视频悬浮窗的实现
  10. 最牛逼android上的图表库MpChart(二) 折线