0

I m creating an clock which will show the present time on the UI I want to show the digitd through the pictures from 0 to 1 For this i m taking the images from 0 to 9 one over the another making a stack and then changing the tranparancy of the image according to the number i want to show

@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;}
        }
    }));
}

}

I cannot take 10 images for each digit i want to show can any 1 suggest any other way to do that. Is it possible through "view"..? can any1 tel me how to use views in android..?

4

1 回答 1

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]])); 
    }
于 2013-02-02T15:51:39.620 回答