Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawing on Image View android

Can anybody help me with how to draw a circle of specific radius on an ImageView every time pixel values (centers for the circle to be drawn) are received from another Activity?

like image 393
xiongdi Avatar asked Apr 27 '26 19:04

xiongdi


1 Answers

Extend ImageView and override onDraw(Canvas c) method - add c.drawCirle(x,y,radius,Paint)

Or for drawing circle you can use pure Canvas as well.

I tried this way but still no luck

public class MyActivity extends Activity {
private MyImageView myImageView;
 float x, y;
 float [] loc;
float radius = 50;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_location);
    myImageView = (MyImageView) findViewById(R.id.mapimageView);
    myImageView.setImageResource(R.drawable.mymap);

    //Receiving location information(x/y pixels array) from myLocation Activity         
        Intent i = getIntent();     
        loc = i.getFloatArrayExtra("Location");

}
public class MyImageView extends ImageView
{

    public MyImageView(Context context) {
        super(context);         
    }
    // Constructor for inflating via XML
   public MyImageView(Context context, AttributeSet attrs) {
   super(context, attrs);         
    }


            @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        x= loc[0];
        y=loc[1];
        p.setColor(Color.RED);
        p.setStrokeWidth(2);        
        canvas.drawCircle(x, y, radius, p);
    }       
}

And in xml change <ImageView to your.package.name.MyImageView

// EDIT Im writing it only from memmory,so there could be typos

like image 101
Hruskozrout Avatar answered Apr 30 '26 10:04

Hruskozrout