Android自定义垂直拖动seekbar进度条

Android自定义垂直拖动seekbar进度条,第1张

概述Android自带的SeekBar是水平的,要垂直的,必须自己写一个类,继承SeekBar。一个简单的垂直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);        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,"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,defStyle);    mScaledtouchSlop = VIEwConfiguration.get(context).getScaledtouchSlop();  }  public VerticalSeekbar(Context context,int oldh)  {    super.onSizeChanged(h,olDW);  }  @OverrIDe  protected synchronized voID onMeasure(int wIDthMeasureSpec,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(),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(),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(),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(),0);  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android自定义垂直拖动seekbar进度条全部内容,希望文章能够帮你解决Android自定义垂直拖动seekbar进度条所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1143902.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存