最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義垂直拖動seekbar進度條

 更新時間:2017年11月02日 14:35:12   作者:圣騎士wind  
這篇文章主要為大家詳細介紹了Android自定義垂直拖動seekbar進度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Android自帶的SeekBar是水平的,要垂直的,必須自己寫一個類,繼承SeekBar。

一個簡單的垂直SeekBar的例子:

(但是它其實是存在一些問題的。不過要是滿足基本需要還是可以湊合的)

package com.example.helloverticalseekbar;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;

public class VerticalSeekBar extends SeekBar
{

 public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle)
 {
  super(context, attrs, defStyle);
 }

 public VerticalSeekBar(Context context, AttributeSet attrs)
 {
  super(context, attrs);
 }

 public VerticalSeekBar(Context context)
 {
  super(context);
 }

 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh)
 {
  super.onSizeChanged(h, w, oldh, oldw);
 }

 @Override
 protected synchronized void onMeasure(int widthMeasureSpec,
   int heightMeasureSpec)
 {
  super.onMeasure(heightMeasureSpec, widthMeasureSpec);
  setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
 }

 @Override
 protected synchronized void onDraw(Canvas canvas)
 {
  canvas.rotate(-90);
  canvas.translate(-getHeight(), 0);
  super.onDraw(canvas);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event)
 {
  if (!isEnabled())
  {
   return false;
  }

  switch (event.getAction())
  {
   case MotionEvent.ACTION_DOWN:
   case MotionEvent.ACTION_MOVE:
   case MotionEvent.ACTION_UP:
    setProgress(getMax()
      - (int) (getMax() * event.getY() / getHeight()));
    onSizeChanged(getWidth(), getHeight(), 0, 0);
    break;

   case MotionEvent.ACTION_CANCEL:
    break;
  }

  return true;
 }

}

Demo中加上一個水平SeekBar作為對比,代碼如下:

Activity:

HelloSeekBarActivity

package com.example.helloverticalseekbar;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class HelloSeekBarActivity extends Activity
{
 private SeekBar horiSeekBar = null; 
 private TextView horiText = null;
 
 private VerticalSeekBar verticalSeekBar = null;
 private TextView verticalText = null;

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  Log.d(AppConstants.LOG_TAG, "onCreate");
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_hello_seek_bar);
  
  horiSeekBar = (SeekBar) findViewById(R.id.horiSeekBar);
  horiText = (TextView)findViewById(R.id.horiText);  
  horiSeekBar.setOnSeekBarChangeListener(horiSeekBarListener);
  
  verticalSeekBar = (VerticalSeekBar)findViewById(R.id.verticalSeekBar);
  verticalText = (TextView)findViewById(R.id.verticalText);
  verticalSeekBar.setOnSeekBarChangeListener(verticalSeekBarChangeListener);
 
  
  
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu)
 {
  getMenuInflater().inflate(R.menu.hello_seek_bar, menu);
  return true;
 }
 
 
 private OnSeekBarChangeListener horiSeekBarListener = new OnSeekBarChangeListener()
 {
  
  @Override
  public void onStopTrackingTouch(SeekBar seekBar)
  {
   
  }
  
  @Override
  public void onStartTrackingTouch(SeekBar seekBar)
  {
   
  }
  
  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser)
  {
   Log.d(AppConstants.LOG_TAG, "Horizontal SeekBar --> onProgressChanged");
   horiText.setText(Integer.toString(progress));
   
  }
 };
 
 private OnSeekBarChangeListener verticalSeekBarChangeListener = new OnSeekBarChangeListener()
 {
  
  @Override
  public void onStopTrackingTouch(SeekBar seekBar)
  {
   
  }
  
  @Override
  public void onStartTrackingTouch(SeekBar seekBar)
  {
   
  }
  
  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser)
  {
   Log.d(AppConstants.LOG_TAG, "Vertical SeekBar --> onProgressChanged");
   verticalText.setText(Integer.toString(progress));
   
  }
 };

}

布局:

