我想创建一个只能在Y轴上拖动的可拖动视图。
通常情况下,我会使用DragShadow
并执行以下操作:
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN: {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
} break;
return true;
}
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
View sourceView = (View) dragEvent.getLocalState();
float sourceX = sourceView.getX();
float sourceY = sourceView.getY();
float dropX = dragEvent.getX() - (sourceView.getWidth() / 2);
float dropY = dragEvent.getY() - (sourceView.getHeight() / 2);
switch(dragEvent.getAction()) {
case DragEvent.ACTION_DRAG_EXITED : {
TranslateAnimation animation = new TranslateAnimation(dropX - sourceX, 0, dropY - sourceY, 0);
animation.setDuration(300);
sourceView.startAnimation(animation);
sourceView.setX(sourceX);
sourceView.setY(sourceY);
sourceView.setVisibility(View.VISIBLE);
} break;
case DragEvent.ACTION_DROP : {
sourceView.setX(dropX);
sourceView.setY(dropY);
sourceView.setVisibility(View.VISIBLE);
// TranslateAnimation animation = new TranslateAnimation(dropX - sourceX, 0, dropY - sourceY, 0);
// animation.setDuration(300);
// sourceView.startAnimation(animation);
// sourceView.setX(sourceX);
// sourceView.setY(sourceY);
} break;
}
}
问题在于我不希望{X轴上的View
或DragShadow
可以拖动。 X位置应始终保持不变,只允许更改Y轴。
我该怎么做?
答案 0 :(得分:0)
简单的伎俩!用户VerticalSeekBar
:
public class VerticalSeekBar extends SeekBar {
/**
*
*/
private OnSeekBarChangeListener onChangeListener = null;
/**
*
*/
private int lastProgress = 0;
/**
*
* @param context
*/
public VerticalSeekBar(Context context) {
super(context);
}
/**
*
* @param context
* @param attrs
* @param defStyle
*/
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
*
* @param context
* @param attrs
*/
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
*
*/
@Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
super.onSizeChanged(height, width, oldHeight, oldWidth);
}
/**
*
*/
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
this.setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
/**
*
*/
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
/**
*
*/
@Override
public void setOnSeekBarChangeListener(OnSeekBarChangeListener onChangeListener){
this.onChangeListener = onChangeListener;
}
/**
*
*/
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.onChangeListener.onStartTrackingTouch(this);
this.setPressed(true);
this.setSelected(true);
break;
case MotionEvent.ACTION_MOVE:
super.onTouchEvent(event);
int progress = getMax() - (int) (getMax() * event.getY() / getHeight());
if(progress < 0) { progress = 0; }
if(progress > getMax()) { progress = getMax(); }
this.setProgress(progress);
if(progress != this.lastProgress) {
this.lastProgress = progress;
this.onChangeListener.onProgressChanged(this, progress, true);
}
this.onSizeChanged(getWidth(), getHeight() , 0, 0);
this.setPressed(true);
this.setSelected(true);
break;
case MotionEvent.ACTION_UP:
this.onChangeListener.onStopTrackingTouch(this);
this.setPressed(false);
this.setSelected(false);
break;
case MotionEvent.ACTION_CANCEL:
super.onTouchEvent(event);
this.setPressed(false);
this.setSelected(false);
break;
}
return true;
}
/**
*
* @param progress
*/
public synchronized void setProgressAndThumb(int progress) {
this.setProgress(progress);
this.onSizeChanged(getWidth(), getHeight() , 0, 0);
if(progress != this.lastProgress) {
this.lastProgress = progress;
this.onChangeListener.onProgressChanged(this, progress, true);
}
}
/**
*
* @return
*/
public synchronized int getMaximum() {
return this.getMax();
}
/**
*
* @param maximum
*/
public synchronized void setMaximum(int maximum) {
this.setMax(maximum);
}
}