Android 动画显示文字与bitmap的BadgeView
16lz
2021-01-23
原文链接: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= new BadgeView( 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 | new BadgeView.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( new View.OnClickListener(){
@Override
publicvoidonClick(Viewv){
badgeView.setValues(0,1,bitmap,3, "Howareyou?" );
}
});
badgeView.getViewTreeObserver().addOnPreDrawListener( new ViewTreeObserver.OnPreDrawListener(){
@Override
publicbooleanonPreDraw(){
//必须remove掉不然会重复调用
badgeView.getViewTreeObserver().removeOnPreDrawListener( this );
badgeView.setValue(6);
return false ;
}});
}
@Override
publicbooleanonCreateOptionsMenu(Menumenu){
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
getMenuInflater().inflate(R.menu.menu_main,menu);
return true ;
}
@Override
publicbooleanonOptionsItemSelected(MenuItemitem){
//Handleactionbaritemclickshere.Theactionbarwill
//automaticallyhandleclicksontheHome/Upbutton,solong
//asyouspecifyaparentactivityinAndroidManifest.xml.
intid=item.getItemId();
//noinspectionSimplifiableIfStatement
if (id==R.id.action_settings){
return true ;
}
return super .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> |