Android实现滑动刻度尺效果

所属分类: 软件编程 / Android 阅读数: 95
收藏 0 赞 0 分享

最近群里的开发人员咨询怎样实现刻度尺的滑动效果去选择身高体重等信息。给个横着的效果,自己试着去改编或者修改一下,看看通过自己的能力能不能做出竖着的效果来,过两天我再把竖着的那个滑动选择效果分享出来。废话不多说了,上代码。

效果图如下:

第一步: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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多