我创建了一个时钟,它将在UI上显示当前时间 我想通过0到1的图片显示数字 为此我将图像从0到9一个在另一个上进行堆叠,然后根据我想要显示的数字改变图像的透明度
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
i0.setAlpha(1);
i1.setAlpha(1);
i2.setAlpha(1);
i3.setAlpha(1);
i4.setAlpha(1);
i5.setAlpha(1);
i6.setAlpha(1);
i7.setAlpha(1);
i8.setAlpha(1);
i9.setAlpha(1);
Timer Tim=new Timer();
Tim.schedule(new TimerTask()
{ @Override
public void run()
{ runThread(); }
},1000,1000);
}
private void runThread()
{ runOnUiThread (new Thread(new Runnable()
{ public void run()
{
if(x==0){
i0.setAlpha(1000);
i1.setAlpha(1);
i2.setAlpha(1);
i3.setAlpha(1);
i4.setAlpha(1);
i5.setAlpha(1);
i6.setAlpha(1);
i7.setAlpha(1);
i8.setAlpha(1);
i9.setAlpha(1);}
if(x==1){
i0.setAlpha(1);
i1.setAlpha(1000);
i2.setAlpha(1);
i3.setAlpha(1);
i4.setAlpha(1);
i5.setAlpha(1);
i6.setAlpha(1);
i7.setAlpha(1);
i8.setAlpha(1);
i9.setAlpha(1);}
if(x==2){
i0.setAlpha(1);
i1.setAlpha(1);
i2.setAlpha(1000);
i3.setAlpha(1);
i4.setAlpha(1);
i5.setAlpha(1);
i6.setAlpha(1);
i7.setAlpha(1);
i8.setAlpha(1);
i9.setAlpha(1);}
if(x==3){
i0.setAlpha(1);
i1.setAlpha(1);
i2.setAlpha(1);
i3.setAlpha(1000);
i4.setAlpha(1);
i5.setAlpha(1);
i6.setAlpha(1);
i7.setAlpha(1);
i8.setAlpha(1);
i9.setAlpha(1);}
if(x==4){
i0.setAlpha(1);
i1.setAlpha(1);
i2.setAlpha(1);
i3.setAlpha(1);
i4.setAlpha(1000);
i5.setAlpha(1);
i6.setAlpha(1);
i7.setAlpha(1);
i8.setAlpha(1);
i9.setAlpha(1);}
if(x==5){
i0.setAlpha(1);
i1.setAlpha(1);
i2.setAlpha(1);
i3.setAlpha(1);
i4.setAlpha(1);
i5.setAlpha(1000);
i6.setAlpha(1);
i7.setAlpha(1);
i8.setAlpha(1);
i9.setAlpha(1);}
if(x==6){
i0.setAlpha(1);
i1.setAlpha(1);
i2.setAlpha(1);
i3.setAlpha(1);
i4.setAlpha(1);
i5.setAlpha(1);
i6.setAlpha(1000);
i7.setAlpha(1);
i8.setAlpha(1);
i9.setAlpha(1);}
if(x==7){
i0.setAlpha(1);
i1.setAlpha(1);
i2.setAlpha(1);
i3.setAlpha(1);
i4.setAlpha(1);
i5.setAlpha(1);
i6.setAlpha(1);
i7.setAlpha(1000);
i8.setAlpha(1);
i9.setAlpha(1);}
if(x==8){
i0.setAlpha(1);
i1.setAlpha(1);
i2.setAlpha(1);
i3.setAlpha(1);
i4.setAlpha(1);
i5.setAlpha(1);
i6.setAlpha(1);
i7.setAlpha(1);
i8.setAlpha(1000);
i9.setAlpha(1);}
if(x==9){
i0.setAlpha(1);
i1.setAlpha(1);
i2.setAlpha(1);
i3.setAlpha(1);
i4.setAlpha(1);
i5.setAlpha(1);
i6.setAlpha(1);
i7.setAlpha(1);
i8.setAlpha(1);
i9.setAlpha(1000);}
x=x+1;
if (x==10){x=0;}
}
}));
}
}
我想要显示的每个数字都不能拍摄10张图像 可以任何1建议任何其他方式来做到这一点。 是否有可能通过“观察”..? 任何人都可以告诉我如何在android中使用视图..?
答案 0 :(得分:0)
我认为如果使用字体而不是所有图像,这可能会更容易。我确定你可以通过使用TextView来实现相同的效果,你将时间放入其中然后改变背景。
有关如何使用资产文件夹中的字体
,请参阅http://www.youtube.com/watch?v=kOJGmVXuuFA编辑(新答案): 在时钟的xml布局中创建ImageButtons,具体取决于您的时钟有多少位数。
int[] x = {....} \\the digits you wish to display
int[] images = { R.drawable.0, R.drawable.1,...,R.drawable.9); //the saves of your images 0-9
int[] btnArray = { R.id.b1, R.id.b2,...}; //the ImageButtons used to display your digits
for(int i = 0; i < numberofbuttons; i++) {
ImageButton btn = (ImageButton) findViewById(btnArray[i-1]);
btn.setBackgroundDrawable(getResources().getDrawable(
images[x[i]]));
}