Reputation: 582
I'm new to android development.
I've built a MarakableImageView for drawing a circle on an image by tapping on it.
public class MarkableImageView extends ImageView {
ArrayList<Marker> mMarkers;
public MarkableImageView(Context context, AttributeSet attrs){
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
for(Marker m : mMarkers){
// TODO: Draw the marker
canvas.drawCircle(m.x, m.y, 3, paint);
}
}
public boolean onTouchEvent(MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
mMarkers.add(new Marker(e.getX(), e.getY()));
invalidate();
return true;
}
return false;
}
public void reset() {
mMarkers.clear();
invalidate();
}
// this class will be visible only inside MarkableImageView
private class Marker {
public float x;
public float y;
// you might want to add other properties, for example
// if you need to have different types of markers
public Marker(float x, float y) {
this.x = x;
this.y = y;
}
}
}
and to use it in my code I've used:
is = getContentResolver().openInputStream(selectedImageUri);
MarkableImageView iv = (MarkableImageView) findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeStream(is);
iv.setImageBitmap(bmp);
My problem occurs in the second line of the second code section above, because in content_main.xml imageView appears as ImageView and not MarkableImageView.
What do I need to fix?
Upvotes: 2
Views: 239
Reputation: 157457
My problem occurs in the second line of the second code section above, because in content_main.xml imageView appears as ImageView and not MarkableImageView.
you have to declare it in your xml, specifying the fully qualified path to your class (have a look here). E.g.
<com.example.customviews.MarkableImageView
please be aware that your ArrayList<Marker> mMarkers;
is never instantiated. This will make your app crash for NPE
Upvotes: 2