activity_hello_seek_bar.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".HelloSeekBarActivity" >

 <TextView
  android:id="@+id/myTextView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:text="@string/hello_world" />

 <SeekBar
  android:id="@+id/horiSeekBar"
  android:layout_width="match_parent"
  android:layout_height="20dp"
  android:layout_below="@id/myTextView" />

 <TextView
  android:id="@+id/horiText"
  android:layout_width="wrap_content"
  android:layout_height="20dp"
  android:layout_below="@id/horiSeekBar"
  android:text="horizontal" />

 <com.example.helloverticalseekbar.VerticalSeekBar
  android:id="@+id/verticalSeekBar"
  android:layout_width="wrap_content"
  android:layout_height="200dp"
  android:layout_below="@id/horiText" />

 <TextView
  android:id="@+id/verticalText"
  android:layout_width="wrap_content"
  android:layout_height="20dp"
  android:layout_below="@id/verticalSeekBar"
  android:text="vertical" />

</RelativeLayout>

運行截圖:

一個改進版的SeekBar

package com.example.helloverticalseekbarv2;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.SeekBar;

public class VerticalSeekBar extends SeekBar
{
 private boolean mIsDragging;
 private float mTouchDownY;
 private int mScaledTouchSlop;
 private boolean isInScrollingContainer = false;

 public boolean isInScrollingContainer()
 {
  return isInScrollingContainer;
 }

 public void setInScrollingContainer(boolean isInScrollingContainer)
 {
  this.isInScrollingContainer = isInScrollingContainer;
 }

 /**
  * On touch, this offset plus the scaled value from the position of the
  * touch will form the progress value. Usually 0.
  */
 float mTouchProgressOffset;

 public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle)
 {
  super(context, attrs, defStyle);
  mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

 }

 public VerticalSeekBar(Context context, AttributeSet attrs)
 {
  super(context, attrs);
 }

 public VerticalSeekBar(Context context)
 {
  super(context);
 }

 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh)
 {

  super.onSizeChanged(h, w, oldh, oldw);

 }

 @Override
 protected synchronized void onMeasure(int widthMeasureSpec,
   int heightMeasureSpec)
 {
  super.onMeasure(heightMeasureSpec, widthMeasureSpec);
  setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
 }

 @Override
 protected synchronized void onDraw(Canvas canvas)
 {
  canvas.rotate(-90);
  canvas.translate(-getHeight(), 0);
  super.onDraw(canvas);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event)
 {
  if (!isEnabled())
  {
   return false;
  }

  switch (event.getAction())
  {
   case MotionEvent.ACTION_DOWN:
    if (isInScrollingContainer())
    {

     mTouchDownY = event.getY();
    }
    else
    {
     setPressed(true);

     invalidate();
     onStartTrackingTouch();
     trackTouchEvent(event);
     attemptClaimDrag();

     onSizeChanged(getWidth(), getHeight(), 0, 0);
    }
    break;

   case MotionEvent.ACTION_MOVE:
    if (mIsDragging)
    {
     trackTouchEvent(event);

    }
    else
    {
     final float y = event.getY();
     if (Math.abs(y - mTouchDownY) > mScaledTouchSlop)
     {
      setPressed(true);

      invalidate();
      onStartTrackingTouch();
      trackTouchEvent(event);
      attemptClaimDrag();

     }
    }
    onSizeChanged(getWidth(), getHeight(), 0, 0);
    break;

   case MotionEvent.ACTION_UP:
    if (mIsDragging)
    {
     trackTouchEvent(event);
     onStopTrackingTouch();
     setPressed(false);

    }
    else
    {
     // Touch up when we never crossed the touch slop threshold
     // should
     // be interpreted as a tap-seek to that location.
     onStartTrackingTouch();
     trackTouchEvent(event);
     onStopTrackingTouch();

    }
    onSizeChanged(getWidth(), getHeight(), 0, 0);
    // ProgressBar doesn't know to repaint the thumb drawable
    // in its inactive state when the touch stops (because the
    // value has not apparently changed)
    invalidate();
    break;
  }
  return true;

 }

 private void trackTouchEvent(MotionEvent event)
 {
  final int height = getHeight();
  final int top = getPaddingTop();
  final int bottom = getPaddingBottom();
  final int available = height - top - bottom;

  int y = (int) event.getY();

  float scale;
  float progress = 0;

  // 下面是最小值
  if (y > height - bottom)
  {
   scale = 0.0f;
  }
  else if (y < top)
  {
   scale = 1.0f;
  }
  else
  {
   scale = (float) (available - y + top) / (float) available;
   progress = mTouchProgressOffset;
  }

  final int max = getMax();
  progress += scale * max;

  setProgress((int) progress);

 }

 /**
  * This is called when the user has started touching this widget.
  */
 void onStartTrackingTouch()
 {
  mIsDragging = true;
 }

 /**
  * This is called when the user either releases his touch or the touch is
  * canceled.
  */
 void onStopTrackingTouch()
 {
  mIsDragging = false;
 }

 private void attemptClaimDrag()
 {
  ViewParent p = getParent();
  if (p != null)
  {
   p.requestDisallowInterceptTouchEvent(true);
  }
 }

 @Override
 public synchronized void setProgress(int progress)
 {

  super.setProgress(progress);
  onSizeChanged(getWidth(), getHeight(), 0, 0);

 }

}

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

