如何在Imageview上绘画?

时间:2017-02-15 07:55:09

标签: android android-paint

我有ImageView,我画了四行。现在我希望用户能够绘制图像。如何实现这一目标?

在另一个解决方案中,我看到我们必须添加另一个视图,一个自定义视图,此解决方案不适合这种情况。

Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
    .getDefaultDisplay().getWidth(), (int) getWindowManager()
    .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawingImageView.setImageBitmap(bitmap);
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int x = metrics.widthPixels;
int y = metrics.heightPixels;
Paint paint1 = new Paint () ;
paint1.setStrokeWidth(10);
int margin = 100;
int margin1 = 300;
int top = 0 + margin;
int bottom = canvas.getHeight() - margin;
int left = 0 + margin1;
int right = canvas.getWidth() - margin1;
int centerX = x / 2;
int centerY = y / 2;

canvas.drawLine(centerX, top, centerX, bottom,paint1);

canvas.drawLine(left, centerY, right, centerY,paint1);

我的布局XML:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.amgsoft_pc.flowermenu.MainActivity">


    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button1"
        android:visibility="invisible" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"

        android:id="@+id/button2"
        android:visibility="invisible" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button2"
        android:layout_alignParentEnd="true"
        android:id="@+id/button3"
        android:visibility="invisible" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/button1"
        android:id="@+id/button4"
        android:visibility="invisible" />
    <Button
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@drawable/round_button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/circle"  />
    <ImageView
        android:id="@+id/DrawingImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:visibility="invisible"/>
    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_alignParentStart="true"
        android:layout_marginTop="49dp"
        android:id="@+id/button5"
        android:visibility="invisible" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button5"
        android:layout_alignStart="@+id/button3"
        android:id="@+id/button6"
        android:visibility="invisible" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button4"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="58dp"
        android:id="@+id/button7"
        android:visibility="invisible" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button7"
        android:layout_alignParentEnd="true"
        android:id="@+id/button8"
        android:visibility="invisible" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

以下摘自Appium的FingerPaint.java

public class CanvasView extends View {
    public int widht;
    public int height;
    private Bitmap mbitmap;
    private Canvas mcanvas;
    private Path path;
    private Paint paint;
    private float mX, mY;
    Context context;
    private static final float Tolerance = 5;

    public CanvasView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        path = new Path();
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(4f);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mbitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
        mcanvas = new Canvas(mbitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(path,paint);
    }

    private void startTouch (float x , float y){
    path.moveTo(x,y);
        mX = x;
        mY = y ;
    }
    public void moveTouche (float x,float y ) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if(dx >= Tolerance || dy >= Tolerance){
            path.quadTo(mX,mY,(x+mX)/2,(y+mY)/2);
            mX = x ;
            mY = y;

        }
    }
    private void upTouch(){
        path.lineTo(mX,mY);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                startTouch(x,y);
                invalidate();
                break ;
            case MotionEvent.ACTION_UP:
                upTouch();
                invalidate();
                break ;
            case MotionEvent.ACTION_MOVE:
                moveTouche(x,y);
                invalidate();
                break ;

        }
        return true ;
    }
}

XML

<com.example.amgsoft_pc.flowermenu.CanvasView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/canvas"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"/>