Android BGABadgeView:BGABadgeImageView以及BGABadgeRelativeLayout(4)



Android BGABadgeView:BGABadgeImageView以及BGABadgeRelativeLayout(4)

在附录文章5,6,7的基础上,写一个小例子说明BGABadgeImageView以及BGABadgeRelativeLayout。现在比如常见的新浪微博的账号头像下,如果有些大v账号,会额外显示一个vip徽标。这个可以用BGABadgeImageView实现,也可以是一个其他的普通BadgeView。
本例中的头像图是我的博客头像,vip图是新浪微博常见的vip小logo。
写一个布局:

<?xml version="1.0" encoding="utf-8"?>
<cn.bingoogolapple.badgeview.BGABadgeRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/badgeRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    tools:context="zhangphil.demo.MainActivity">

    <cn.bingoogolapple.badgeview.BGABadgeImageView
        android:id="@+id/badgeImageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="5dp"
        android:src="@drawable/zhangphil"
        app:badge_horizontalMargin="5dp"
        app:badge_verticalMargin="5dp" />

    <cn.bingoogolapple.badgeview.BGABadgeImageView
        android:id="@+id/badgeImageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="5dp"
        android:src="@drawable/zhangphil" />

    <cn.bingoogolapple.badgeview.BGABadgeImageView
        android:id="@+id/badgeImageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:padding="5dp"
        android:src="@drawable/zhangphil" />

</cn.bingoogolapple.badgeview.BGABadgeRelativeLayout>



Java代码:

package zhangphil.demo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import cn.bingoogolapple.badgeview.BGABadgeImageView;
import cn.bingoogolapple.badgeview.BGABadgeRelativeLayout;
import cn.bingoogolapple.badgeview.BGABadgeViewHelper;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //BGABadgeRelativeLayout显示一个右下角的红色提示小圆球
        //半径大小为30dp
        BGABadgeRelativeLayout mBGABadgeRelativeLayout = (BGABadgeRelativeLayout) findViewById(R.id.badgeRelativeLayout);
        mBGABadgeRelativeLayout.showCirclePointBadge();
        mBGABadgeRelativeLayout.getBadgeViewHelper().setBadgePaddingDp(30);
        mBGABadgeRelativeLayout.getBadgeViewHelper().setBadgeGravity(BGABadgeViewHelper.BadgeGravity.RightBottom);
        mBGABadgeRelativeLayout.getBadgeViewHelper().setDragable(true);

        //右上角一个红色小圆球
        BGABadgeImageView badgeImageView1 = (BGABadgeImageView) findViewById(R.id.badgeImageView1);
        badgeImageView1.showCirclePointBadge();
        badgeImageView1.getBadgeViewHelper().setBadgeGravity(BGABadgeViewHelper.BadgeGravity.RightTop);
        badgeImageView1.getBadgeViewHelper().setBadgePaddingDp(6);

        //右下角一个VIP徽标的图案
        BGABadgeImageView badgeImageView2 = (BGABadgeImageView) findViewById(R.id.badgeImageView2);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.avatar_vip);
        Bitmap avatorBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil);
        RoundedBitmapDrawable circleDrawable = RoundedBitmapDrawableFactory.create(getResources(), avatorBitmap);
        circleDrawable.getPaint().setAntiAlias(true);
        circleDrawable.setCornerRadius(Math.max(avatorBitmap.getWidth(), avatorBitmap.getHeight()));
        badgeImageView2.setImageDrawable(circleDrawable);
        badgeImageView2.showDrawableBadge(bitmap);
        badgeImageView2.getBadgeViewHelper().setBadgeGravity(BGABadgeViewHelper.BadgeGravity.RightBottom);


        //默认的右上角小圆球,但是颜色修改为蓝色
        BGABadgeImageView badgeImageView3 = (BGABadgeImageView) findViewById(R.id.badgeImageView3);
        badgeImageView3.showCirclePointBadge();
        badgeImageView3.getBadgeViewHelper().setBadgeBgColorInt(Color.BLUE);


        /**
         badge.showTextBadge("9");
         badge.getBadgeViewHelper().setBadgeTextSizeSp(15);
         badge.getBadgeViewHelper().setBadgeTextColorInt(Color.WHITE);
         badge.getBadgeViewHelper().setBadgeBgColorInt(Color.RED);
         badge.getBadgeViewHelper().setDragable(true);
         badge.getBadgeViewHelper().setBadgePaddingDp(6);
         badge.getBadgeViewHelper().setBadgeBorderWidthDp(2);
         badge.getBadgeViewHelper().setBadgeBorderColorInt(Color.WHITE);
         badge.getBadgeViewHelper().setBadgeGravity(BGABadgeViewHelper.BadgeGravity.RightTop);
         */
    }
}

关于圆角图片的处理见附录文章4。



代码运行结果:



附录文章:
1,《仿微信、短信、QQ等消息数目右上角红色小圆球气泡显示(基于Android XML布局文件实现)》链接地址:http://blog.csdn.net/zhangphil/article/details/43702953 
2,《仿短信条目右上角的红色小圆球提示气泡》链接地址:http://blog.csdn.net/zhangphil/article/details/43667727
3,《Android开源BezierView:仿QQ未读消息99+条的红色气泡》链接地址:http://blog.csdn.net/zhangphil/article/details/49746709
4,《Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案》链接地址:http://blog.csdn.net/zhangphil/article/details/51829650
5,《Android BGABadgeView:新消息/未接来电/未读消息/新通知圆球红点提示(1)》链接地址:http://blog.csdn.net/zhangphil/article/details/51822514
6,《Android BGABadgeView:显示提示数字(2)》链接地址:http://blog.csdn.net/zhangphil/article/details/51828808
7,《Android BGABadgeView:BGABadgeLinearLayout以整体线性布局作为BadgeView(3)》链接地址:http://blog.csdn.net/zhangphil/article/details/51828968

原文地址:https://www.cnblogs.com/hehehaha/p/6147286.html