像QQ把聯系人頭像添加到桌面并顯示消息個數的效果:bubbles-for-android

jopen 9年前發布 | 16K 次閱讀 Android開發 移動開發 bubbles-for-android

Bubbles for Android是一個Android庫,提供類似QQ那樣可以把聯系人頭像添加到桌面并顯示消息個數的效果。

Logo

</div>

使用說明:

配置項目的依賴

在build.gradle文件中添加依賴。

dependencies {
    ...
    compile 'com.txusballesteros:bubbles:1.1'
}

配置AndroidManifest

在AndroidManifest.xml 文件中添加下面的代碼

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.txusballesteros.bubbles.app" >

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    <application ...>

        ...

        <service android:name="com.txusballesteros.bubbles.BubblesService"
            android:enabled="true"
            android:exported="false" />

    </application>

</manifest>

添加第一個Bubble

構建Bubble layout,記住,第一個view必須是BubbleLayout

<com.txusballesteros.bubbles.BubbleLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/avatar"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center"
        android:background="@drawable/profile_decorator"
        android:src="@drawable/profile"
        android:scaleType="centerCrop"/>

</com.txusballesteros.bubbles.BubbleLayout>

創建BubblesManager實例

private BubblesManager bubblesManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
     bubblesManager = new BubblesManager.Builder(this)
                                        .build();
     bubblesManager.initialize();
    ...
}

@Override
protected void onDestroy() {
    bubblesManager.recycle();
    ...
}

Bubble依附到窗口。

BubbleLayout bubbleView = (BubbleLayout)LayoutInflater
                                    .from(MainActivity.this).inflate(R.layout.bubble_layout, null);
bubblesManager.addBubble(bubbleView, 60, 20);

配置bubbles垃圾桶

如果你想用一個垃圾桶來去除屏幕上的bubbles,你可以配置它的布局。

定義垃圾桶的布局:

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:src="@mipmap/bubble_trash_background"
    android:layout_gravity="bottom|center_horizontal" />

利用BubblesManager builder來配置垃圾桶布局

private BubblesManager bubblesManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
     bubblesManager = new BubblesManager.Builder(this)
                                        .setTrashLayout(R.layout.bubble_trash_layout)
                                        .build();
     bubblesManager.initialize();
    ...
}

項目主頁:http://www.baiduhome.net/lib/view/home/1436406125677

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!