Android实现滑动刻度尺效果,选择身高体重和生日
刻度尺效果虽然看起来很美,我个人认为很不实用,即使再不实用,也有用的,鉴于群里成员对我的苦苦哀求,我就分享一个他用不到的,横屏滑动刻度尺,因为他需要竖屏的,哈哈……
最近群里的开发人员咨询怎样实现刻度尺的滑动效果去选择身高体重等信息。我倒是做过这种效果,貌似群里要的那个开发者要竖着的刻度尺,那我就先分享个横着的刻度尺滑动选择效果。哈哈……我就是这么贱,贱贱的才惹人爱嘛!好了,不逗了,先给个横着的效果,自己试着去改编或者修改一下,看看通过自己的能力能不能做出竖着的效果来,过两天我再把竖着的那个滑动选择效果分享出来。废话不多说了,上代码。
效果图:

第一步:activity_mian.xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/tab_blue"
android:gravity="center_vertical"
android:orientation="vertical"
tools:context=".MainActivity" > <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp" > <LinearLayout
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="出生年"
android:textColor="@color/white"
android:textSize="16sp" /> <TextView
android:id="@+id/user_birth_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:text="1972"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout> <HorizontalScrollView
android:id="@+id/birthruler"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/two"
android:background="@drawable/birthday_ruler"
android:scrollbars="none" > <LinearLayout
android:id="@+id/ruler_layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout> </LinearLayout>
第二步:水平空白刻度布局,blankhrulerunit.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="20dp"
android:background="@null"
android:orientation="vertical" > <TextView
android:id="@+id/hrulerunit"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@null"
android:textColor="@color/white"
android:textSize="14sp" /> </RelativeLayout>
第三步:中间刻度尺布局,hrulerunit.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="20dp"
android:orientation="vertical" > <ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="25dp"
android:layout_marginTop="3dp"
android:background="@null"
android:contentDescription="@null"
android:scaleType="fitXY"
android:src="@drawable/rulerscale_horizontal" /> <TextView
android:id="@+id/hrulerunit"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@null"
android:textColor="@color/white"
android:textSize="14sp" /> </RelativeLayout>
第四步:MainActivity.java主代码实现:
package net.loonggg.rulerdemo; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; import android.app.Activity;
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.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView; public class MainActivity extends Activity {
private HorizontalScrollView ruler;
private LinearLayout rulerlayout, all_layout;
private TextView user_birth_value;
private int beginYear; private String birthyear = "1970";
private long time = 0;
private int screenWidth;
private boolean isFirst = true; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_birth_value = (TextView) findViewById(R.id.user_birth_value);
user_birth_value.setText("1970");
ruler = (HorizontalScrollView) findViewById(R.id.birthruler);
rulerlayout = (LinearLayout) findViewById(R.id.ruler_layout);
ruler.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
user_birth_value.setText(String.valueOf(beginYear
+ (int) Math.ceil((ruler.getScrollX()) / 20)));
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
user_birth_value.setText(String.valueOf(beginYear
+ (int) Math.ceil((ruler.getScrollX()) / 20)));
birthyear = String.valueOf((int) (beginYear + Math
.ceil((ruler.getScrollX()) / 20)));
try {
time = (new SimpleDateFormat("yyyy")
.parse(String.valueOf(birthyear)))
.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
}
}, 1000);
break;
}
return false;
} });
} @Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (isFirst) {
screenWidth = ruler.getWidth();
constructRuler();
isFirst = false;
}
} @Override
protected void onResume() {
super.onResume();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
scroll();
}
}, 100);
} private void scroll() {
ruler.smoothScrollTo((1970 - beginYear) * 20, 0);
} @SuppressWarnings("deprecation")
private void constructRuler() {
int year = new Date().getYear();
if (year < 2015)
year = 2010;
beginYear = year / 10 * 10 - 150;
View leftview = (View) LayoutInflater.from(this).inflate(
R.layout.blankhrulerunit, null);
leftview.setLayoutParams(new LayoutParams(screenWidth / 2,
LayoutParams.MATCH_PARENT));
rulerlayout.addView(leftview);
for (int i = 0; i < 16; i++) {
View view = (View) LayoutInflater.from(this).inflate(
R.layout.hrulerunit, null);
view.setLayoutParams(new LayoutParams(200,
LayoutParams.MATCH_PARENT));
TextView tv = (TextView) view.findViewById(R.id.hrulerunit);
tv.setText(String.valueOf(beginYear + i * 10));
rulerlayout.addView(view);
}
View rightview = (View) LayoutInflater.from(this).inflate(
R.layout.blankhrulerunit, null);
rightview.setLayoutParams(new LayoutParams(screenWidth / 2,
LayoutParams.MATCH_PARENT));
rulerlayout.addView(rightview);
} }
索要源码的方式很简单,跟以前一样,在公众号“非著名程序员”里回复关键字“刻度尺”即可获得。欢迎大家关注,转发和分享。
Android实现滑动刻度尺效果,选择身高体重和生日的更多相关文章
- 【Android UI】案例03滑动切换效果的实现(ViewPager)
本例使用ViewPager实现滑动切换的效果.本例涉及的ViewPager.为android.support.v4.view.ViewPager.所以须要在android项目中导入android-su ...
- Android实现下拉导航选择菜单效果
本文介绍在Android中如何实现下拉导航选择菜单效果. 关于下拉导航选择菜单效果在新闻客户端中用的比较多,当然也可以用在其他的项目中,这样可以很方便的选择更多的菜单.我们可以让我们的应用顶部有左 ...
- Android UI效果实现——Activity滑动退出效果
更新说明: 1.在QQ网友北京-旭的提醒下,在SlideFrame的initilize方法中添加了focusable.focusableInTouch.clickable的状态设置,否则会导致部分情况 ...
- 【转】Android 实现ListView的滑动删除效果
http://www.cnblogs.com/weixiao870428/p/3524055.html http://download.csdn.net/download/love_javc_you/ ...
- Android实现左右滑动指引效果
本文介绍Android中实现左右滑动的指引效果. 关于左右滑动效果,我在以前的一篇博文中提到过,有兴趣的朋友可以查看:http://www.cnblogs.com/hanyonglu/archive/ ...
- [Android] Android 类似今日头条顶部的TabLayout 滑动标签栏 效果
APP市场中大多数新闻App都有导航菜单,导航菜单是一组标签的集合,在新闻客户端中,每个标签标示一个新闻类别,对应下面ViewPager控件的一个分页面,今日头条, 网易新闻等. 本文主要讲的是用:T ...
- Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9671609 记得在很早之前,我写了一篇关于Android滑动菜单的文章,其中有一个 ...
- 【转】(转)【Android】Paint的效果研究
转自:http://wpf814533631.iteye.com/blog/1847661 (转)[Android]Paint的效果研究 博客分类: android 在Paint中有很多的属性可以 ...
- Android仿IOS回弹效果 ScrollView回弹 总结
Android仿IOS回弹效果 ScrollView回弹 总结 应项目中的需求 须要仿IOS 下拉回弹的效果 , 我在网上搜了非常多 大多数都是拿scrollview 改吧改吧 试了一些 发现总 ...
随机推荐
- MySQL 调优基础(二) Linux内存管理
进程的运行,必须使用内存.下图是Linux中进程中的内存的分布图: 其中最重要的 heap segment 和 stack segment.其它内存段基本是大小固定的.注意stack是向低地址增长的, ...
- android-The method findViewById(int) is undefined for the type ContactMainFragment报错
@Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view ...
- 七、Android学习第六天——SQLite与文件下载(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 七.Android学习第六天——SQLite与文件下载 SQLite SQ ...
- Bootstrap 学习(1)
简介 Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快捷. Bootst ...
- Linux vi 操作命令整理
转自:http://www.lupaworld.com/?uid-296380-action-viewspace-itemid-118973 vi/vim 基本使用方法 本文介绍了vi (vim) ...
- [转]学习Nop中Routes的使用
本文转自:http://www.cnblogs.com/miku/archive/2012/09/27/2706276.html 1. 映射路由 大型MVC项目为了扩展性,可维护性不能像一般项目在Gl ...
- 转: VMware 安装mac osx 10.11 安装步骤(一)(from伟东)
http://blog.csdn.net/soachenshui/article/details/49251513
- Samsung I9103刷cm-10.1的方法
按照官方网站的说明一步一步的做下去的时候发现在执行heimdall.exe文件的时候出现“不是win32的应用程序”的错误提示,因此决定按照其它方法安装recovery,然后再刷入CM10.1. sa ...
- HTML-学习笔记(样式)
HTML 样式 style属性用于改变HTML元素的样式. <p style="font-family: arial; color: red;">字体是arial,字体 ...
- NOIP2015 Revenge
辣鸡出题人,毁我比赛,颓我精神,耗我钱财,废我青春. 去年成绩惨不忍睹就不说了...好像是100+80+0+100+50+60. 大概列一下算法. 幻方:模拟 #include <iostrea ...