滚动的电表,类似于家里的电表和水表,也是在之前的pickView 基础修改的,如果有好的方案能够实现类似于这样滚动表的请记得告诉我 哦

 

 

1.主Activity

 

 
  1. package com.example.scrolnum;  
  2.  
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.  
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.os.Handler;  
  9. import android.os.Message;  
  10. import android.util.Log;  
  11. import android.widget.Button;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.TextView;  
  14.  
  15. import com.example.scrolnum.adapter.CountryAdapter;  
  16. import com.example.scrolnum.view.OnWheelChangedListener;  
  17. import com.example.scrolnum.view.WheelView;  
  18.  
  19. public class MainActivity extends Activity {  
  20.     public static MainActivity instance;  
  21.     private Button back_btn;  
  22.     private TextView title_text;  
  23.     private int num = 1234567;  
  24.     private LinearLayout content_linear;  
  25.     private Button button;  
  26.     int i = 0;  
  27.     WheelView country1, country2, country3, country4, country5, country6,  
  28.             country7;  
  29.  
  30.     @Override 
  31.     protected void onCreate(Bundle savedInstanceState) {  
  32.         // TODO Auto-generated method stub  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.activity_main);  
  35.         instance = this;  
  36.         getViews();  
  37.         setInitialize();  
  38.         setOnListener();  
  39.         set1();  
  40.     }  
  41.  
  42.     private void getViews() {  
  43.  
  44.         country1 = (WheelView) findViewById(R.id.country1);  
  45.         country1.setVisibleItems(3);  
  46.         country1.setViewAdapter(new CountryAdapter(this));  
  47.         country1.setCyclic(true);  
  48.         country1.setClickable(false);  
  49.         country1.setFocusable(false);  
  50.  
  51.         country2 = (WheelView) findViewById(R.id.country2);  
  52.         country2.setVisibleItems(3);  
  53.         country2.setViewAdapter(new CountryAdapter(this));  
  54.         country2.setCyclic(true);  
  55.         country2.setCurrentItem(0);  
  56.  
  57.         country3 = (WheelView) findViewById(R.id.country3);  
  58.         country3.setVisibleItems(3);  
  59.         country3.setViewAdapter(new CountryAdapter(this));  
  60.         country3.setCyclic(true);  
  61.         country3.setCurrentItem(0);  
  62.  
  63.         country4 = (WheelView) findViewById(R.id.country4);  
  64.         country4.setVisibleItems(3);  
  65.         country4.setViewAdapter(new CountryAdapter(this));  
  66.         country4.setCyclic(true);  
  67.         country4.setCurrentItem(0);  
  68.  
  69.         country5 = (WheelView) findViewById(R.id.country5);  
  70.         country5.setVisibleItems(3);  
  71.         country5.setViewAdapter(new CountryAdapter(this));  
  72.         country5.setCyclic(true);  
  73.         country5.setCurrentItem(0);  
  74.  
  75.         country6 = (WheelView) findViewById(R.id.country6);  
  76.         country6.setVisibleItems(3);  
  77.         country6.setViewAdapter(new CountryAdapter(this));  
  78.         country6.setCyclic(true);  
  79.         country6.setCurrentItem(0);  
  80.  
  81.         country7 = (WheelView) findViewById(R.id.country7);  
  82.         country7.setVisibleItems(3);  
  83.         country7.setViewAdapter(new CountryAdapter(this));  
  84.         country7.setCyclic(true);  
  85.         country7.setCurrentItem(0);  
  86.  
  87.     }  
  88.  
  89.     private void setOnListener() {  
  90.         country1.addChangingListener(new OnWheelChangedListener() {  
  91.  
  92.             @Override 
  93.             public void onChanged(WheelView wheel, int oldValue, int newValue) {  
  94.                 // TODO Auto-generated method stub  
  95.  
  96.                 if (oldValue == 9 && timer2 == null) {  
  97.                     set2();  
  98.                 }  
  99.  
  100.             }  
  101.         });  
  102.  
  103.         country2.addChangingListener(new OnWheelChangedListener() {  
  104.  
  105.             @Override 
  106.             public void onChanged(WheelView wheel, int oldValue, int newValue) {  
  107.                 // TODO Auto-generated method stub  
  108.  
  109.                 if (oldValue == 9 && timer3 == null) {  
  110.                     set3();  
  111.                 }  
  112.  
  113.             }  
  114.         });  
  115.  
  116.         country3.addChangingListener(new OnWheelChangedListener() {  
  117.  
  118.             @Override 
  119.             public void onChanged(WheelView wheel, int oldValue, int newValue) {  
  120.                 // TODO Auto-generated method stub  
  121.  
  122.                 if (oldValue == 9 && timer4 == null) {  
  123.                     set4();  
  124.                 }  
  125.  
  126.             }  
  127.         });  
  128.  
  129.         country4.addChangingListener(new OnWheelChangedListener() {  
  130.  
  131.             @Override 
  132.             public void onChanged(WheelView wheel, int oldValue, int newValue) {  
  133.                 // TODO Auto-generated method stub  
  134.  
  135.                 if (oldValue == 9 && timer5 == null) {  
  136.                     set5();  
  137.                 }  
  138.  
  139.             }  
  140.         });  
  141.  
  142.         country5.addChangingListener(new OnWheelChangedListener() {  
  143.  
  144.             @Override 
  145.             public void onChanged(WheelView wheel, int oldValue, int newValue) {  
  146.                 // TODO Auto-generated method stub  
  147.  
  148.                 if (oldValue == 9 && timer6 == null) {  
  149.                     set6();  
  150.                 }  
  151.  
  152.             }  
  153.         });  
  154.  
  155.         country6.addChangingListener(new OnWheelChangedListener() {  
  156.  
  157.             @Override 
  158.             public void onChanged(WheelView wheel, int oldValue, int newValue) {  
  159.                 // TODO Auto-generated method stub  
  160.  
  161.                 if (oldValue == 9 && timer7 == null) {  
  162.                     set7();  
  163.                 }  
  164.  
  165.             }  
  166.         });  
  167.  
  168.  
  169.     }  
  170.  
  171.     Timer timer1;  
  172.     Timer timer2;  
  173.     Timer timer3;  
  174.     Timer timer4;  
  175.     Timer timer5;  
  176.     Timer timer6;  
  177.     Timer timer7;  
  178.  
  179.     private void set1() {  
  180.         timer1 = new Timer();  
  181.  
  182.         timer1.schedule(new TimerTask() {  
  183.  
  184.             @Override 
  185.             public void run() {  
  186.                 // TODO Auto-generated method stub  
  187.                 handler.sendEmptyMessage(1);  
  188.             }  
  189.         }, 50005000);  
  190.     }  
  191.  
  192.     private void set2() {  
  193.         timer2 = new Timer();  
  194.         timer2.schedule(new TimerTask() {  
  195.  
  196.             @Override 
  197.             public void run() {  
  198.                 // TODO Auto-generated method stub  
  199.                 handler.sendEmptyMessage(2);  
  200.             }  
  201.         }, 05000 * 10);  
  202.     }  
  203.  
  204.     private void set3() {  
  205.         timer3 = new Timer();  
  206.         timer3.schedule(new TimerTask() {  
  207.  
  208.             @Override 
  209.             public void run() {  
  210.                 // TODO Auto-generated method stub  
  211.                 handler.sendEmptyMessage(3);  
  212.             }  
  213.         }, 05000 * 100);  
  214.     }  
  215.  
  216.     private void set4() {  
  217.         timer4 = new Timer();  
  218.         timer4.schedule(new TimerTask() {  
  219.  
  220.             @Override 
  221.             public void run() {  
  222.                 // TODO Auto-generated method stub  
  223.                 handler.sendEmptyMessage(4);  
  224.             }  
  225.         }, 05000 * 1000);  
  226.     }  
  227.  
  228.     private void set5() {  
  229.         timer5 = new Timer();  
  230.         timer5.schedule(new TimerTask() {  
  231.  
  232.             @Override 
  233.             public void run() {  
  234.                 // TODO Auto-generated method stub  
  235.                 handler.sendEmptyMessage(5);  
  236.             }  
  237.         }, 05000 * 10000);  
  238.     }  
  239.  
  240.     private void set6() {  
  241.         timer6 = new Timer();  
  242.         timer6.schedule(new TimerTask() {  
  243.  
  244.             @Override 
  245.             public void run() {  
  246.                 // TODO Auto-generated method stub  
  247.                 handler.sendEmptyMessage(6);  
  248.             }  
  249.         }, 05000 * 100000);  
  250.     }  
  251.  
  252.     private void set7() {  
  253.         timer7 = new Timer();  
  254.         timer7.schedule(new TimerTask() {  
  255.  
  256.             @Override 
  257.             public void run() {  
  258.                 // TODO Auto-generated method stub  
  259.                 handler.sendEmptyMessage(7);  
  260.             }  
  261.         }, 05000 * 100000);  
  262.     }  
  263.  
  264.  
  265.     private Handler handler = new Handler() {  
  266.         @Override 
  267.         public void handleMessage(Message msg) {  
  268.             super.handleMessage(msg);  
  269.             switch (msg.what) {  
  270.             case 1:  
  271.                 // country.setCurrentItem(i);  
  272.                 country1.scroll(0 + 13000);  
  273.                 break;  
  274.             case 2:  
  275.                 // country.setCurrentItem(i);  
  276.                 country2.scroll(0 + 13000);  
  277.                 break;  
  278.             case 3:  
  279.                 // country.setCurrentItem(i);  
  280.                 country3.scroll(0 + 13000);  
  281.                 break;  
  282.             case 4:  
  283.                 // country.setCurrentItem(i);  
  284.                 country4.scroll(0 + 13000);  
  285.                 break;  
  286.  
  287.             case 5:  
  288.                 // country.setCurrentItem(i);  
  289.                 country5.scroll(0 + 13000);  
  290.                 break;  
  291.             case 6:  
  292.                 // country.setCurrentItem(i);  
  293.                 country6.scroll(0 + 13000);  
  294.                 break;  
  295.             case 7:  
  296.                 // country.setCurrentItem(i);  
  297.                 country7.scroll(0 + 13000);  
  298.                 break;  
  299.             }  
  300.         }  
  301.     };  
  302.  
  303.     /**  
  304.      * 设置初始值  
  305.      */ 
  306.     private int a1, a2, a3, a4, a5, a6, a7, a8;  
  307.     private int[] a = { a1, a2, a3, a4, a5, a6, a7, a8 };  
  308.     int j = 0;  
  309.  
  310.     private int[] getNum() {  
  311.         while (num % 10 != 0) {  
  312.             a[j] = num % 10;  
  313.             Log.d("debug""a[j]= " + num % 10);  
  314.             num = num / 10;  
  315.             Log.d("debug""num= " + num);  
  316.             j++;  
  317.         }  
  318.         return a;  
  319.     }  
  320.  
  321.     /**  
  322.      * 初始化表盘上的数字  
  323.      */ 
  324.  
  325.     private void setInitialize() {  
  326.         getNum();  
  327.         country1.setCurrentItem(a[0]);  
  328.         country2.setCurrentItem(a[1]);  
  329.         country3.setCurrentItem(a[2]);  
  330.         country4.setCurrentItem(a[3]);  
  331.         country5.setCurrentItem(a[4]);  
  332.         country6.setCurrentItem(a[5]);  
  333.         country7.setCurrentItem(a[6]);  
  334.     }  
  335. }  

 

