0

I have this classes

MyTouchEventView.java

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;

public class MyTouchEventView extends View {

private Paint paint = new Paint();
private Path path = new Path();
private Path circlePath = new Path();

public Button btnReset;
public LayoutParams params;

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

    paint.setAntiAlias(true);
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(4f);

    btnReset = new Button(context);
    btnReset.setText("Clear Screen");

    params = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    btnReset.setLayoutParams(params);

    btnReset.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            path.reset();
            postInvalidate();

        }
    });

}

@Override
protected void onDraw(Canvas canvas) {

    canvas.drawPath(path, paint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    float pointX = event.getX();
    float pointY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        path.moveTo(pointX, pointY);

        return true;
    case MotionEvent.ACTION_MOVE:
        path.lineTo(pointX, pointY);
        circlePath.reset();

        circlePath.addCircle(pointX, pointY, 30, Path.Direction.CW);

        break;

    case MotionEvent.ACTION_UP:
        circlePath.reset();

        break;
    default:
        return false;
    }

    postInvalidate();
    return true;
}
}

DrawingBrush.java

public class DrawingBrush extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MyTouchEventView tv = new MyTouchEventView(this);
    setContentView(tv);
    addContentView(tv.btnReset, tv.params);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

Now I want to convert this

    MyTouchEventView tv = new MyTouchEventView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);

to be as setContentView(R.layout.main); and put the button and other components that in view in "main.xml" layout

how can I do this? hope anyone got my mean.

0

1 Answer 1

2

MyTouchEventView should have at least one more constructor. This constructor should accept a AttributeSet parameter, besides Context.

public MyTouchEventView (Context context, AttributeSet attrs) {
  // perform initialization
}

And it will be invoked when the view is declared in XML layout file.

Then, you would declare the view in XML like this:

<com.my_package_name.MyTouchEventView
     ....
      />

This topic is explained in detail in Android docs. Have a look here: http://developer.android.com/training/custom-views/index.html

Sign up to request clarification or add additional context in comments.

3 Comments

I want to remove MyTouchEventView class and put what in it to the class DrawingBrush
I'm not sure I understand what you just said. But, if you need to use the MyTouchEventView in DrawingBrush, modify the MyTouchEventView class by adding one more constructor, create a new layout XML file, declare the MyTouchEventView in that XML file, and then use this XML file in DrawingBrush. If you are new to custom views, I highly suggest you pause a bit, and read the docs first.
Thanks for your answer, this example mobile.tutsplus.com/tutorials/android/… helped me too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.