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);
	}

}

索要源码的方式很简单,跟以前一样,在公众号“非著名程序员”里回复关键字“ 刻度尺 ”即可获得。欢迎大家关注,转发和分享。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Android应用中实现滑动刻度尺效果,可以使用SeekBar控件。SeekBar是一个滑动条控件,可以让用户通过滑动选择数值。我们可以通过自定义SeekBar的样式来实现滑动刻度尺效果,以便用户可以方便地选择身高和体重。 以下是一个实现滑动刻度尺效果的示例代码: 1. 首先,在layout文件中添加SeekBar控件,例如: ``` <SeekBar android:id="@+id/height_seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:max="220" android:progress="170" android:progressDrawable="@drawable/custom_seekbar" android:thumb="@drawable/custom_thumb" /> ``` 在这个示例中,我们创建了一个id为height_seekbar的SeekBar控件,并设置了它的最大值和初始值。我们还指定了SeekBar的样式,包括进度条和拇指图标,这些样式可以通过自定义drawable来实现。 2. 在drawable文件夹中创建custom_seekbar.xml文件,定义SeekBar的进度条样式,例如: ``` <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape android:shape="rectangle"> <corners android:radius="8dp" /> <solid android:color="#EFEFEF" /> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape android:shape="rectangle"> <corners android:radius="8dp" /> <solid android:color="#FF4081" /> </shape> </clip> </item> </layer-list> ``` 在这个示例中,我们创建了一个layer-list来定义SeekBar的进度条样式。进度条由两个矩形组成,一个代表背景,另一个代表进度。我们通过设置shape来定义矩形的圆角和颜色。 3. 在drawable文件夹中创建custom_thumb.xml文件,定义SeekBar的拇指图标样式,例如: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="30dp" android:height="30dp" /> <solid android:color="#FF4081" /> </shape> ``` 在这个示例中,我们创建了一个圆形的拇指图标,设置了它的大小和颜色。 4. 在Java代码中添加SeekBar的监听器,例如: ``` SeekBar heightSeekBar = findViewById(R.id.height_seekbar); heightSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // 在这里处理身高值的变化 } @Override public void onStartTrackingTouch(SeekBar seekBar) { // 在这里处理开始滑动SeekBar的事件 } @Override public void onStopTrackingTouch(SeekBar seekBar) { // 在这里处理停止滑动SeekBar的事件 } }); ``` 在这个示例中,我们获取了SeekBar控件的实例,并设置了它的监听器。当用户拖动SeekBar时,会回调onProgressChanged方法,并传递新的进度值。我们可以在这个方法中处理身高值的变化。在onStartTrackingTouch和onStopTrackingTouch方法中,我们可以处理开始和停止滑动SeekBar的事件。 希望这个示例能够对你有帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值