Android studio实现画板功能

LinearLayout ll_layout = findViewById(R.id.ll_layout);

RadioGroup rg_color = findViewById(R.id.rg_color);

遍历单选按钮,当单选按钮选中时,获取单选按钮颜色并将画笔颜色设置当前按钮的文本颜色,最后注意要设置画笔宽度,以免在后面点橡皮擦的时候画笔宽度调不回来

for (int i = 0;i<rg_color.getChildCount();i++){

RadioButton rb = (RadioButton) rg_color.getChildAt(i);

rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (buttonView.isChecked()){

paint.setColor(buttonView.getTextColors().getDefaultColor());

paint.setStrokeWidth(5);

}

}

});

}

首先创建一张空白图片和一张灰色画布,将图片放在画布上面

imageview = findViewById(R.id.imageview);

Log.i(“MyPaintToolsActivity”,imageview.getWidth()+" "+imageview.getHeight());

Point point = new Point();

getWindowManager().getDefaultDisplay().getSize(point);

Log.i(“MyPaintToolsActivity”,point.x+" "+point.y);

bitmap = Bitmap.createBitmap(888,1200,Bitmap.Config.ARGB_8888);

canvas = new Canvas(bitmap);

canvas.drawColor(Color.argb(100,0,0,0));

paint = new Paint();

paint.setStrokeWidth(5);

paint.setAntiAlias(true);

paint.setColor(Color.RED);

canvas.drawBitmap(bitmap,new Matrix(),paint);

imageview.setImageBitmap(bitmap);

注册触摸监听事件,获取鼠标按下时的坐标和鼠标移动后的坐标。在开始和结束之间画一条直线并更新画布图片

imageview.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

switch(event.getAction()){

case MotionEvent.ACTION_DOWN:

Log.i(“MyPaintToolsActivity”,“ACTION_DOWN”);

startX = (int) (event.getX()/1.4);

startY = (int) (event.getY()/1.4);

break;

case MotionEvent.ACTION_MOVE:

Log.i(“MyPaintToolsActivity”,“ACTION_MOVE”);

endX = (int) (event.getX()/1.4);

endY = (int) (event.getY()/1.4);

canvas.drawLine(startX,startY,endX,endY,paint);

startX = (int) (event.getX()/1.4);

startY = (int) (event.getY()/1.4);

imageview.setImageBitmap(bitmap);

break;

case MotionEvent.ACTION_UP:

Log.i(“MyPaintToolsActivity”,“ACTION_UP”);

break;

}

imageview.invalidate();

return true;

}

});

清屏的话就一行代码 ,剩下的是重新生成一块画布

Button btn_clear = findViewByI
d(R.id.btn_clear);

btn_clear.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

canvas.drawColor(0,PorterDuff.Mode.CLEAR);

bitmap = Bitmap.createBitmap(888,1200,Bitmap.Config.ARGB_8888);

canvas = new Canvas(bitmap);

canvas.drawColor(Color.argb(100,0,0,0));

paint = new Paint();

paint.setStrokeWidth(5);

paint.setAntiAlias(true);

paint.setColor(Color.RED);

canvas.drawBitmap(bitmap,new Matrix(),paint);

imageview.setImageBitmap(bitmap);

}

});

呃,这里会把画布擦掉…也就是擦成白色…

// 擦除

Button btn_eraser = findViewById(R.id.btn_eraser);

btn_eraser.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

paint.setColor(Color.rgb(250,250,250));

paint.setStrokeWidth(30);

}

});

}

}

最后看看页面布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”

android:id=“@+id/ll_layout”>

<ImageView

android:id=“@+id/imageview”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_weight=“1” />

<RadioGroup

android:background=“#747373”

android:layout_width=“match_parent”

android:orientation=“horizontal”

android:id=“@+id/rg_color”

android:layout_height=“wrap_content”>

<RadioButton

android:id=“@+id/rb_red”

android:layout_width=“wrap_content”

android:layout_height=“43dp”

android:layout_weight=“1”

android:text=“红色”

android:textColor=“#FF0000”

android:textSize=“18sp” />

<RadioButton

android:id=“@+id/rb_green”

android:layout_width=“wrap_content”

android:layout_height=“30dp”

android:layout_weight=“1”

android:text=“黑色”

android:textColor=“#000000”

android:textSize=“18sp” />

<RadioButton

android:id=“@+id/rb_blue”

android:layout_width=“wrap_content”

android:layout_height=“30dp”

android:layout_weight=“1”

android:text=“白色”

android:textColor=“#FFFFFF”

android:textSize=“18sp” />

<LinearLayout

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值