私はRelativeLayoutの2つのビューの間に線を描こうとしていますが、これを行うことはできません。私は、ビューが自分の位置を変更したときに変更して再描画することができ、その中心から二つのビューの間に線を描きたいImageButtonCustomAndroidの2つのビュー間に線を引く
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.bobo2);
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(ViewPositionUtil.getXYPositionRelativeToRoot(imageButtonCustom));//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);
}
}
編集
public class CustomRelativeLayout extends RelativeLayout { private Context mContext; private ImageButtonCustom[] imageButtonCustoms = new ImageButtonCustom[3]; private Paint paint; CustomRelativeLayout customRelativeLayout; //private LineView lineView; public CustomRelativeLayout(Context context) { super(context); this.mContext = context; init(); } public CustomRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; init(); } public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mContext = context; init(); } private void init() { //setBackgroundColor(Color.BLACK); customRelativeLayout = this; setWillNotDraw(false); paint = new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth((float) 25); for(int x = 0 ; x < 3 ; x++){ imageButtonCustoms[x] = new ImageButtonCustom(mContext,customRelativeLayout); imageButtonCustoms[x].setOnMoveListener(new ImageButtonCustom.OnMoveListener() { @Override public void onMove(Position positionXY) { } }); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(imageButtonCustoms[0] != null && imageButtonCustoms[1] != null) canvas.drawLine(imageButtonCustoms[0].getCenterX(),imageButtonCustoms[0].getCenterY() ,imageButtonCustoms[1].getCenterX(),imageButtonCustoms[1].getCenterY(),paint); if(imageButtonCustoms[1] != null && imageButtonCustoms[2] != null) canvas.drawLine(imageButtonCustoms[1].getCenterX(),imageButtonCustoms[1].getCenterY() ,imageButtonCustoms[2].getCenterX(),imageButtonCustoms[2].getCenterY(),paint); if(imageButtonCustoms[0] != null && imageButtonCustoms[2] != null) canvas.drawLine(imageButtonCustoms[0].getCenterX(),imageButtonCustoms[0].getCenterY() ,imageButtonCustoms[2].getCenterX(),imageButtonCustoms[2].getCenterY(),paint); } }
。
あなたは線色、幅1 dp(線の太さ)として背景を持つ2 ..の間にもう1つのビューを使用することができました。 – Pr38y
あなたはすでにあなたのonDraw()をデバッグしましたか? getX()とgetY()に実際にビューの座標が含まれていますか? – Chris623