Tôi đã tạo một chế độ xem có thể kéo bên trong RelativeLayout. Nhưng dường như nó vượt ra ngoài RelativeLayout.Giới hạn Chế độ xem để kéo bên trong RelativeLayout
tôi chỉ đơn giản muốn thực hiện một Xem kéo bên ViewGroup
Và quan điểm này là draggable
theo màn hình. Và nó có thể kéo được vượt ra ngoài ranh giới của RelativeLayout. Làm thế nào tôi có thể hạn chế nó ở lại kéo được trong RelativeLayout.
CustomImageButton
public class ImageButtonCustom extends ImageButton implements View.OnTouchListener{
float dX, dY;
private RelativeLayout rootView;
private ImageButtonCustom imageButtonCustom;
private OnMoveListener onMoveListener;
public ImageButtonCustom(Context context,RelativeLayout rootView){
super(context);
this.rootView = rootView;
init();
}
public ImageButtonCustom(Context context) {
super(context);
init();
}
public ImageButtonCustom(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ImageButtonCustom(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
imageButtonCustom = this;
setImageResource(R.drawable.upper_left);
setBackgroundColor(Color.TRANSPARENT);
setOnTouchListener(this);
/*RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.addRule(RelativeLayout.ALIGN_BOTTOM);*/
rootView.addView(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
dX = v.getX() - event.getRawX();
dY = v.getY() - event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
v.animate()
.x(event.getRawX() + dX)
.y(event.getRawY() + dY)
.setDuration(0)
.start();
//no use of ViewPositionUtil
onMoveListener.onMove(new Position());//positionXY);
break;
}
rootView.invalidate();
return true;
}
public void setOnMoveListener(OnMoveListener onMoveListener){
this.onMoveListener = onMoveListener;
}
public float getCenterX(){
return getX() + getWidth()/2;
}
public float getCenterY(){
return getY() + getHeight()/2;
}
public interface OnMoveListener{
void onMove(Position positionXY);
}
}
EDIT:
Các ImageButton là Draggable nhưng nó đi outside of parent
. Giới hạn để kéo trong Bố cục cha mẹ của nó.
cũng đã cố gắng http://stackoverflow.com/ câu hỏi/21662397/cách giữ-an-hình ảnh-trong-bố cục-với-kéo-và-thả – Nepster