欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)滑動刻度尺效果

 更新時間:2020年06月19日 09:36:23   作者:weixin_33690367  
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)滑動刻度尺效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近群里的開發(fā)人員咨詢怎樣實現(xiàn)刻度尺的滑動效果去選擇身高體重等信息。給個橫著的效果,自己試著去改編或者修改一下,看看通過自己的能力能不能做出豎著的效果來,過兩天我再把豎著的那個滑動選擇效果分享出來。廢話不多說了,上代碼。

效果圖如下:

第一步: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主代碼實現(xiàn):

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android自定義wheelview隨機(jī)選號效果

    Android自定義wheelview隨機(jī)選號效果

    這篇文章主要介紹了Android自定義wheelview隨機(jī)選號效果,利用wheelview實現(xiàn)滾動隨機(jī)選擇號碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Kotlin比較與解釋Lazy與Lateinit的用法

    Kotlin比較與解釋Lazy與Lateinit的用法

    在使用kotlin開發(fā)中,因為各種原因,我們會經(jīng)常需要使用到延遲加載的功能,目前kotlin的延遲加載主要有兩種:lateinit和lazy,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值
    2023-02-02
  • Android使用GridView實現(xiàn)日歷的簡單功能

    Android使用GridView實現(xiàn)日歷的簡單功能

    這篇文章主要為大家詳細(xì)介紹了Android使用GridView實現(xiàn)日歷的簡單功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android實現(xiàn)下拉展示條目效果

    Android實現(xiàn)下拉展示條目效果

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)下拉展示條目效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • 詳談OnTouchListener與OnGestureListener的區(qū)別

    詳談OnTouchListener與OnGestureListener的區(qū)別

    下面小編就為大家?guī)硪黄斦凮nTouchListener與OnGestureListener的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android中Glide實現(xiàn)超簡單的圖片下載功能

    Android中Glide實現(xiàn)超簡單的圖片下載功能

    本篇文章主要介紹了Android中Glide實現(xiàn)超簡單的圖片下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Android編程實現(xiàn)鬧鐘的方法詳解

    Android編程實現(xiàn)鬧鐘的方法詳解

    這篇文章主要介紹了Android編程實現(xiàn)鬧鐘的方法,結(jié)合實例形式較為詳細(xì)的分析了Android鬧鐘的原理、布局、權(quán)限控制及相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2017-02-02
  • Android ListView與RecycleView的對比使用解析

    Android ListView與RecycleView的對比使用解析

    這篇文章主要介紹了Android ListView與RecycleView的對比使用解析,需要的朋友可以參考下
    2017-12-12
  • Jetpack Compose實現(xiàn)列表和動畫效果詳解

    Jetpack Compose實現(xiàn)列表和動畫效果詳解

    這篇文章主要為大家詳細(xì)講講Jetpack Compose實現(xiàn)列表和動畫效果的方法步驟,文中的代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-06-06
  • Android webview與js交換JSON對象數(shù)據(jù)示例

    Android webview與js交換JSON對象數(shù)據(jù)示例

    js主動調(diào)用android的對象方式,android也無法返回給js一個jsonobject,需要js做一下轉(zhuǎn)換,具體代碼如下,感興趣的朋友可以參考下哈
    2013-06-06

最新評論