【网学网提醒】:网学会员为需要朋友们搜集整理了android-自定义换件--数字图象时钟控件相关资料,希望对各位网友有所帮助!
android自定义换件--数字图象时钟控件android自定义换件--数字图象时钟控件
android本身包含有数量庞大的控件,一般来说是可以用来满足日常开发工作的.但为了满足人们日益增强的审美需求和视觉体验,各家各户的
程序员和UI大师无不绞尽脑汁设计更复杂,功能更丰富,效果更绚丽的自定义控件.这是个数字时钟控件,由于一些原因,没有使用字体,而是使用了各种字体的图像包贴图实现.代码如下:
控件布局,使用6个
图片控件,分别显示时间和各个数字.布局如下:
控件代码为控件提供
属性操作,代码如下:
packagecom.a;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.view.LayoutInflater;importandroid.widget.ImageView;importandroid.widget.RelativeLayout;publicclassImageTimeextendsRelativeLayout{ privateImageViewimageView1; privateImageViewimageView2; privateImageViewimageView3; privateImageViewimageView4; privateImageViewimageView5; privateImageViewimageView6; publicImageTime(Contextcontext){ super(context); //TODOAuto-generatedconstructorstub } publicImageTime(Contextcontext,AttributeSetattrs){ super(context,attrs); //TODOAuto-generatedconstructorstub LayoutInflaterinflater=(LayoutInflater)context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.imagebtn,this); imageView1=(ImageView)findViewById(R.id.imageView1); imageView2=(ImageView)findViewById(R.id.imageView2); imageView3=(ImageView)findViewById(R.id.imageView3); imageView4=(ImageView)findViewById(R.id.imageView4); imageView5=(ImageView)findViewById(R.id.imageView5); imageView6=(ImageView)findViewById(R.id.imageView6); } /** * *设置图片资源 */ publicvoidsetImageResource1(intresId){ imageView1.setImageResource(resId); } publicvoidsetImageResource2(intresId){ imageView2.setImageResource(resId); } publicvoidsetImageResource3(intresId){ imageView3.setImageResource(resId); } publicvoidsetImageResource4(intresId){ imageView4.setImageResource(resId); } publicvoidsetImageResource5(intresId){ imageView5.setImageResource(resId); } publicvoidsetImageResource6(intresId){ imageView6.setImageResource(resId); } }简单使用控件,把控件加入窗口,对就布局和代码如下:
packagecom.a;importandroid.app.Activity;importandroid.os.Bundle;importandroid.text.format.Time;publicclassIdentifyButtonActivityextendsActivity{privateImageTimeimageBtn1;privatestaticfinalint[]TIME_IMGS={R.drawable.t0,R.drawable.t1,R.drawable.t2,R.drawable.t3,R.drawable.t4,R.drawable.t5,R.drawable.t6,R.drawable.t7,R.drawable.t8,R.drawable.t9,R.drawable.am,R.drawable.pm,R.drawable.colon};/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);imageBtn1=(ImageTime)this.findViewById(R.id.btn_right);Timetime=newTime("GMT+8");time.setToNow();intyear=time.year;intmonth=time.month;intday=time.monthDay;int
minute=time.minute;inthour=time.hour;intsec=time.second;imageBtn1.setImageResource1(TIME_IMGS[hour/10]);imageBtn1.setImageResource2(TIME_IMGS[hour%10]);//imageBtn1.setImageResource3(R.drawable.t2);imageBtn1.setImageResource4(TIME_IMGS[minute/10]);imageBtn1.setImageResource5(TIME_IMGS[minute%10]);imageBtn1.setImageResource6(R.drawable.am);}}