您的位置:首页 > 移动开发 > Android开发

安卓数字消息提醒、角标-测试BadgeView效果

2017-03-30 11:42 459 查看

效果



代码

1.BadgeViewTwo.java

package com.fe.statuslayout;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.jauker.widget.BadgeView;

/**
* Created by Administrator on 2017/3/29 0029.
* 测试BadgeView效果
* 方式1:jar包使用,  badgeview.jar
* 方式2:复制代码使用 BadgeView.java
*/

public class BadgeViewTwo extends Activity implements View.OnClickListener {
private Button button9;
private Button button10;
private BadgeView badgeViewTwo;
private ListView listview;
private Button button11;
private MyBaseAdapter listAdapter;
private Button button12;
private Button button13;
private Button button14;
private BadgeView badgeView;
private Button button15;
private ImageButton imagebutton16;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.badgeviewtwo);
initView();
badgeViewTwo = new BadgeView(this);
badgeViewTwo.setBadgeCount(1);
badgeViewTwo.setTargetView(button10);

badgeView = new BadgeView(this);
badgeView.setBadgeCount(22);
badgeView.setBadgeGravity(Gravity.RIGHT | Gravity.TOP);
badgeView.setTargetView(button9);
}

private void initView() {
button9 = (Button) findViewById(R.id.button9);

button9.setOnClickListener(this);
button10 = (Button) findViewById(R.id.button10);
button10.setOnClickListener(this);
listview = (ListView) findViewById(R.id.listview);
//listview.setOnClickListener(this);
button11 = (Button) findViewById(R.id.button11);
button11.setOnClickListener(this);
button12 = (Button) findViewById(R.id.button12);
button12.setOnClickListener(this);
button13 = (Button) findViewById(R.id.button13);
button13.setOnClickListener(this);
button14 = (Button) findViewById(R.id.button14);
button14.setOnClickListener(this);
button15 = (Button) findViewById(R.id.button15);
button15.setOnClickListener(this);
imagebutton16 = (ImageButton) findViewById(R.id.imagebutton16);
imagebutton16.setOnClickListener(this);
}

boolean flag;

@Override
public void onClick(View v) {
switch (v.getId()) {
//button需要设置android:background="@null",防止徽章角标被遮挡
case R.id.button9:
//模拟点赞和取消点赞
if (flag) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//减少点赞
badgeView.decrementBadgeCount(1);

}
}, 1000);
} else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//增加点赞
badgeView.incrementBadgeCount(1);

}
}, 1000);
}
flag = !flag;

break;
case R.id.button10:
//思路:判断上一次状态,如果是隐藏,则显示
badgeViewTwo.setVisibility(badgeViewTwo.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
break;
case R.id.button11:
//展示listview列表
listAdapter = new MyBaseAdapter();
listview.setAdapter(listAdapter);
break;
case R.id.button12:
final BadgeView badgeView2 = new BadgeView(this);
//badgeView2.setText("文字");
badgeView2.setText("文字");
//设置字体
badgeView2.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC));
//设置阴影
badgeView2.setShadowLayer(5, -1, -1, Color.GREEN);
badgeView2.setTargetView(button12);
//2秒后更换字体
new Handler().postDelayed(new Runnable() {
@Override
public void run() {

badgeView2.setText("new", TextView.BufferType.EDITABLE);

}
}, 2000);
break;

case R.id.button13:
BadgeView badgeView3 = new BadgeView(this);

//badgeView3.setText("0");
//设置0无效果,不会展示。。。
//通过" "+"0"代替
badgeView3.setText(" " + "0");
//badgeView3.setBackground(12,Color.BLUE);
//设置具体的颜色
badgeView3.setBackground(12, Color.parseColor("#9b2eef"));
badgeView3.setTargetView(button13);
break;
case R.id.button14:
//修改背景图片
final BadgeView badgeView4 = new BadgeView(this);
badgeView4.setTargetView(button14);
badgeView4.setText("1");

badgeView4.setBackgroundResource(R.drawable.badge_orange);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
badgeView4.setBackgroundResource(R.drawable.badge_blue);
}
}, 2000);

break;
case R.id.button15:
//显示没有文字的徽章
BadgeView badgeView5 = new BadgeView(this);
badgeView5.setTargetView(button15);
badgeView5.setText("  ");
break;
case R.id.imagebutton16:
//适配@style/AppTheme:用ImageButton代替Button
BadgeView badgeView6 = new BadgeView(this);
badgeView6.setText("99+");
badgeView6.setTargetView(imagebutton16);
break;
}
}

