XML布局上的自定义视图

时间:2014-08-28 10:44:32

标签: java android xml

我制作了一个自定义的View DrawView.java,在那里我绘制了一条简单的线条。我希望这个自定义视图覆盖我的mainActivity.java的activity_main.xml。出于测试目的,XML文件中有一个简单的Button。绘制的线应该在Button上绘制。此时,绘制的线条位于按钮下,分别位于XML视图下。

我如何实现这一目标,在一个完美的世界中仍然保持按钮可点击? ;)

DrawView.java

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

private int endX;
private int endY;
private int startX;
private int startY;

public DrawView(Context context) {
    super(context);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mInflater.inflate(R.layout.activity_main, this, true);
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(10);

}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawLine(startX, startY, endX, endY, paint);

}

public void setCoordinates(int startX, int startY, int endX, int endY) {
    this.endX = endX;
    this.endY = endY;
    this.startX = startX;
    this.startY = startY;
}}

MainActivity.java

public class MainActivity extends ActionBarActivity implements OnTouchListener{

DrawView drawView;

private int number;
private Handler handler;
private boolean Running = true;
private int endX = 500;
private int endY = 500;
private int startX = 50;
private int startY = 50;
private int frames = 25;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    drawView = new DrawView(this);
    drawView.setBackgroundColor(Color.WHITE);

    handler = new Handler();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            while (Running) {
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        int coordX = ((((endX - startX) / frames) * number))
                                + startX;
                        int coordY = ((((endY - startY) / frames) * number))
                                + startY;
                        number += 1;
                        drawView.setCoordinates(startX, startY, coordX,
                                coordY);
                        setContentView(drawView);
                        if ((coordX - endX) == 0 && coordY - endY ==0) {
                            Running = false;
                        }
                    }
                });
            }
            // TODO Auto-generated method stub

        }

    };
    new Thread(runnable).start();
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return false;
}}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.trialar.linedrawer.MainActivity"
>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="180dp"
    android:text="Button" />

更新

嗨,马丁,我尝试了你的解决方案,它对我有用。我用你的方法代替了Imageview。当我点击一个按钮时,画出了我的画线。不幸的是,当绘制这一行时,我的onClicklistener不再被调用。但按钮仍然是可点击的(颜色变为蓝色默认的android按钮)。

public class MainActivity extends ActionBarActivity implements OnTouchListener,
    OnClickListener {

// DrawView drawView;

private int number;
private Handler handler;
private boolean Running = true;
private int endX = 50;
private int endY = 500;
private int startX = 50;
private int startY = 50;
private int frames = 25;
ImageView Line01;
Button button2;
Button buttonLineDrawer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // drawView = new DrawView(this);
    // drawView.setBackgroundColor(Color.WHITE);

    Line01 = (ImageView) findViewById(R.id.Line01);
    Line01.setVisibility(View.GONE);
    Button buttonLineDrawer;
    buttonLineDrawer = (Button) findViewById(R.id.buttonLineDrawer);
    buttonLineDrawer.setOnClickListener(this);

    Button button2;
    button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(this);

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.buttonLineDrawer:
        Toast.makeText(getApplicationContext(), "klick", Toast.LENGTH_LONG)
                .show();
        Running = true;

        number = 0;
        drawLine();

        break;
    case R.id.button2:
        Toast.makeText(getApplicationContext(), "klick", Toast.LENGTH_LONG)
                .show();
        break;
    }

}

public void drawLine() {
    Line01.setVisibility(View.VISIBLE);
    handler = new Handler();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {

            while (Running) {

                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        int coordX = ((((endX - startX) / frames) * number))
                                + startX;
                        int coordY = ((((endY - startY) / frames) * number))
                                + startY;
                        number += 1;
                        CustomDraw.setCoordinates(startX, startY, coordX,
                                coordY);
                        setContentView(R.layout.activity_main);
                        if ((coordX - endX) == 0 && coordY - endY == 0) {
                            Running = false;

                        }
                    }
                });
            }

        }

    };
    new Thread(runnable).start();
}

}

1 个答案:

答案 0 :(得分:0)

你可以将按钮子类化,并在那里自定义绘图吗?!

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.Button;

public class CustomDraw extends Button {

    public CustomDraw(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomDraw(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomDraw(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //your custom draw here
    }

}

您必须在布局中设置该按钮,然后......

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="de.trialar.linedrawer.MainActivity"
>

<com.example.yourpackage.CustomDraw
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="180dp"
 android:text="Button" />