滚动的电表,类似于家里的电表和水表,也是在之前的pickView 基础修改的,如果有好的方案能够实现类似于这样滚动表的请记得告诉我 哦
1.主Activity
- package com.example.scrolnum;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import com.example.scrolnum.adapter.CountryAdapter;
- import com.example.scrolnum.view.OnWheelChangedListener;
- import com.example.scrolnum.view.WheelView;
- public class MainActivity extends Activity {
- public static MainActivity instance;
- private Button back_btn;
- private TextView title_text;
- private int num = 1234567;
- private LinearLayout content_linear;
- private Button button;
- int i = 0;
- WheelView country1, country2, country3, country4, country5, country6,
- country7;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- instance = this;
- getViews();
- setInitialize();
- setOnListener();
- set1();
- }
- private void getViews() {
- country1 = (WheelView) findViewById(R.id.country1);
- country1.setVisibleItems(3);
- country1.setViewAdapter(new CountryAdapter(this));
- country1.setCyclic(true);
- country1.setClickable(false);
- country1.setFocusable(false);
- country2 = (WheelView) findViewById(R.id.country2);
- country2.setVisibleItems(3);
- country2.setViewAdapter(new CountryAdapter(this));
- country2.setCyclic(true);
- country2.setCurrentItem(0);
- country3 = (WheelView) findViewById(R.id.country3);
- country3.setVisibleItems(3);
- country3.setViewAdapter(new CountryAdapter(this));
- country3.setCyclic(true);
- country3.setCurrentItem(0);
- country4 = (WheelView) findViewById(R.id.country4);
- country4.setVisibleItems(3);
- country4.setViewAdapter(new CountryAdapter(this));
- country4.setCyclic(true);
- country4.setCurrentItem(0);
- country5 = (WheelView) findViewById(R.id.country5);
- country5.setVisibleItems(3);
- country5.setViewAdapter(new CountryAdapter(this));
- country5.setCyclic(true);
- country5.setCurrentItem(0);
- country6 = (WheelView) findViewById(R.id.country6);
- country6.setVisibleItems(3);
- country6.setViewAdapter(new CountryAdapter(this));
- country6.setCyclic(true);
- country6.setCurrentItem(0);
- country7 = (WheelView) findViewById(R.id.country7);
- country7.setVisibleItems(3);
- country7.setViewAdapter(new CountryAdapter(this));
- country7.setCyclic(true);
- country7.setCurrentItem(0);
- }
- private void setOnListener() {
- country1.addChangingListener(new OnWheelChangedListener() {
- @Override
- public void onChanged(WheelView wheel, int oldValue, int newValue) {
- // TODO Auto-generated method stub
- if (oldValue == 9 && timer2 == null) {
- set2();
- }
- }
- });
- country2.addChangingListener(new OnWheelChangedListener() {
- @Override
- public void onChanged(WheelView wheel, int oldValue, int newValue) {
- // TODO Auto-generated method stub
- if (oldValue == 9 && timer3 == null) {
- set3();
- }
- }
- });
- country3.addChangingListener(new OnWheelChangedListener() {
- @Override
- public void onChanged(WheelView wheel, int oldValue, int newValue) {
- // TODO Auto-generated method stub
- if (oldValue == 9 && timer4 == null) {
- set4();
- }
- }
- });
- country4.addChangingListener(new OnWheelChangedListener() {
- @Override
- public void onChanged(WheelView wheel, int oldValue, int newValue) {
- // TODO Auto-generated method stub
- if (oldValue == 9 && timer5 == null) {
- set5();
- }
- }
- });
- country5.addChangingListener(new OnWheelChangedListener() {
- @Override
- public void onChanged(WheelView wheel, int oldValue, int newValue) {
- // TODO Auto-generated method stub
- if (oldValue == 9 && timer6 == null) {
- set6();
- }
- }
- });
- country6.addChangingListener(new OnWheelChangedListener() {
- @Override
- public void onChanged(WheelView wheel, int oldValue, int newValue) {
- // TODO Auto-generated method stub
- if (oldValue == 9 && timer7 == null) {
- set7();
- }
- }
- });
- }
- Timer timer1;
- Timer timer2;
- Timer timer3;
- Timer timer4;
- Timer timer5;
- Timer timer6;
- Timer timer7;
- private void set1() {
- timer1 = new Timer();
- timer1.schedule(new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- handler.sendEmptyMessage(1);
- }
- }, 5000, 5000);
- }
- private void set2() {
- timer2 = new Timer();
- timer2.schedule(new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- handler.sendEmptyMessage(2);
- }
- }, 0, 5000 * 10);
- }
- private void set3() {
- timer3 = new Timer();
- timer3.schedule(new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- handler.sendEmptyMessage(3);
- }
- }, 0, 5000 * 100);
- }
- private void set4() {
- timer4 = new Timer();
- timer4.schedule(new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- handler.sendEmptyMessage(4);
- }
- }, 0, 5000 * 1000);
- }
- private void set5() {
- timer5 = new Timer();
- timer5.schedule(new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- handler.sendEmptyMessage(5);
- }
- }, 0, 5000 * 10000);
- }
- private void set6() {
- timer6 = new Timer();
- timer6.schedule(new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- handler.sendEmptyMessage(6);
- }
- }, 0, 5000 * 100000);
- }
- private void set7() {
- timer7 = new Timer();
- timer7.schedule(new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- handler.sendEmptyMessage(7);
- }
- }, 0, 5000 * 100000);
- }
- private Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- switch (msg.what) {
- case 1:
- // country.setCurrentItem(i);
- country1.scroll(0 + 1, 3000);
- break;
- case 2:
- // country.setCurrentItem(i);
- country2.scroll(0 + 1, 3000);
- break;
- case 3:
- // country.setCurrentItem(i);
- country3.scroll(0 + 1, 3000);
- break;
- case 4:
- // country.setCurrentItem(i);
- country4.scroll(0 + 1, 3000);
- break;
- case 5:
- // country.setCurrentItem(i);
- country5.scroll(0 + 1, 3000);
- break;
- case 6:
- // country.setCurrentItem(i);
- country6.scroll(0 + 1, 3000);
- break;
- case 7:
- // country.setCurrentItem(i);
- country7.scroll(0 + 1, 3000);
- break;
- }
- }
- };
- /**
- * 设置初始值
- */
- private int a1, a2, a3, a4, a5, a6, a7, a8;
- private int[] a = { a1, a2, a3, a4, a5, a6, a7, a8 };
- int j = 0;
- private int[] getNum() {
- while (num % 10 != 0) {
- a[j] = num % 10;
- Log.d("debug", "a[j]= " + num % 10);
- num = num / 10;
- Log.d("debug", "num= " + num);
- j++;
- }
- return a;
- }
- /**
- * 初始化表盘上的数字
- */
- private void setInitialize() {
- getNum();
- country1.setCurrentItem(a[0]);
- country2.setCurrentItem(a[1]);
- country3.setCurrentItem(a[2]);
- country4.setCurrentItem(a[3]);
- country5.setCurrentItem(a[4]);
- country6.setCurrentItem(a[5]);
- country7.setCurrentItem(a[6]);
- }
- }
2.相对的布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:gravity="center" >
- <RelativeLayout
- android:layout_width="434dp"
- android:layout_height="wrap_content"
- android:gravity="center" >
- <LinearLayout
- android:id="@+id/linearLayout1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="23dp"
- android:layout_weight="1"
- android:background="#000000"
- android:orientation="horizontal" >
- <com.example.scrolnum.view.WheelView
- android:id="@+id/country7"
- android:layout_width="wrap_content"
- android:layout_height="100.0dip"
- android:layout_gravity="center_vertical" />
- <com.example.scrolnum.view.WheelView
- android:id="@+id/country6"
- android:layout_width="wrap_content"
- android:layout_height="100.0dip"
- android:layout_gravity="center_vertical" />
- <com.example.scrolnum.view.WheelView
- android:id="@+id/country5"
- android:layout_width="wrap_content"
- android:layout_height="100.0dip"
- android:layout_gravity="center_vertical" />
- <com.example.scrolnum.view.WheelView
- android:id="@+id/country4"
- android:layout_width="wrap_content"
- android:layout_height="100.0dip"
- android:layout_gravity="center_vertical" />
- <com.example.scrolnum.view.WheelView
- android:id="@+id/country3"
- android:layout_width="wrap_content"
- android:layout_height="100.0dip"
- android:layout_gravity="center_vertical" />
- <com.example.scrolnum.view.WheelView
- android:id="@+id/country2"
- android:layout_width="wrap_content"
- android:layout_height="100.0dip"
- android:layout_gravity="center_vertical" />
- <com.example.scrolnum.view.WheelView
- android:id="@+id/country1"
- android:layout_width="wrap_content"
- android:layout_height="100.0dip"
- android:layout_gravity="center_vertical"
- android:layout_marginRight="5dp" />
- </LinearLayout>
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="30dp"
- android:layout_alignParentLeft="true"
- android:layout_alignTop="@+id/linearLayout1"
- android:layout_weight="1.14"
- android:background="#ffffff" />
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="30dp"
- android:layout_alignBottom="@+id/linearLayout1"
- android:layout_alignParentLeft="true"
- android:layout_weight="1.14"
- android:background="#ffffff" />
- </RelativeLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="horizontal" >
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
~~~~~~~~~~~~具体还是看工程吧,超过8w字符不让贴代码了~~~~~~~~~~~~~~~~