• 钟表动画的实现


    1,android中凡是牵涉到动画一般需要线程,及在线程中进行一定时间的休眠来实现。(除了系统自带的几种animation)

    2,在项目中需要每个钟表的色块需要实现从0点至当前时间的顺序添加代表不同时钟的色块,并且需要根据等级选择不同颜色的色块。

    实现的步骤如下:

    (1)将不同颜色,代表不同时刻的图片资源id利用二维数组来表示:

     private int[] levels={1,3,2,1,2,2,3,2,1,3,1};
    public
    static int[][] imgs = { { R.drawable.brown1, R.drawable.brown2, R.drawable.brown3, R.drawable.brown4, R.drawable.brown5, R.drawable.brown6, R.drawable.brown7, R.drawable.brown8, R.drawable.brown9, R.drawable.brown10,R.drawable.brown11 }, { R.drawable.red1, R.drawable.red2, R.drawable.red3, R.drawable.red4, R.drawable.red5, R.drawable.red6, R.drawable.red7, R.drawable.red8, R.drawable.red9, R.drawable.red10,R.drawable.red11 }, { R.drawable.green1, R.drawable.green2, R.drawable.green3, R.drawable.green4, R.drawable.green5, R.drawable.green6, R.drawable.green7, R.drawable.green8, R.drawable.green9, R.drawable.green10 ,R.drawable.green11} };

    (2)接下来主线程的代码如下:

        private void generateClock() {
            //首先添加钟表的背景图片
            ImageView clock=new ImageView(ShouyeActivity.this);
            clock.setImageResource(com.bobo.mylvluo.R.drawable.clock_bg);
            frame_clock.addView(clock);        
            //开启线程
            new AddImgThread().start();
     
        }

    (3)而实现动画的线程如下:(记得,主线程中的UI未必只有while)

    class AddImgThread extends Thread{
            public void run(){
                 for(int i=0;i<levels.length;i++){
                     Message msg=new Message();
                     msg.arg1=i;
                     handler.sendMessage(msg);
                     try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                 }
    
            }
        }

    (4)在主界面更新UI的handler为

        private Handler handler=new Handler(){
    
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                    int i=msg.arg1;
                    ImageView img=new ImageView(ShouyeActivity.this);
                    //其中i+1代表的是时间;level【i】代表i+1时间的空气质量等级,img【levels【i】-1】【i】代表的是对应的图片
                    img.setImageResource(Constant.imgs[levels[i]-1][i]);
                    //为其添加一个位移的动画效果
                    TranslateAnimation ani=new TranslateAnimation(5f,0f,5f,0f);
                    ani.setDuration(200);
                    frame_clock.addView(img);
                    
                    img.setAnimation(ani);
                    
                
                
            }
            
        };
  • 相关阅读:
    PAT (Advanced Level) 1114. Family Property (25)
    PAT (Advanced Level) 1113. Integer Set Partition (25)
    PAT (Advanced Level) 1112. Stucked Keyboard (20)
    PAT (Advanced Level) 1111. Online Map (30)
    PAT (Advanced Level) 1110. Complete Binary Tree (25)
    PAT (Advanced Level) 1109. Group Photo (25)
    PAT (Advanced Level) 1108. Finding Average (20)
    PAT (Advanced Level) 1107. Social Clusters (30)
    PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)
    PAT (Advanced Level) 1105. Spiral Matrix (25)
  • 原文地址:https://www.cnblogs.com/bobodeboke/p/3035597.html
Copyright © 2020-2023  润新知