垂直的seekbar

看下效果:

垂直的seekbar  

  1 package org.qianyukun.core.views;
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
{
Drawable mThumb;
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(0, -getWidth());
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);
// System.out.println(event.getY());
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)
{
System.out.println("mScaledTouchSlop "+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 = 1.0f;
}
else if (y < top)
{
scale = 0.0f;
}
else
{
scale = (float) (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); } @Override
public void setThumb(Drawable thumb) {
super.setThumb(thumb);
mThumb = thumb;
}
public Drawable getSeekBarThumb() {
return mThumb;
} }

最近在做PDF文档的浏览,文档右侧有一个垂直的透明的滑动块,来快速定位文档位置和显示当前页面的位置。

由于原生的seekbar是水平的,所以需要可自定义View了。虽然View的自定义这块,了解的不够深,但是够用了。

实现方式:继承SeekBar,在onDrow()中将画布旋转,然后平移到能够显示的位置。添加滑动事件。

 mPageSlider = new VerticalSeekBar(context);
mPageSlider.setThumb(getResources().getDrawable(
R.drawable.pdfseekbarthum));//添加thumb样式
mPageSlider.setProgressDrawable(getResources().getDrawable(
R.drawable.seek_progress));//添加进度条样式
mPageSlider.setBackgroundResource(R.color.toolbar);
mPageSlider.setThumbOffset(0);//这句比较重要,使thumb显示完整

记录每一行代码,滴水穿石!

上一篇:Android使用MVP时应该注意的问题


下一篇:迷你MVVM框架 avalonjs 入门教程