两种观点之间Android-绘制线 [英] Android- Draw line between two views

查看:172
本文介绍了两种观点之间Android-绘制线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的,我已经创建了一个框架三个新观点的程序。在点击了两种不同的看法我想画的视图之间的一条线。我试图找出如何做到这一点...

 球球1 =新球(这一点,100,100,45);
    球ball2 =新球(这一点,40​​0,100,45);
    球ball3 =新球(这一点,250,350,45);
    的FrameLayout帧1 =(的FrameLayout)findViewById(R.id.main_view);
    frame1.addView(球1);
    frame1.addView(ball2);
    frame1.addView(ball3);

      frame1.setOnTouchListener(新View.OnTouchListener(){
        公共布尔onTouch(视图V,MotionEvent事件){
            开关(event.getAction()){
            案例MotionEvent.ACTION_DOWN:{
                浮X = event.getX();
                浮动Y = event.getY();
                的System.out.println(×:+ X +Y:+ y)基
                如果(X GT; 55&安培;&安培; X  - 其中; 142&安培;&安培y与其所连接; 55&安培;&安培; Y&所述; 142)
                    {
                    的System.out.println(working1+计);
                    吐司面包= Toast.makeText(getBaseContext(),做工精细,Toast.LENGTH_SHORT);
                    toast.show();
                }
 

解决方案

有关你的两个观点之间平局线。

有关视图,它划清界限创建类。

 公共类drawView函数扩展视图{
    涂料粉刷=新的油漆();

    公共drawView函数(上下文的背景下){
        超(上下文);
        paint.setColor(Color.BLACK);
    }

    @覆盖
    公共无效的OnDraw(帆布油画){
            canvas.drawLine(0,50,350,50,油漆);
    }

}
 

现在从activtiy要加入这一行的布局。 创建这个类的对象,并在布局中添加了这一观点。

根据您的需求量的尝试是这样的。

drawView函数drawView函数;  drawView函数=新drawView函数(本);

  frame1.addView(球1);
                            //这里补充一点看法
frame1.addView(drawView函数);
frame1.addView(ball2);
                            //这里同样的方式
frame1.addView(ball3);
 

有关更详细的请参见例

Below is my program where I have created three new views in a frame. On click of two different views I want to draw a line between the views. I am trying to figure out how to do this...

    Ball ball1=new Ball(this,100,100,45);
    Ball ball2=new Ball(this,400,100,45);
    Ball ball3=new Ball(this,250,350,45);
    FrameLayout frame1=(FrameLayout) findViewById(R.id.main_view);
    frame1.addView(ball1);
    frame1.addView(ball2);
    frame1.addView(ball3);

      frame1.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
            case MotionEvent.ACTION_DOWN: {
                float x = event.getX();
                float y = event.getY();
                System.out.println("x:"+x+"y:"+y);
                if (x>55 && x<142 && y>55 && y<142) 
                    {
                    System.out.println("working1 "+count);
                    Toast toast = Toast.makeText(getBaseContext(), "Works fine", Toast.LENGTH_SHORT);
                    toast.show();
                }

解决方案

For a draw line between your two views.

Create class for view which draw a line.

public class DrawView extends View {
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.BLACK);
    }

    @Override
    public void onDraw(Canvas canvas) {
            canvas.drawLine(0, 50, 350, 50, paint);
    }

}

now from your activtiy where you want to add this line in your layout. Create object of this class and add this view in your layout.

According to your requirment try like this.

DrawView drawView; drawView = new DrawView(this);

frame1.addView(ball1);
                            // add that view here
frame1.addView(drawView);
frame1.addView(ball2);
                            // same way here
frame1.addView(ball3);

For more Detail See Example

这篇关于两种观点之间Android-绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