这篇文章主要介绍了Android如何实现模仿正点闹钟时间齿轮滑动效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
创新互联建站是一家集网站建设,天全企业网站建设,天全品牌网站建设,网站定制,天全网站建设报价,网络营销,网络优化,天全网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
效果图如下:
首先是MainActivity的布局文件,这个布局文件非常简单,就是一个Button:activity_main.xml文件,代码如下:
紧接着就是MainActivity的代码,代码如下:
package net.loonggg.test; import net.loonggg.view.CustomerDateDialog; import net.loonggg.view.CustomerDateDialog.DateDialogListener; import android.app.Activity; import android.os.Bundle; import android.text.format.DateFormat; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private int h, m; private CustomerDateDialog dialog; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String datetime = DateFormat.format("kk:mm", System.currentTimeMillis()).toString(); String[] strs = datetime.split(":"); h = Integer.parseInt(strs[0]); m = Integer.parseInt(strs[1]); dialog = new CustomerDateDialog(MainActivity.this, h, m); dialog.show(); dialog.setOnDateDialogListener(new DateDialogListener() { @Override public void getDate() { Toast.makeText( MainActivity.this, "时间是:" + dialog.getSettingHour() + "点" + dialog.getSettingMinute() + "分", Toast.LENGTH_LONG).show(); } }); } }); } }
再就是我自定义了一个时钟的Dialog,自定义Dialog也非常简单,自己可以学一下,这方面网上的资料非常多。现在我把我自定义时钟的Dialog的代码分享一下,代码如下:
package net.loonggg.view; import net.loonggg.test.R; import android.annotation.SuppressLint; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewTreeObserver; import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; @SuppressLint("HandlerLeak") public class CustomerDateDialog extends Dialog { private View customView; private Button setBtn; private Button cancleBtn; private TextView arrow_up; private TextView tv01, tv02; private ScrollView sv01, sv02; private LinearLayout llTimeWheel; private DateDialogListener listener; private int lastY; private int flag;// 标记时分 private int itemHeight;// 每一行的高度 private int pHour, pMinute;// 初始化时显示的时分时间 private int setHour, setMinute; public CustomerDateDialog(Context context, int hour, int minute) { super(context, R.style.CustomerDateDialog); customView = LayoutInflater.from(context).inflate(R.layout.time_wheel, null); init(context, hour, minute); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(customView); } private void init(Context context, final int hour, final int minute) { tv01 = (TextView) customView.findViewById(R.id.tv01); tv02 = (TextView) customView.findViewById(R.id.tv02); sv01 = (ScrollView) customView.findViewById(R.id.sv01); sv02 = (ScrollView) customView.findViewById(R.id.sv02); setBtn = (Button) customView.findViewById(R.id.setBtn); cancleBtn = (Button) customView.findViewById(R.id.cancleBtn); arrow_up = (TextView) customView.findViewById(R.id.arrow_up); this.pHour = hour; this.pMinute = minute; setHour = pHour; setMinute = pMinute; llTimeWheel = (LinearLayout) customView .findViewById(R.id.ll_time_wheel); setHourDial(tv01); setMinuteDial(tv02); sv01.setOnTouchListener(tListener); sv02.setOnTouchListener(tListener); final ViewTreeObserver observer = sv01.getViewTreeObserver();// observer // 作用当视图完全加载进来的时候再取控件的高度,否则取得值是0 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @SuppressWarnings("deprecation") public void onGlobalLayout() { int tvHeight = tv02.getHeight(); itemHeight = tvHeight / 180; if (sv01.getViewTreeObserver().isAlive()) { sv01.getViewTreeObserver().removeGlobalOnLayoutListener( this); } LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, (itemHeight * 3) + arrow_up.getHeight() * 2); llTimeWheel.setLayoutParams(params); sv01.setLayoutParams(new LinearLayout.LayoutParams(tv02 .getWidth(), (itemHeight * 3))); sv02.setLayoutParams(new LinearLayout.LayoutParams(tv02 .getWidth(), (itemHeight * 3))); sv01.scrollTo(0, (pHour + 23) * itemHeight); sv02.scrollTo(0, (pMinute + 59) * itemHeight); } }); setBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getSettingDate(); CustomerDateDialog.this.cancel(); } }); cancleBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CustomerDateDialog.this.cancel(); } }); } private OnTouchListener tListener = new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (v == sv01) { flag = 1; } else { flag = 2; } if (event.getAction() == MotionEvent.ACTION_UP) { final ScrollView sv = (ScrollView) v; lastY = sv.getScrollY(); System.out.println("lastY" + lastY); handler.sendMessageDelayed(handler.obtainMessage(0, v), 50); } return false; } }; private Handler handler = new Handler() { @SuppressLint("HandlerLeak") public void handleMessage(android.os.Message msg) { ScrollView sv = (ScrollView) msg.obj; if (msg.what == 0) { if (lastY == sv.getScrollY()) { int num = lastY / itemHeight; int over = lastY % itemHeight; if (over > itemHeight / 2) {// 超过一半滚到下一格 locationTo((num + 1) * itemHeight, sv, flag); } else {// 不到一半滚回上一格 locationTo(num * itemHeight, sv, flag); } } else { lastY = sv.getScrollY(); handler.sendMessageDelayed(handler.obtainMessage(0, sv), 50);// 滚动还没停止隔50毫秒再判断 } } }; }; private void locationTo(int position, ScrollView scrollview, int flag) { switch (flag) { case 1: int mPosition = 0; if (position <= 23 * itemHeight) { mPosition = position + 24 * itemHeight; scrollview.scrollTo(0, mPosition); } else if (position >= 48 * itemHeight) { mPosition = position - 24 * itemHeight; scrollview.scrollTo(0, mPosition); } else { mPosition = position; scrollview.smoothScrollTo(0, position); } setHour = (mPosition / itemHeight - 23) % 24; break; case 2: int hPosition = 0; if (position <= 57 * itemHeight) { hPosition = position + 60 * itemHeight; scrollview.scrollTo(0, hPosition); } else if (position >= 120 * itemHeight) { hPosition = position - 60 * itemHeight; scrollview.scrollTo(0, hPosition); } else { hPosition = position; scrollview.smoothScrollTo(0, position); } setMinute = (hPosition / itemHeight) % 60 + 1; break; } } /** * 设置分刻度盘 * * @param tv */ private void setMinuteDial(TextView tv) { StringBuffer buff = new StringBuffer(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 60; j++) { if (j <= 9) { buff.append("0" + j); } else { buff.append(j + ""); } } } tv.setText(buff); } /** * 设置时刻度盘 * * @param tv */ private void setHourDial(TextView tv) { StringBuffer buff = new StringBuffer(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 24; j++) { if (j <= 9) { buff.append("0" + j); } else { buff.append(j + ""); } } } tv.setText(buff); } public void setpHour(int pHour) { this.pHour = pHour; } public void setpMinute(int pMinute) { this.pMinute = pMinute; } public void setOnDateDialogListener(DateDialogListener listener) { this.listener = listener; } public interface DateDialogListener { void getDate(); } public void getSettingDate() { if (listener != null) { listener.getDate(); } } public int getSettingHour() { return setHour; } public int getSettingMinute() { return setMinute; } }
这里光有java代码还不够,还有自定义Dialog的布局文件,time_wheel.xml代码如下:
为了让自定义的Dialog的样式更好看,这里还需要自定义样式的Style,Style的代码如下;
感谢你能够认真阅读完这篇文章,希望小编分享的“Android如何实现模仿正点闹钟时间齿轮滑动效果”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!