Android——设计一个能在图片上涂鸦的程序

一、代码实现

1、设计布局函数




    

    
        

2、设计主控文件

package com.example.e4__5;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {
    private HandWrite handWrite=null;
    private Button clear =null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handWrite=(HandWrite)findViewById(R.id.icon);
        clear=(Button)findViewById(R.id.clear);
        clear.setOnClickListener(new mClick());
    }

    public class mClick implements OnClickListener{
        public void onClick(View v){
            handWrite.clear();
        }

    }
}

3、记录在屏幕上滑动的轨迹,实现在图片上涂鸦的功能

package com.example.e4__5;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class HandWrite extends View {
    Paint paint=null;
    Bitmap originalBitmap=null;
    Bitmap new1_Bitmap=null;
    Bitmap new2_Bitmap=null;
    float startX=0,startY=0;
    float clickX=0,clickY=0;
    boolean isMove=true;
    boolean isClear=false;
    int color= Color.GREEN;
    float strokeWidth=2.0f;

    public HandWrite(Context context, AttributeSet attrs){
        super(context,attrs);
        originalBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.icon).copy(Bitmap.Config.ARGB_8888,true);
        new1_Bitmap=Bitmap.createBitmap(originalBitmap);
    }
    public void clear(){
        isClear=true;
        new2_Bitmap=Bitmap.createBitmap(originalBitmap);
        invalidate();
    }
    public void setstyle(float strokeWidth){
        this.strokeWidth=strokeWidth;
    }
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.drawBitmap(HandWriting(new1_Bitmap),0,0,null);
    }
    public Bitmap HandWriting(Bitmap o_Bitmap){
        Canvas canvas=null;
        if(isClear){
            canvas=new Canvas(new2_Bitmap);
        }else {
            canvas=new Canvas(o_Bitmap);
        }
        paint=new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setStrokeWidth(strokeWidth);
        if(isMove){
            canvas.drawLine(startX,startY,clickX,clickY,paint);
        }
        startX=clickX;
        startY=clickY;
        if(isClear){
            return new2_Bitmap;
        }
        return o_Bitmap;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event){
        clickX=event.getX();
        clickY=event.getY();
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            isMove=false;
            invalidate();
            return true;
        }else if(event.getAction()==MotionEvent.ACTION_MOVE){
            isMove=true;
            invalidate();
            return true;

        }
        return  super.onTouchEvent(event);

    }
}

二、结果截图

Android——设计一个能在图片上涂鸦的程序_第1张图片Android——设计一个能在图片上涂鸦的程序_第2张图片

你可能感兴趣的:(Android——设计一个能在图片上涂鸦的程序)