700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android实战简易教程六十五(自定义控件实现数字液晶时钟Demo)

Android实战简易教程六十五(自定义控件实现数字液晶时钟Demo)

时间:2023-12-31 11:15:22

相关推荐

Android实战简易教程六十五(自定义控件实现数字液晶时钟Demo)

下面我们研究一下如何实现一个数字液晶时钟,本质属于特效一种哈。

首先创建一个布局文件:

[html]view plain copy<?xmlversion="1.0"encoding="utf-8"?> <RelativeLayoutxmlns:android="/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/ledview_clock_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:shadowColor="#00ff00" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="10" android:textColor="#00ff00" android:textSize="80sp"/> <TextView android:id="@+id/ledview_clock_bg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center" android:text="@string/default_time" android:textColor="#3300ff00" android:textSize="80sp"/> </RelativeLayout>

对于阴影的几个属性我们引用一下别人博客里的内容:(/whoispo/article/details/8061907)

Android的TextView的XML属性中有关于阴影的几条属性

shadowDX、shadowDy、shadowRadius,说明分别是阴影的横、纵坐标偏移,以及阴影的半径,这个不太好理解。一下的图可以实际说明这些的参数的内容。

shadowDx,shadowDy从下面三幅图可以看出是是什么

DX=20,Dy=0

DX=0,DY=20

DX=20,DY=20

shadowRadius可以从下面三幅图看出是什么

R=3

R=10

R=40

通过这些效果可以直观的看出每个属性的含义。

下面自定义一个控件:

[java]view plain copypackagecom.yayun.leddemo; importjava.io.File; importjava.util.Calendar; importjava.util.Date; importjava.util.TimeZone; importandroid.annotation.SuppressLint; importandroid.content.Context; importandroid.content.res.AssetManager; importandroid.graphics.Typeface; importandroid.os.Handler; importandroid.util.AttributeSet; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.widget.LinearLayout; importandroid.widget.TextView; publicclassLEDViewextendsLinearLayout{ privateTextViewtimeView; privateTextViewbgView; privatestaticfinalStringFONT_DIGITAL_7="fonts"+File.separator +"digital-7.ttf";//字体 privatestaticfinalStringDATE_FORMAT="%02d:%02d:%02d";//日期格式 privatestaticfinalintREFRESH_DELAY=500;//刷新延迟 privatefinalHandlermHandler=newHandler(); privatefinalRunnablemTimeRefresher=newRunnable(){ @Override publicvoidrun(){ Calendarcalendar=Calendar.getInstance(TimeZone .getTimeZone("GMT+8"));//时区 finalDated=newDate(); calendar.setTime(d); timeView.setText(String.format(DATE_FORMAT, calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND))); mHandler.postDelayed(this,REFRESH_DELAY); } }; @SuppressLint("NewApi") publicLEDView(Contextcontext,AttributeSetattrs,intdefStyle){ super(context,attrs,defStyle); init(context); } publicLEDView(Contextcontext,AttributeSetattrs){ super(context,attrs); init(context); } publicLEDView(Contextcontext){ super(context); init(context); } privatevoidinit(Contextcontext){ LayoutInflaterlayoutInflater=LayoutInflater.from(context); Viewview=layoutInflater.inflate(R.layout.ledview,this); timeView=(TextView)view.findViewById(R.id.ledview_clock_time); bgView=(TextView)view.findViewById(R.id.ledview_clock_bg); AssetManagerassets=context.getAssets();//字体管家类 finalTypefacefont=Typeface.createFromAsset(assets,FONT_DIGITAL_7); timeView.setTypeface(font);//设置字体 bgView.setTypeface(font); } publicvoidstart(){ mHandler.post(mTimeRefresher); } publicvoidstop(){ mHandler.removeCallbacks(mTimeRefresher); } }

新建一个Activity调用这个控件:

[html]view plain copy<RelativeLayoutxmlns:android="/apk/res/android" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".LEDActivity" android:background="@color/black"> <com.yayun.leddemo.LEDView android:id="@+id/ledview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center"/> </RelativeLayout>

[java]view plain copypackagecom.yayun.leddemo; importandroid.annotation.SuppressLint; importandroid.annotation.TargetApi; importandroid.app.ActionBar; importandroid.app.Activity; importandroid.os.Build; importandroid.os.Bundle; importandroid.view.Menu; @TargetApi(Build.VERSION_CODES.HONEYCOMB) publicclassLEDActivityextendsActivity{ privateLEDViewledView; @SuppressLint("NewApi") @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_led); ledView=(LEDView)findViewById(R.id.ledview); ActionBaractionBar=getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); } @Override protectedvoidonResume(){ super.onResume(); ledView.start();//调用开始 } @Override protectedvoidonStop(){ super.onStop(); ledView.stop();//暂停 } @Override publicbooleanonCreateOptionsMenu(Menumenu){ getMenuInflater().inflate(R.menu.activity_led,menu); returntrue; } }

运行实例如下:

录制显示问题,不知道为什么,大家可以自行运行查看效果。

喜欢的朋友关注我,谢谢!

源码下载

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。