0

I want to make an app that can edit a picture. The activity that I have now has a bitmap imageview. I want to be able to place circles on the image. The implementation I have now puts the circles on a view BEHIND the bitmap image. How can I place the circles on the image, that stay there even when I zoom in/ move the image? Here is my code:

public class ImageDisplayActivity extends FragmentActivity {

    public static final String KEY_PATH = "img.jpg";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_display);



    Intent intent = getIntent();
    String path = getIntent().getStringExtra(ImageDisplayActivity.KEY_PATH);
    try {
        java.io.FileInputStream in = this.openFileInput(path);
        Bitmap bitmap = BitmapFactory.decodeStream(in);
        ZoomInZoomOut touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
        touch = arrangeImageView(touch);
        touch.setImageBitmap(bitmap);
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Button button = (Button) findViewById(R.id.button2);
    final ball mCanvasView;
    mCanvasView = (ball) findViewById(R.id.ball1);
    button.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCanvasView.drawCircle();
                }
            }
    );
}
private ZoomInZoomOut arrangeImageView(ZoomInZoomOut img){
    try {
        img.setRotation(90);
        img.setScaleX(2f);
        img.setScaleY(2f);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return img;
}

}

The 'ball' class:

public class ball extends View {
 public Paint mPaint;
public static Canvas mCanvas;
private int mPivotX = 0;
private int mPivotY = 0;
private int radius = 60;

//constructor
public ball(Context context, AttributeSet attrs) {
    super(context, attrs);
    mPaint = new Paint();
}

public void drawCircle() {

    int minX = radius * 2;
    int maxX = getWidth() - (radius *2 );

    int minY = radius * 2;
    int maxY = getHeight() - (radius *2 );

    //Generate random numbers for x and y locations of the circle on screen
    Random random = new Random();
    mPivotX = random.nextInt(maxX - minX + 1) + minX;
    mPivotY = random.nextInt(maxY - minY + 1) + minY;

    //important. Refreshes the view by calling onDraw function
    invalidate();

}

//what I want to draw is here
protected void onDraw(Canvas canvas) {
    mCanvas = canvas;
    super.onDraw(mCanvas);
    canvas.drawColor(Color.GRAY);
    mPaint.setColor(Color.BLUE);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setAntiAlias(true);
    canvas.drawCircle(mPivotX, mPivotY, radius, mPaint);
}

}

The XML for Image display activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"   android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/background"
tools:context="com.commonsware.android.test1.ImageDisplayActivity"
>


<com.commonsware.android.test1.ball
    android:id="@+id/ball1"
    android:layout_width="143dp"
    android:layout_height="169dp"
    android:layout_column="1"
    android:layout_gravity="left|top"
    android:layout_row="0" />

<com.commonsware.android.test1.ZoomInZoomOut
    android:id="@+id/IMAGEID"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="O"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    />

nnnnnnitters
  • 65
  • 10
  • You should probably use a `Canvas` instead. – Jonas Czech Mar 17 '15 at 18:31
  • @JonasCz How can I use canvas to edit the picture to add circles? My app uses gestures already to zoom in and zoom out the imageview, so I would need the circles to scale up and down accordingly... – nnnnnnitters Mar 17 '15 at 18:39
  • See [this question](http://stackoverflow.com/questions/25941097/android-pinch-zoom-and-panning-for-canvas-something-doesnt-work) for how to do pinch-to-zoom in `Canvas`. It should not be too hard to draw circles, since that is what canvas is for. – Jonas Czech Mar 17 '15 at 18:53

0 Answers0