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

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

介绍:

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

运行效果:

Android 动画显示文字与bitmap的BadgeView_第1张图片

使用说明:

添加到布局:

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,我做了一个,运行的最终效果如下:

Android 动画显示文字与bitmap的BadgeView_第2张图片


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手动画柱状图的例子
  2. Android中的矢量动画
  3. Android 自定义dialog出入场动画
  4. Android 属性动画(Property Animation) 完全解析 (上)

随机推荐

  1. 2011.09.01(2)——— android 处理双击事件
  2. 个人练习:ListView绑定数据和显示的几种方
  3. Android(安卓)onSaveInstanceState和onRe
  4. Android(安卓)NFC架构分析
  5. android ios主流浏览器User-Agent
  6. Mobile Development basis
  7. android couldn't save which view has f
  8. Android增大button响应区域
  9. 同时安装调试两个相同的应用 android:pac
  10. Android--MediaPlayer(实现列表选歌,上一