刻度尺效果虽然看起来很美,我个人认为很不实用,即使再不实用,也有用的,鉴于群里成员对我的苦苦哀求,我就分享一个他用不到的,横屏滑动刻度尺,因为他需要竖屏的,哈哈……

最近群里的开发人员咨询怎样实现刻度尺的滑动效果去选择身高体重等信息。我倒是做过这种效果,貌似群里要的那个开发者要竖着的刻度尺,那我就先分享个横着的刻度尺滑动选择效果。哈哈……我就是这么贱,贱贱的才惹人爱嘛!好了,不逗了,先给个横着的效果,自己试着去改编或者修改一下,看看通过自己的能力能不能做出竖着的效果来,过两天我再把竖着的那个滑动选择效果分享出来。废话不多说了,上代码。

效果图:

第一步: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实现滑动刻度尺效果,选择身高体重和生日的更多相关文章

  1. 【Android UI】案例03滑动切换效果的实现(ViewPager)

    本例使用ViewPager实现滑动切换的效果.本例涉及的ViewPager.为android.support.v4.view.ViewPager.所以须要在android项目中导入android-su ...

  2. Android实现下拉导航选择菜单效果

    本文介绍在Android中如何实现下拉导航选择菜单效果.   关于下拉导航选择菜单效果在新闻客户端中用的比较多,当然也可以用在其他的项目中,这样可以很方便的选择更多的菜单.我们可以让我们的应用顶部有左 ...

  3. Android UI效果实现——Activity滑动退出效果

    更新说明: 1.在QQ网友北京-旭的提醒下,在SlideFrame的initilize方法中添加了focusable.focusableInTouch.clickable的状态设置,否则会导致部分情况 ...

  4. 【转】Android 实现ListView的滑动删除效果

    http://www.cnblogs.com/weixiao870428/p/3524055.html http://download.csdn.net/download/love_javc_you/ ...

  5. Android实现左右滑动指引效果

    本文介绍Android中实现左右滑动的指引效果. 关于左右滑动效果,我在以前的一篇博文中提到过,有兴趣的朋友可以查看:http://www.cnblogs.com/hanyonglu/archive/ ...

  6. [Android] Android 类似今日头条顶部的TabLayout 滑动标签栏 效果

    APP市场中大多数新闻App都有导航菜单,导航菜单是一组标签的集合,在新闻客户端中,每个标签标示一个新闻类别,对应下面ViewPager控件的一个分页面,今日头条, 网易新闻等. 本文主要讲的是用:T ...

  7. Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9671609 记得在很早之前,我写了一篇关于Android滑动菜单的文章,其中有一个 ...

  8. 【转】(转)【Android】Paint的效果研究

    转自:http://wpf814533631.iteye.com/blog/1847661 (转)[Android]Paint的效果研究 博客分类: android   在Paint中有很多的属性可以 ...

  9. Android仿IOS回弹效果 ScrollView回弹 总结

    Android仿IOS回弹效果  ScrollView回弹 总结 应项目中的需求  须要仿IOS 下拉回弹的效果 , 我在网上搜了非常多 大多数都是拿scrollview 改吧改吧 试了一些  发现总 ...

随机推荐

  1. Mysql常用的一些技巧命令

    1.统计指定数据库下表的数量 mysql > use information_schema; mysql > SELECT count(TABLE_NAME) FROM informati ...

  2. C语言(函数)学习之strstr strcasestr

    C语言(函数)学习之[strstr]&[strcasestr]一.strstr函数使用[1]函数原型char*strstr(constchar*haystack,constchar*needl ...

  3. 错误“Unexpected namespace prefix "xmlns" found for tag LinearLayout”的解决方法(转)

    (转自:http://blog.csdn.net/bombzhang/article/details/12676789) 有一次升级开发工具后发现xml脚本出现错误“Unexpected namesp ...

  4. Ubuntu14.04桌面版基本配置

    最近公司需要用Ubuntu测试一些功能,安装好后绚丽的桌面环境很是吸引人,但切换到字符界面后,发现不少地方与RHEL不一样,连ssh.vim都没有,字符界面玩得不开心. 搜了不少文章,才将基本的功能配 ...

  5. C# FTP 命令无法获取ServerU目录列表问题

    第一步:  使用C# 的Ftp功能时,发现了一个很奇怪的现象,获取目录列表的命令,在SeverU上面直接返回错误,而在windows自带的FTP上则正常,经过反复试验,终于发现,原来是ServerU默 ...

  6. Xamarin Android中引用Jar包的方法

    新建一个Java Bingdings Library 将Jar包复制,或使用添加已存在的文件,到Jars文件夹中 确认属性中的“生成操作” 如果有类型转换不正确,请修改Transforms文件夹中的相 ...

  7. 06章 Struts2数据校验

    一.三种实现方式 ① 用validate()方法实现数据校验 继承ActionSupport类,该类实现了Validateable接口,该接口中定义了一个validate()方法,在自定义的Actio ...

  8. Unity游戏暂停之Update与FixedUpdate区别

    游戏暂停 示例程序 下面这段代码演示游戏暂停 using UnityEngine; using System.Collections; public class GamePauseTest : Mon ...

  9. Netty系列之Netty 服务端创建

    1. 背景 1.1. 原生NIO类库的复杂性 在开始本文之前,我先讲一件自己亲身经历的事:大约在2011年的时候,周边的两个业务团队同时进行新版本开发,他们都需要基于NIO非阻塞特性构建高性能.异步和 ...

  10. ADO.Net属性扩展

    属性扩展 大体意思:有外键关系时将代号化信息处理成原始文字 如:Info表中的民族列显示的是民族代号处理成Nation表中的民族名称 需要在Info类里面扩展一个显示nation名称的属性 using ...