class MyBaseAdapter extends BaseAdapter {

@Override
public int getCount() {
return 15;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (null == convertView) {
convertView = View.inflate(parent.getContext(), R.layout.list_row_badge, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(android.R.id.text1);
//                viewHolder.text.setText(position);
//报错
viewHolder.badge = new BadgeView(parent.getContext());
viewHolder.badge.setTargetView(viewHolder.text);
viewHolder.badge.setBadgeGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
viewHolder.badge.setBadgeMargin(8);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText("111");
viewHolder.badge.setBadgeCount(position);

return convertView;
}
}

static class ViewHolder {
TextView text;
//需要在这里定义
BadgeView badge;
}
}

2.BadgeView.java

/*
* BadgeView.java
* BadgeView
*
* Copyright (c) 2012 Stefan Jauker.
* https://github.com/kodex83/BadgeView *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.badgeviewtwo;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TabWidget;
import android.widget.TextView;

public class BadgeView extends TextView {

private boolean mHideOnNull = true;

public BadgeView(Context context) {
this(context, null);
}

public BadgeView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}

public BadgeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

init();
}

private void init() {
if (!(getLayoutParams() instanceof LayoutParams)) {
LayoutParams layoutParams =
new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.RIGHT | Gravity.TOP);
setLayoutParams(layoutParams);
}

// set default font
setTextColor(Color.WHITE);
setTypeface(Typeface.DEFAULT_BOLD);
setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
setPadding(dip2Px(5), dip2Px(1), dip2Px(5), dip2Px(1));

// set default background
setBackground(9, Color.parseColor("#d3321b"));

setGravity(Gravity.CENTER);

// default values
setHideOnNull(true);
setBadgeCount(0);
}

public void setBackground(int dipRadius, int badgeColor) {
int radius = dip2Px(dipRadius);
float[] radiusArray = new float[] { radius, radius, radius, radius, radius, radius, radius, radius };

RoundRectShape roundRect = new RoundRectShape(radiusArray, null, null);
ShapeDrawable bgDrawable = new ShapeDrawable(roundRect);
bgDrawable.getPaint().setColor(badgeColor);
setBackground(bgDrawable);
}

/**
* @return Returns true if view is hidden on badge value 0 or null;
*/
public boolean isHideOnNull() {
return mHideOnNull;
}

/**
* @param hideOnNull the hideOnNull to set
*/
public void setHideOnNull(boolean hideOnNull) {
mHideOnNull = hideOnNull;
setText(getText());
}

/*
* (non-Javadoc)
*
* @see android.widget.TextView#setText(java.lang.CharSequence, android.widget.TextView.BufferType)
*/
@Override
public void setText(CharSequence text, BufferType type) {
if (isHideOnNull() && (text == null || text.toString().equalsIgnoreCase("0"))) {
setVisibility(View.GONE);
} else {
setVisibility(View.VISIBLE);
}
super.setText(text, type);
}

public void setBadgeCount(int count) {
setText(String.valueOf(count));
}

public Integer getBadgeCount() {
if (getText() == null) {
return null;
}

String text = getText().toString();
try {
return Integer.parseInt(text);
} catch (NumberFormatException e) {
return null;
}
}

public void setBadgeGravity(int gravity) {
LayoutParams params = (LayoutParams) getLayoutParams();
params.gravity = gravity;
setLayoutParams(params);
}

public int getBadgeGravity() {
LayoutParams params = (LayoutParams) getLayoutParams();
return params.gravity;
}

public void setBadgeMargin(int dipMargin) {
setBadgeMargin(dipMargin, dipMargin, dipMargin, dipMargin);
}

public void setBadgeMargin(int leftDipMargin, int topDipMargin, int rightDipMargin, int bottomDipMargin) {
LayoutParams params = (LayoutParams) getLayoutParams();
params.leftMargin = dip2Px(leftDipMargin);
params.topMargin = dip2Px(topDipMargin);
params.rightMargin = dip2Px(rightDipMargin);
params.bottomMargin = dip2Px(bottomDipMargin);
setLayoutParams(params);
}

public int[] getBadgeMargin() {
LayoutParams params = (LayoutParams) getLayoutParams();
return new int[] { params.leftMargin, params.topMargin, params.rightMargin, params.bottomMargin };
}

public void incrementBadgeCount(int increment) {
Integer count = getBadgeCount();
if (count == null) {
setBadgeCount(increment);
} else {
setBadgeCount(increment + count);
}
}

public void decrementBadgeCount(int decrement) {
incrementBadgeCount(-decrement);
}

/*
* Attach the BadgeView to the TabWidget
*
* @param target the TabWidget to attach the BadgeView
*
* @param tabIndex index of the tab
*/
public void setTargetView(TabWidget target, int tabIndex) {
View tabView = target.getChildTabViewAt(tabIndex);
setTargetView(tabView);
}

/*
* Attach the BadgeView to the target view
*
* @param target the view to attach the BadgeView
*/
public void setTargetView(View target) {
if (getParent() != null) {
((ViewGroup) getParent()).removeView(this);
}

if (target == null) {
return;
}

if (target.getParent() instanceof FrameLayout) {
((FrameLayout) target.getParent()).addView(this);

} else if (target.getParent() instanceof ViewGroup) {
// use a new Framelayout container for adding badge
ViewGroup parentContainer = (ViewGroup) target.getParent();
int groupIndex = parentContainer.indexOfChild(target);
parentContainer.removeView(target);

FrameLayout badgeContainer = new FrameLayout(getContext());
ViewGroup.LayoutParams parentLayoutParams = target.getLayoutParams();

badgeContainer.setLayoutParams(parentLayoutParams);
target.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

parentContainer.addView(badgeContainer, groupIndex, parentLayoutParams);
badgeContainer.addView(target);

badgeContainer.addView(this);
} else if (target.getParent() == null) {
Log.e(getClass().getSimpleName(), "ParentView is needed");
}

}

/*
* converts dip to px
*/
private int dip2Px(float dip) {
return (int) (dip * getContext().getResources().getDisplayMetrics().density + 0.5f);
}
}


布局xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:background="@null"
android:id="@+id/button9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示标记,增加标记,减少标记"/>
<Button
android:background="@null"
android:id="@+id/button10"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="隐藏/显示"/>
<Button
android:background="@null"
android:id="@+id/button11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="展示listview列表"/>
<ListView android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="150dp">
</ListView>
<Button
android:background="@null"
android:id="@+id/button12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="展示文字标记"/>
<Button
android:background="@null"
android:id="@+id/button13"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="修复背景颜色"/>
<Button
android:background="@null"
android:id="@+id/button14"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="修改背景图片"/>
<Button
android:background="@null"
android:id="@+id/button15"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="显示没有文字的徽章"/>
<ImageButton
android:id="@+id/imagebutton16"
android:background="@drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>


。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 安卓