在Android studio环境下提供一个完整的实现画板功能的程序
时间: 2024-02-11 08:07:18 浏览: 152
Android 实现画板功能
好的,以下是一个简单的Android画板应用程序的实现代码,你可以根据自己的需求进行修改和扩展:
activity_main.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<Button
android:id="@+id/btn_color"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Color" />
<Button
android:id="@+id/btn_brush"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Brush" />
<Button
android:id="@+id/btn_clear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear" />
</LinearLayout>
</LinearLayout>
```
MainActivity.java:
```java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button btnColor, btnBrush, btnClear;
private FrameLayout frameLayout;
private DrawingView drawingView;
private int selectedColor = Color.BLACK;
private int selectedBrushSize = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnColor = findViewById(R.id.btn_color);
btnBrush = findViewById(R.id.btn_brush);
btnClear = findViewById(R.id.btn_clear);
frameLayout = findViewById(R.id.frame_layout);
drawingView = new DrawingView(this);
frameLayout.addView(drawingView);
btnColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ColorPickerDialog colorPickerDialog = new ColorPickerDialog(MainActivity.this, selectedColor, new ColorPickerDialog.OnColorChangedListener() {
@Override
public void colorChanged(int color) {
selectedColor = color;
}
});
colorPickerDialog.show();
}
});
btnBrush.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BrushSizeDialog brushSizeDialog = new BrushSizeDialog(MainActivity.this, selectedBrushSize, new BrushSizeDialog.OnBrushSizeChangedListener() {
@Override
public void brushSizeChanged(int brushSize) {
selectedBrushSize = brushSize;
}
});
brushSizeDialog.show();
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawingView.clearCanvas();
}
});
}
public class DrawingView extends View {
private Paint paint;
private Path path;
public DrawingView(Context context) {
super(context);
init();
}
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(selectedColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(selectedBrushSize);
path = new Path();
}
public void clearCanvas() {
path.reset();
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(event.getX(), event.getY());
invalidate();
return true;
case MotionEvent.ACTION_UP:
return true;
default:
return false;
}
}
}
}
```
ColorPickerDialog.java:
```java
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
public class ColorPickerDialog extends Dialog {
private int initialColor;
private OnColorChangedListener onColorChangedListener;
public ColorPickerDialog(Context context, int initialColor, OnColorChangedListener onColorChangedListener) {
super(context);
this.initialColor = initialColor;
this.onColorChangedListener = onColorChangedListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_color_picker);
final SeekBar seekBarRed = findViewById(R.id.seek_bar_red);
final SeekBar seekBarGreen = findViewById(R.id.seek_bar_green);
final SeekBar seekBarBlue = findViewById(R.id.seek_bar_blue);
final View colorView = findViewById(R.id.color_view);
Button btnOk = findViewById(R.id.btn_ok);
final int red = Color.red(initialColor);
final int green = Color.green(initialColor);
final int blue = Color.blue(initialColor);
seekBarRed.setProgress(red);
seekBarGreen.setProgress(green);
seekBarBlue.setProgress(blue);
colorView.setBackgroundColor(initialColor);
seekBarRed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
colorView.setBackgroundColor(Color.rgb(progress, green, blue));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBarGreen.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
colorView.setBackgroundColor(Color.rgb(red, progress, blue));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBarBlue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
colorView.setBackgroundColor(Color.rgb(red, green, progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int color = Color.rgb(seekBarRed.getProgress(), seekBarGreen.getProgress(), seekBarBlue.getProgress());
onColorChangedListener.colorChanged(color);
dismiss();
}
});
}
public interface OnColorChangedListener {
void colorChanged(int color);
}
}
```
BrushSizeDialog.java:
```java
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
public class BrushSizeDialog extends Dialog {
private int initialBrushSize;
private OnBrushSizeChangedListener onBrushSizeChangedListener;
public BrushSizeDialog(Context context, int initialBrushSize, OnBrushSizeChangedListener onBrushSizeChangedListener) {
super(context);
this.initialBrushSize = initialBrushSize;
this.onBrushSizeChangedListener = onBrushSizeChangedListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_brush_size);
final TextView txtBrushSize = findViewById(R.id.txt_brush_size);
final SeekBar seekBarBrushSize = findViewById(R.id.seek_bar_brush_size);
Button btnOk = findViewById(R.id.btn_ok);
txtBrushSize.setText(String.valueOf(initialBrushSize));
seekBarBrushSize.setProgress(initialBrushSize);
seekBarBrushSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
txtBrushSize.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int brushSize = seekBarBrushSize.getProgress();
onBrushSizeChangedListener.brushSizeChanged(brushSize);
dismiss();
}
});
}
public interface OnBrushSizeChangedListener {
void brushSizeChanged(int brushSize);
}
}
```
这个画板应用程序实现了以下功能:
- 可以选择不同的画笔颜色;
- 可以选择不同的画笔大小;
- 可以清除画板。
你可以根据自己的需求进行修改和扩展,添加更多的功能。
阅读全文