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

一、内容

设计两种形式的对话框程序,一种是发出提示信息的普通对话框,另一种是用户登录对话框。

二、实现

1.主界面 activity_main.xml



    
    
        

2.主控制程序 MainActivity.java

import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends AppCompatActivity {
 
    HandWrite handWrite = null;
    Button clear = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handWrite=(HandWrite)findViewById(R.id.handwriteview);
        clear=(Button)findViewById(R.id.clear);
        clear.setOnClickListener(new mClick());
    }
    class mClick implements View.OnClickListener{
 
        @Override
        public void onClick(View v) {
            handWrite.clear();
        }
    }
}

3.HandWrite.java 的代码

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.support.annotation.Nullable;
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.YELLOW;
    float strokeWidth = 2.0f;
    public HandWrite(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        originalBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.cy).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 Studio设计一个能在图片上面涂鸦的程序)