0

I have a path drawn on a canvas as shown

@Override
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas);
    path.moveTo(X, Y);
    path.lineTo(X+20, Y);        
    canvas.drawPath(path, paint); 
    invalidate();
}     

I want this to appear in an imageView in an xml, but not sure how to do that. How to I make this canvas appear in the imageview? I have a bitmap but unable to connect all of them.

Leos Literak
  • 8,805
  • 19
  • 81
  • 156
Sparkplug
  • 485
  • 7
  • 21

2 Answers2

0
Path path=new Path();    
path.moveTo(50, 50);
path.lineTo(50, 50);
ShapeDrawable star =
     new ShapeDrawable(new PathShape(path, 100, 100));
     star.setIntrinsicHeight(100);
     star.setIntrinsicWidth(100);
     star.getPaint().setColor(Color.RED);
     star.getPaint().setStyle(Paint.Style.STROKE);
     ImageView iView = (ImageView)findViewById(R.id.ImageView1);
     iView.setImageDrawable(star);
user543
  • 3,623
  • 2
  • 16
  • 14
0

Why not extend the ImageView and override onDraw like this:

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        path.lineTo(X + 20, Y);

        canvas.drawPath(path, paint);
    }

Here everything in ImageView will be draw in addition to your path.

If this is not what you want maybe this could be helpful link

And calling invalidate() in onDraw() may be bad idea. It's recursive.

Community
  • 1
  • 1
Bipin Bhandari
  • 2,694
  • 23
  • 38