Drawing on Canvas and save image

前端 未结 2 1576
误落风尘
误落风尘 2020-12-04 11:25

I am new to the Android Graphics class. I want to draw an image(actually a signature kind) using the touch events and want it to be saved on SDcard when I want to save it. I

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 12:04

    the drawing thing

    Scribbler.java:

    package org.yourpackage.scribble;
    
    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    
    public class Scribbler extends Activity {
        DrawView drawView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            drawView = new DrawView(this);
            drawView.setBackgroundColor(Color.WHITE);
            setContentView(drawView);
            drawView.requestFocus();
    
        }
    }
    

    DrawView.java:

    package org.yourpackage.scribble;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    
    public class DrawView extends View implements OnTouchListener {
        List points = new ArrayList();
        Paint paint = new Paint();
    
        public DrawView(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);
            this.setOnTouchListener(this);
            paint.setColor(Color.BLACK);
        }
    
        @Override
        public void onDraw(Canvas canvas) {
            for (Point point : points) {
                canvas.drawCircle(point.x, point.y, 2, paint);  
            }
        }
    
        public boolean onTouch(View view, MotionEvent event) {
            Point point = new Point();
            point.x = event.getX();
            point.y = event.getY();
            points.add(point);
            invalidate();
            return true;
        }
    }
    
    class Point {
        float x, y;
    }
    

    AndroidManifest.xml:

    
    
        
            
                
                    
                    
                
            
        
        
     
    

    ..will give you something like that:

    alt text

    You may want to draw lines instead of pixels

    the saving thing

    To implement saving you can pass a Bitmap into the Canvas constructor, then use the compress method of Bitmap to create an OutputStream which can be written to the SD card

    EDIT to answer your comment:
    Sure you can use XML to define your layout since DrawView extends View you can use it in your layout xml file. An example for the main.xml layout file

    
    
    
        
                
    
    

    that gives you something like that:
    enter image description here
    You'll just need an additional contstructor public DrawView(Context context, AttributeSet attrSet)

提交回复
热议问题