Android:使用Canvas编辑图片?

时间:2015-03-17 17:36:52

标签: java android canvas bitmap imageview

我想创建一个可以编辑图片的应用。我现在的活动有一个位图imageview。我希望能够在图像上放置圆圈。我现在的实现将圆圈放在位图图像后面的视图上。如何将圆圈放在图像上,即使放大/移动图像,也会留在那里?这是我的代码:

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;
}

}

'球'类:

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);
}

}

XML for Image显示活动:

<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"
    />

0 个答案:

没有答案