0

First of all, thanks in advance for any help you can provide. This has been driving me nuts and google's been no friend of mine for this.

I've created a DrawView so the user can paint something in the app. What I'd like to do is place that DrawView within a layout XML file, so I can place a TextView title on the top and two buttons on the bottom, one of which will save what the user painted as an image file. So far, all I've been able to do is create an area for the user to paint in. Is there any way to place this DrawView view into an XML file? If not, what alternative solutions do I have?

Also, this is an optional question as I'm sure I'll find the solution by googling, at some point, but is there a better way to smoothly draw along the user's finger movement, other than the canvas.drawLine() method I am currently using?

public class Draw extends Activity {
DrawView drawView;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set full screen view
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                     WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    drawView = new DrawView(this);
    setContentView(drawView);
    drawView.requestFocus();
  }
}

public class DrawView extends View implements OnTouchListener {
  private static final String TAG = "DrawView";

  ArrayList <Float> nikpointx = new ArrayList<Float>(); 
  ArrayList <Float> nikpointy = new ArrayList<Float>();
  Bitmap nikbmp;

  Paint paint = new Paint();

  public DrawView(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);

    this.setOnTouchListener(this);

    paint.setColor(Color.WHITE);
    paint.setAntiAlias(true);
    paint.setStrokeWidth(4);
  }

  @Override
  public void onDraw(Canvas canvas) {

    //I'm doing this, because the drawCircle method places circles at different points
    //in the screen, rather than drawing smooth lines.  I haven't found a more elegant
    //solution yet :-(

    canvas.drawLine(nikpointx.get(i-1), nikpointy.get(i-1), nikpointx.get(i), nikpointy.get(i), paint);
            canvas.setBitmap(nikbmp);


  }     
}

public boolean onTouch(View view, MotionEvent event) {
    // if(event.getAction() != MotionEvent.ACTION_DOWN)
    // return super.onTouchEvent(event);
    Point point = new Point();
    point.x = event.getX();
    point.y = event.getY();
    points.add(point);
    invalidate();
    Log.d(TAG, "point: " + point);



    nikpointx.add(event.getX());
    nikpointy.add(event.getY());

    return true;
}
}
LU RD
  • 34,438
  • 5
  • 88
  • 296
Wildblood
  • 40
  • 5

1 Answers1

0

You can place you custom view in the xml layout like this:

<full.package.to.customview.DrawView android:id="@+id/idOfCustom" 
//other attributes
 />

You can also add custom attributes to your view to set directly from the xml file.

I don't have any solutions for your second problem.

edit: after a quick search on this site i found this -> Android How to draw a smooth line following your finger

Community
  • 1
  • 1
user
  • 86,916
  • 18
  • 197
  • 190
  • Thanks for the XML code and for the link! I added my custom view to the XML after adding some constructors to it. Problem is, now it's embedded within my main.xml, I can no longer paint onto it. Any ideas why? I did fill_parent for both width and height and wedged it between two textviews, so I know it's there, on my screen - but it lost its interactivity. – Wildblood Jan 25 '12 at 09:42
  • @Wildblood check and see what you actually draw(if you get what you expect). I've made a simple test(that you can see here -> http://pastebin.com/A7Hv7fZQ) and there is no problem on drawing something on touch. – user Jan 25 '12 at 10:13
  • Thanks again for your answer. It wasn't working because I noobed - I didn't set an onTouchListener in my main activity. Looks like it's working wonderfully now, cheers! – Wildblood Jan 25 '12 at 10:30