我的想法是这样的,先获得两个控件在屏幕上的位置,然后用自定义View画线。求各位大神,指点迷津,在两个控件间画线要怎么实现。
代码如下:
class Userview extends View {
private Paint uPaint;
private Rect user_space;
public play plat_istance;//建立一个play类对象
private Canvas canvas;
public Userview(Context context, AttributeSet attrs) {
super(context, attrs);
uPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
user_space = new Rect();
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
uPaint.setColor(Color.WHITE);
uPaint.setStrokeWidth(10);
//canvas.drawRect(0, 0, getWidth(), getHeight(), uPaint);
canvas.drawLine(plat_istance.location[0],plat_istance.location[1],plat_istance.posetion[0],plat_istance.posetion[1], uPaint);
//画线函数,里面前四个参数是数组里的值
}
}
public class play extends Activity {
public int[] location = new int[2];
public int[] posetion = new int[2];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play_case3);
RelativeLayout cc=(RelativeLayout)findViewById(R.id.relativeLayout1);
RadioButton r1=(RadioButton)findViewById(R.id.radioButton1);
RadioButton r2=(RadioButton)findViewById(R.id.radioButton2);
r1.getLocationInWindow(location);//获得r1的坐标
r2.getLocationInWindow(posetion);//获得r2的坐标
}
}
------解决方案--------------------
额,代码雏形,如果你要画的话还要考虑到不同屏幕分辩率,代码的话百度下吧,,,
------解决方案--------------------
楼主
你代码有一个问题
你把线画在了Userview了
却没把Userview显示出来
还不如这样写
Boolean btnstate1=false;
Boolean btnstate2=false;
Boolean btnstate3=false;
public void Button1_click(View v){
btnstate1=true;
if(btnstate2=true)
{//按钮1画线连接按钮2}
else if(btnstate3=true)
{//按钮2画线连接按钮3}
else
{//do nothing}
}