Android绘图/着色应用

时间:2016-02-06 16:25:13

标签: android graphics drawing android-view

我正在制作一个使用绘图功能的Android着色应用程序。我有它,以便用户可以选择多种颜色,并且他们可以选择颜色并绘制,但是当他们选择新颜色时,它会更改所有先前绘制的线条,而不仅仅是正在绘制的新线条。任何帮助我如何纠正这个并在图纸上使用多种颜色非常感谢。我的一些代码如下:第一个是着色类 公共类着色扩展了View {

// setup initial color
private int paintColor = Color.BLACK;
private Paint drawPaint;
private Path drawPath;
// stores the path
//private Path path = new Path();

public Colouring(Context context, AttributeSet attrs) {
    super(context, attrs);
    setFocusable(true);
    setFocusableInTouchMode(true);
    setupcolour();
}

private void setupcolour() {
    // Setup paint with color and stroke styles
    drawPath = new Path();
    drawPaint = new Paint();
    drawPaint.setColor(paintColor);
    drawPaint.setAntiAlias(true);
    drawPaint.setStrokeWidth(15);
    drawPaint.setStyle(Paint.Style.STROKE);
    drawPaint.setStrokeJoin(Paint.Join.ROUND);
    drawPaint.setStrokeCap(Paint.Cap.ROUND);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawPath(drawPath, drawPaint);

}

public void setColor(String newColor) {
    invalidate();
    paintColor = Color.parseColor(newColor);
    drawPaint.setColor(paintColor);

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float pointX = event.getX();
    float pointY = event.getY();
    // Checks for the event that occurs
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            drawPath.moveTo(pointX, pointY);
            return true;
        case MotionEvent.ACTION_MOVE:
            drawPath.lineTo(pointX, pointY);
            break;
        default:
            return false;
    }
    // Force a view to draw again
    invalidate();
    return true;
}

}

,第二个是绘图发生的主要活动。 公共类ColourImage扩展了Activity {

private Colouring drawView;
private ImageButton currcolour, save;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_colour_image);
    drawView = (Colouring)findViewById(R.id.colouringBook);
    LinearLayout paintLayout = (LinearLayout) findViewById(R.id.colours);
    currcolour = (ImageButton) paintLayout.getChildAt(0);
    currcolour.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));

    save = (ImageButton)findViewById(R.id.save);
}

public void saveclicked(View view){
    drawView.setDrawingCacheEnabled(true);
    String saveImage = MediaStore.Images.Media.insertImage(
            getContentResolver(), drawView.getDrawingCache(),
            UUID.randomUUID().toString()+".png", "colouredImage");
    if(saveImage!=null){
        Toast savedToast = Toast.makeText(getApplicationContext(),
                "Your colouring has been saved to your phone", Toast.LENGTH_SHORT);
        savedToast.show();
    }
    else{
        Toast unsavedToast = Toast.makeText(getApplicationContext(),
                "Your colouring was not saved", Toast.LENGTH_SHORT);
        unsavedToast.show();
    }
    drawView.destroyDrawingCache();
}

public void paintClicked(View view){
    if(view!=currcolour){
        ImageButton colourImage = (ImageButton)view;
        String color = view.getTag().toString();

        drawView.setColor(color);
        colourImage.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
        currcolour.setImageDrawable(getResources().getDrawable(R.drawable.paint));
        currcolour=(ImageButton)view;
    }
}

}

再次感谢:)

0 个答案:

没有答案