Android(安卓)Launcher研究(四)-----------桌面应用快捷方式的开发!
16lz
2021-01-25
大家好,今天我给大家分享的是Launcher桌面快捷图标的开发,我们都知道快捷图标有两部分组成,一部分是应用的图标,另一部分就是应用的名 称。其实Launcher中的快捷图标只是继承了TextView控件,重绘了一下,将背景弄成浅灰色(具体是什么颜色我也不知道)的椭圆背景,显示的文 字颜色则是白色。TextView有android:drawableTop;drawableBottom(上下左右我这里就不全写出来了)属性,用来 显示应用的图标。
废话不多说了,直接上例子,大家一步一步来,多敲敲代码,成长快一点。
第一步:新建一个Android工程,命名为ApplicationDemo.如下图:
第二步:在values目录下新建colors.xml文件,定义一些要用的颜色,代码如下:
view plain copy to clipboard print ?- <?xmlversion= "1.0" encoding= "utf-8" ?>
- <resources>
- <colorname="white" >#FFFFFF</color>
- <colorname="black" ># 000000 </color>
- <colorname="bubble_dark_background" >#B2191919</color>
- </resources>
第三步:也就是重点了,新建一个BubbleTextView类,继承TextView,代码如下:
view plain copy to clipboard print ?- package com.tutor.application;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.RectF;
- import android.text.Layout;
- import android.util.AttributeSet;
- import android.widget.TextView;
- public class BubbleTextView extends TextView{
- private static final int CORNER_RADIUS= 8 ;
- private static final int PADDING_H= 5 ;
- private static final int PADDING_V= 1 ;
- private final RectFmRect= new RectF();
- private PaintmPaint;
- public BubbleTextView(Contextcontext){
- super (context);
- init();
- }
- public BubbleTextView(Contextcontext,AttributeSetattrs){
- super (context,attrs);
- init();
- }
- public BubbleTextView(Contextcontext,AttributeSetattrs, int defStyle){
- super (context,attrs,defStyle);
- init();
- }
- private void init(){
- setFocusable(true );
- //Weneedextrapaddingbelowtopreventthebubblebeingcut.
- setPadding(PADDING_H,0 ,PADDING_H,PADDING_V);
- mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
- mPaint.setColor(getContext().getResources()
- .getColor(R.color.bubble_dark_background));
- }
- @Override
- protected void drawableStateChanged(){
- invalidate();
- super .drawableStateChanged();
- }
- @Override
- public void draw(Canvascanvas){
- final Layoutlayout=getLayout();
- final RectFrect=mRect;
- final int left=getCompoundPaddingLeft();
- final int top=getExtendedPaddingTop();
- rect.set(left+layout.getLineLeft(0 )-PADDING_H,
- top+layout.getLineTop(0 )-PADDING_V,
- Math.min(left+layout.getLineRight(0 )+PADDING_H,
- getScrollX()+getRight()-getLeft()),
- top+layout.getLineBottom(0 )+PADDING_V);
- canvas.drawRoundRect(rect,CORNER_RADIUS,CORNER_RADIUS,mPaint);
- super .draw(canvas);
- }
- }
第四步:修改main.xml布局文件,代码如下:
view plain copy to clipboard print ?- <?xmlversion= "1.0" encoding= "utf-8" ?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:drawableTop="@drawable/icon"
- android:text="ApplicationDemo"
- android:textColor="@color/black"
- />
- <com.tutor.application.BubbleTextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:drawableTop="@drawable/icon"
- android:textColor="@color/white"
- android:text="ApplicationDemo"
- />
- </LinearLayout>
第五步:修改AndroidManifest.xml文件,注意这里我们在Activity里增加了一个透明的样式,Launcher其实就是透明的Activity。
代码如下(第8行代码):
view plain copy to clipboard print ?- <?xmlversion= "1.0" encoding= "utf-8" ?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package = "com.tutor.application"
- android:versionCode="1"
- android:versionName="1.0" >
- <applicationandroid:icon="@drawable/icon" android:label= "@string/app_name" >
- <activityandroid:name=".ApplicationDemo"
- android:theme="@android:style/Theme.Wallpaper.NoTitleBar"
- android:label="@string/app_name" >
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN" />
- <categoryandroid:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdkandroid:minSdkVersion="7" />
- </manifest>
第六步:运行上述工程,查看效果如下:
将android:drawableLeft修改为android:drawableTop,效果如下:
Ok~大功告成,收工睡觉!!!
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右
- 【Android】说做就做:带图标的list item
- 第一次使用Android(安卓)Studio时你应该知道的一切配置
- 把SVN项目存到码云,然后下载到Android(安卓)Studio进行代码改写
- 配置Windows下Android的NDK环境:安装,更新cygwin...
- 给Android的应用换个图标(桌面快捷图标)sShortcut
- Android(安卓)数字签名及命令行启动执行APK
- Android(安卓)应用广告过滤几种方式