2.相对的布局文件

 

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <LinearLayout 
  8.         android:layout_width="match_parent" 
  9.         android:layout_height="match_parent" 
  10.         android:orientation="vertical" 
  11.         android:gravity="center" > 
  12.  
  13.  
  14.  
  15.             <RelativeLayout 
  16.                 android:layout_width="434dp" 
  17.                 android:layout_height="wrap_content" 
  18.                 android:gravity="center" > 
  19.  
  20.                 <LinearLayout 
  21.                     android:id="@+id/linearLayout1" 
  22.                     android:layout_width="wrap_content" 
  23.                     android:layout_height="wrap_content" 
  24.                     android:layout_alignParentBottom="true" 
  25.                     android:layout_centerHorizontal="true" 
  26.                     android:layout_marginBottom="23dp" 
  27.                     android:layout_weight="1" 
  28.                     android:background="#000000" 
  29.                     android:orientation="horizontal" > 
  30.  
  31.                     <com.example.scrolnum.view.WheelView 
  32.                         android:id="@+id/country7" 
  33.                         android:layout_width="wrap_content" 
  34.                         android:layout_height="100.0dip" 
  35.                         android:layout_gravity="center_vertical" /> 
  36.  
  37.                     <com.example.scrolnum.view.WheelView 
  38.                         android:id="@+id/country6" 
  39.                         android:layout_width="wrap_content" 
  40.                         android:layout_height="100.0dip" 
  41.                         android:layout_gravity="center_vertical" /> 
  42.  
  43.                     <com.example.scrolnum.view.WheelView 
  44.                         android:id="@+id/country5" 
  45.                         android:layout_width="wrap_content" 
  46.                         android:layout_height="100.0dip" 
  47.                         android:layout_gravity="center_vertical" /> 
  48.  
  49.                     <com.example.scrolnum.view.WheelView 
  50.                         android:id="@+id/country4" 
  51.                         android:layout_width="wrap_content" 
  52.                         android:layout_height="100.0dip" 
  53.                         android:layout_gravity="center_vertical" /> 
  54.  
  55.                     <com.example.scrolnum.view.WheelView 
  56.                         android:id="@+id/country3" 
  57.                         android:layout_width="wrap_content" 
  58.                         android:layout_height="100.0dip" 
  59.                         android:layout_gravity="center_vertical" /> 
  60.  
  61.                     <com.example.scrolnum.view.WheelView 
  62.                         android:id="@+id/country2" 
  63.                         android:layout_width="wrap_content" 
  64.                         android:layout_height="100.0dip" 
  65.                         android:layout_gravity="center_vertical" /> 
  66.  
  67.                     <com.example.scrolnum.view.WheelView 
  68.                         android:id="@+id/country1" 
  69.                         android:layout_width="wrap_content" 
  70.                         android:layout_height="100.0dip" 
  71.                         android:layout_gravity="center_vertical" 
  72.                         android:layout_marginRight="5dp" /> 
  73.                 </LinearLayout> 
  74.  
  75.                 <ImageView 
  76.                     android:layout_width="match_parent" 
  77.                     android:layout_height="30dp" 
  78.                     android:layout_alignParentLeft="true" 
  79.                     android:layout_alignTop="@+id/linearLayout1" 
  80.                     android:layout_weight="1.14" 
  81.                     android:background="#ffffff" /> 
  82.  
  83.                 <ImageView 
  84.                     android:layout_width="match_parent" 
  85.                     android:layout_height="30dp" 
  86.                     android:layout_alignBottom="@+id/linearLayout1" 
  87.                     android:layout_alignParentLeft="true" 
  88.                     android:layout_weight="1.14" 
  89.                     android:background="#ffffff" /> 
  90.             </RelativeLayout> 
  91.  
  92.             <LinearLayout 
  93.                 android:layout_width="fill_parent" 
  94.                 android:layout_height="wrap_content" 
  95.                 android:gravity="center_horizontal" 
  96.                 android:orientation="horizontal" > 
  97.             </LinearLayout> 
  98.         </LinearLayout> 
  99.  
  100. </LinearLayout> 

 

 

 

 

 

 

 

~~~~~~~~~~~~具体还是看工程吧,超过8w字符不让贴代码了~~~~~~~~~~~~~~~~