原文链接:http://www.jcodecraeer.com/a/opensource/2015/0910/3436.html

项目地址: https://github.com/elevenetc/BadgeView
开发环境: Android Studio

介绍:

动画显示文字与bitmap的Badge view 。

运行效果:

使用说明:

添加到布局:

1 2 3 4 <su.levenetc.android.badgeview.BadgeView android:layout_width="wrap_content" android:layout_height="wrap_content" app:badgeText="Hello!"/>

或者直接使用java代码:

1 2 BadgeViewbadgeView=newBadgeView(this); badgeView.setValue(R.string.hello);

XML 属性

1 2 3 4 5 6 7 <attrname="badgeText"format="string"/> <attrname="badgeBitmap"format="reference"/> <attrname="badgeBackgroundColor"format="color"/> <attrname="badgeTextColor"format="color"/> <attrname="badgeTextSize"format="dimension"/> <attrname="badgePadding"format="dimension"/> <attrname="badgeAnimationDuration"format="integer"/>


最好至少设置badgeTextSize和badgeAnimationDuration。badgeAnimationDuration建议值1000。

动画

要依次显示不同的值请用setValues方法:

1 2 BitmapbitmapX; badgeView.setValues(0,1,bitmapX,3,"Howareyou?");

也可以单独设置一个值:

1 badgeView.setValue(6);

要特别为每个值设置延迟时间请使用helper类BadgeView.AnimationSet(经过初步测试发现没用,badgeAnimationDuration属性才能决定动画时间):

1 2 3 4 5 newBadgeView.AnimationSet(badgeView) .add("Hi!",1000) .add("Howareyou?",1200) .add("Imfine!",1500) .play();

下载

1 2 3 4 5 6 7 8 repositories{ maven{url"https://jitpack.io"} } //... dependencies{ //... compile'com.github.elevenetc:badgeview:v1.0.0' }

demo

项目没有提供demo,我做了一个,运行的最终效果如下:


MainActivity

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 packagecom.jcodecraeer.sample; importandroid.graphics.Bitmap; importandroid.graphics.BitmapFactory; importandroid.os.Bundle; importandroid.support.v7.app.AppCompatActivity; importandroid.view.Menu; importandroid.view.MenuItem; importandroid.view.View; importandroid.view.ViewTreeObserver; importsu.levenetc.android.badgeview.BadgeView; publicclassMainActivityextendsAppCompatActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); finalBadgeViewbadgeView=(BadgeView)this.findViewById(R.id.badgeview); finalBitmapbitmap=BitmapFactory.decodeResource(this.getResources(),R.drawable.ss); badgeView.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ badgeView.setValues(0,1,bitmap,3,"Howareyou?"); } }); badgeView.getViewTreeObserver().addOnPreDrawListener(newViewTreeObserver.OnPreDrawListener(){ @Override publicbooleanonPreDraw(){ //必须remove掉不然会重复调用 badgeView.getViewTreeObserver().removeOnPreDrawListener(this); badgeView.setValue(6); returnfalse; }}); } @Override publicbooleanonCreateOptionsMenu(Menumenu){ //Inflatethemenu;thisaddsitemstotheactionbarifitispresent. getMenuInflater().inflate(R.menu.menu_main,menu); returntrue; } @Override publicbooleanonOptionsItemSelected(MenuItemitem){ //Handleactionbaritemclickshere.Theactionbarwill //automaticallyhandleclicksontheHome/Upbutton,solong //asyouspecifyaparentactivityinAndroidManifest.xml. intid=item.getItemId(); //noinspectionSimplifiableIfStatement if(id==R.id.action_settings){ returntrue; } returnsuper.onOptionsItemSelected(item); } }

activity_main.xml

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <RelativeLayoutxmlns: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:layout_width="match_parent" android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"tools:context=".MainActivity"> <su.levenetc.android.badgeview.BadgeView android:id="@+id/badgeview" android:layout_width="wrap_content" android:layout_height="wrap_content" app:badgeTextSize="16dip" app:badgeAnimationDuration="1000" app:badgeText="Hello!"/> </RelativeLayout>

更多相关文章

  1. Android(安卓)开发笔记 动画效果 --Animation
  2. android 属性android:visibility
  3. Android(安卓)AlarmManager的取消
  4. 在iOS / Android的统一字体问题
  5. Android手动画柱状图的例子
  6. Android中的矢量动画
  7. Android设置View的margin
  8. 关于单选框RadioGroup和RadioButton
  9. Android之android:theme设置在Application 和 Activity的区别

随机推荐

  1. 【转载】android仿QQ列表的效果实现
  2. android内核编译过程
  3. Android各种获取Context方法
  4. Android(安卓)Lock 使用
  5. Android下使用Logcat打印信息
  6. android 知识点汇总
  7. Android(安卓)BitmapUtils工具类
  8. [置顶] android开发之 listview中的item
  9. Android(安卓)报错加入第三方库报错
  10. android 中如何限制 EditText 最大输入字