相關文章

  • android壓力測試命令monkey詳解

    android壓力測試命令monkey詳解

    這篇文章主要介紹了android monkey命令詳解,Monkey 就是SDK中附帶的一個工具,該工具主要用于進行壓力測試,需要的朋友可以參考下
    2014-03-03
  • Android 中 退出多個activity的經(jīng)典方法

    Android 中 退出多個activity的經(jīng)典方法

    這篇文章主要介紹了Android 中 退出多個activity的經(jīng)典方法 的相關資料,本文給大家分享兩種方法,在這小編給大家推薦使用第一種方法,對此文感興趣的朋友可以參考下
    2016-09-09
  • android webview 中l(wèi)ocalStorage無效的解決方法

    android webview 中l(wèi)ocalStorage無效的解決方法

    這篇文章主要介紹了android webview 中l(wèi)ocalStorage無效的解決方法,本文直接給出解決方法實現(xiàn)代碼,需要的朋友可以參考下
    2015-06-06
  • android,不顯示標題的方法小例子

    android,不顯示標題的方法小例子

    android,不顯示標題的方法小例子,需要的朋友可以參考一下
    2013-05-05
  • Android編程之客戶端通過socket與服務器通信的方法

    Android編程之客戶端通過socket與服務器通信的方法

    這篇文章主要介紹了Android編程之客戶端通過socket與服務器通信的方法,結合實例形式分析了Android基于socket通訊的具體步驟與相關使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Android應用關閉的情況以及識別方法詳解

    Android應用關閉的情況以及識別方法詳解

    對于現(xiàn)在的安卓手機而言,很多功能都是在逐步完善的,這篇文章主要給大家介紹了關于Android應用關閉的情況以及識別的相關資料,文章通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-06-06
  • Android圖像處理之霓虹濾鏡效果

    Android圖像處理之霓虹濾鏡效果

    這篇文章主要介紹了Android圖像處理之霓虹濾鏡效果的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 詳解Android使用@hide的API的方法

    詳解Android使用@hide的API的方法

    這篇文章主要介紹了詳解Android使用@hide的API的方法的相關資料,希望通過本文大家能理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • Android 通過TCP協(xié)議上傳指定目錄文件的方法

    Android 通過TCP協(xié)議上傳指定目錄文件的方法

    這篇文章主要介紹了Android 通過TCP協(xié)議上傳指定目錄文件的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Android讀取串口數(shù)據(jù)的操作指南

    Android讀取串口數(shù)據(jù)的操作指南

    在Android系統(tǒng)上讀取串口數(shù)據(jù)是一個常見的需求,特別是當我們需要與硬件設備進行通信時,本文給大家介紹了Android讀取串口數(shù)據(jù)的操作指南,文中有詳細的步驟和代碼示例,幫助你更好地理解和實現(xiàn)串口通信,需要的朋友可以參考下
    2024-05-05

最新評論

成都市| 桦川县| 西乌珠穆沁旗| 东丰县| 景谷| 桐城市| 新安县| 互助| 油尖旺区| 漯河市| 灯塔市| 丹巴县| 塘沽区| 绵阳市| 安化县| 扎兰屯市| 柳林县| 淮南市| 长乐市| 突泉县| 新津县| 营山县| 芦溪县| 南投县| 平湖市| 杂多县| 同江市| 健康| 富锦市| 屯昌县| 淮北市| 久治县| 寿光市| 龙游县| 新巴尔虎左旗| 晋江市| 孟津县| 河间市| 瓮安县| 盱眙县| 长汀县|