现在的位置: 首页 > 综合 > 正文

Android Launcher桌面应用快捷方式的开发

2013年02月26日 ⁄ 综合 ⁄ 共 1966字 ⁄ 字号 评论关闭

 

快捷图标有两部分组成,一部分是应用的图标,另一部分就是应用的名称。其实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() {    

抱歉!评论已关闭.