我试图让垂直搜索栏与模拟器去,但我 那种卡住。我可以得到搜索栏以显示我想要的方式, 我能得到的进展做我想做的,我可以修改 的onTouchEvent让拇指去,而不是垂直的 水平。我不能做的就是拇指向外移动 默认29水平像素,而不使用setThumbOffset()。这 本身是没有问题的。问题是从这一事实到来,我 不了解thumbOffset在所有 - 我猜。我想我可以(正确地)调整 窗口小部件,而我是pretty的肯定我没有做的事情。或者,也许我可以 只需使用thumbOffset如果我能找到答案。因为我可以 正确计算的进展我想我只想用线性 窗口小部件,但那个 - 进步*功能(getBottom()共达()) 似乎并没有做到这一点。但是,我想不出有什么偏移 围绕着。
作为一个有点一边,我真的不能确定,如果我做什么 onSizeChanged()是明智的,或者如果它是怎么回事后咬我的屁股 上。
下面是main.xml中的布局:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< com.mobilsemantic.mobipoll.SlideBar
机器人:ID =@ + ID /幻灯片
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:最大=100
机器人:进度=0
机器人:secondaryProgress =25/>
<按钮机器人:ID =@ + ID /按钮
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:文本=你好,我是一个按钮/>
< TextView的机器人:ID =@ + ID /跟踪
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
和类(忽略调试垃圾):
进口android.content.Context;
进口android.graphics.Canvas;
进口android.util.AttributeSet;
进口android.util.Log;
进口android.view.MotionEvent;
进口android.view.View;
进口android.widget.SeekBar;
公共类滑杆扩展搜索栏{
私人诠释oHeight = 320,oWidth = 29;
私人诠释oProgress = -1,oOffset = -1 ;;
私人浮子XPOS = -1,yPos = -1;
私人诠释顶级= -1,底部= -1,左= -1,右= -1;
公共滑杆(上下文的背景下){
超(上下文);
}
公共滑杆(上下文的背景下,ATTRS的AttributeSet)
{
超(背景下,ATTRS);
oOffset = this.getThumbOffset();
oProgress = this.getProgress();
}
公共滑杆(上下文的背景下,ATTRS的AttributeSet,INT defStyle)
{
超(背景下,ATTRS,defStyle);
}
保护同步无效onMeasure(INT widthMeasureSpec,intheightMeasureSpec)
{
INT高= View.MeasureSpec.getSize(heightMeasureSpec);
oHeight =高度;
this.setMeasuredDimension(oWidth,oHeight);
}
保护无效onSizeChanged(INT W,INT小时,INT oldw,INT oldh)
{
super.onSizeChanged(H,W,oldw,oldh);
}
保护无效onLayout(布尔改变,诠释L,INT T,INT R,int b)在
{
super.onLayout(改变,L,T,R,B);
左=升;
右= R;
顶部= T;
底部= B;
}
保护无效的OnDraw(帆布C)
{
c.rotate(90);
c.translate(0,-29);
super.onDraw(C);
}
公共布尔的onTouchEvent(MotionEvent事件)
{
XPOS = event.getX();
yPos = event.getY();
浮动进展=(yPos-this.getTop())/(this.getBottom() - this.getTop());
oOffset = this.getThumbOffset();
oProgress = this.getProgress();
Log.d(偏置+ System.nanoTime(),新整型(oOffset)的ToString());
Log.d(进步+ System.nanoTime(),新的整数(oProgress)的ToString());
浮动补偿;
偏移量=进展*(this.getBottom() - this.getTop());
this.setThumbOffset((INT)偏移量);
Log.d(offset_postsetprogress+ System.nanoTime(),新的整数(oOffset)的ToString());
Log.d(progress_postsetprogress+ System.nanoTime(),新的整数(oProgress)的ToString());
this.setProgress((int)的(100 * event.getY()/ this.getBottom()));
返回true;
}
}
解决方案
有关 API 11及更高版本
,可以使用搜索栏的XML
属性(机器人:旋转=270)。
垂直效果
<搜索栏
机器人:ID =@ + ID / seekBar1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:旋转=270/>
对于较老的API级别(如API10),用途: https://github.com/AndroSelva /垂直,搜索栏,Android的或看到这个样本这里
I'm trying to get a vertical seekbar going with the emulator, but I'm sort of stuck. I can get the seekbar to display the way I want it to, and I can get the progress to do what I want, and I can modify the onTouchEvent to get the thumb to go vertically instead of horizontally. What I can't do is get the thumb to move outside of the default 29 horizontal pixels without using setThumbOffset(). This in itself isn't a problem. The problem is coming from the fact that I don't understand the thumbOffset at all -- I guess. I think I could (properly) resize the widget, which I am pretty sure I'm not doing right. Or maybe I could just use the thumbOffset if I could figure it out. Since I can calculate the progress correctly I thought I would just use a linear function of progress * (getTop() - getBottom()) of the widget but that doesn't seem to do it. But I can't figure out what the offset is centered around.
As a somewhat aside, I am really unsure if what I am doing in onSizeChanged() is sane or if it's going to bite me in the ass later on.
Here's the main.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.mobilsemantic.mobipoll.SlideBar
android:id="@+id/slide"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:max="100"
android:progress="0"
android:secondaryProgress="25" />
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello, I am a Button" />
<TextView android:id="@+id/tracking"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And the class (ignore the debugging junk):
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SeekBar;
public class SlideBar extends SeekBar {
private int oHeight = 320, oWidth = 29;
private int oProgress = -1, oOffset = -1;;
private float xPos = -1, yPos = -1;
private int top = -1, bottom = -1, left = -1, right = -1;
public SlideBar(Context context) {
super(context);
}
public SlideBar(Context context, AttributeSet attrs)
{
super(context, attrs);
oOffset = this.getThumbOffset();
oProgress = this.getProgress();
}
public SlideBar(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
protected synchronized void onMeasure(int widthMeasureSpec, intheightMeasureSpec)
{
int height = View.MeasureSpec.getSize(heightMeasureSpec);
oHeight = height;
this.setMeasuredDimension(oWidth, oHeight);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(h, w, oldw, oldh);
}
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
super.onLayout(changed, l, t, r, b);
left = l;
right = r;
top = t;
bottom = b;
}
protected void onDraw(Canvas c)
{
c.rotate(90);
c.translate(0,-29);
super.onDraw(c);
}
public boolean onTouchEvent(MotionEvent event)
{
xPos = event.getX();
yPos = event.getY();
float progress = (yPos-this.getTop())/(this.getBottom()-this.getTop());
oOffset = this.getThumbOffset();
oProgress = this.getProgress();
Log.d("offset" + System.nanoTime(), new Integer(oOffset).toString());
Log.d("progress" + System.nanoTime(), new Integer(oProgress).toString());
float offset;
offset = progress * (this.getBottom()-this.getTop());
this.setThumbOffset((int)offset);
Log.d("offset_postsetprogress" + System.nanoTime(), new Integer(oOffset).toString());
Log.d("progress_postsetprogress" + System.nanoTime(), new Integer(oProgress).toString());
this.setProgress((int)(100*event.getY()/this.getBottom()));
return true;
}
}
解决方案
For API 11 and later
, can use seekbar's XML
attributes(android:rotation="270")
for vertical effect.
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rotation="270"/>
For older API level (ex API10),use: https://github.com/AndroSelva/Vertical-SeekBar-Android or see this sample here