画板——画笔颜色、基本功能
(代码中有基础解释,有疑问欢迎来找我探讨!)
xml
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<!--
constraintLayout
需要知道:x,y,宽,高-->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/operation"
app:layout_constraintTop_toTopOf="parent"
android:orientation="horizontal">
<!--<SeekBar-->
<!--android:id="@+id/slider"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="50dp"-->
<!--android:background="#666"-->
<!--android:progressTint="#000"-->
<!--android:progressBackgroundTint="#f00"-->
<!--android:thumbTint="#fff"-->
<!--android:max="100"/>-->
<!--
滑动条
自定义Slider
1.进度条
2.滑动条
3.横竖切换
创建Slider extends View
-->
<swu.xujiangtao.day_11drawtop.Slider
android:id="@+id/slider"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
app:layout_constraintLeft_toLeftOf="parent"/>
<!--画板-->
<swu.xujiangtao.day_11drawtop.DrawBoardView
android:id="@+id/Board"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintLeft_toRightOf="@+id/slider"
app:layout_constraintRight_toLeftOf="@+id/color"
android:background="@drawable/ic_launcher_foreground"/>
<LinearLayout
android:id="@+id/color"
android:layout_width="60dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginRight="20dp"
app:layout_constraintRight_toRightOf="parent"
android:gravity="center">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@color/colorPrimary"
android:onClick="choiceColor"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#000"
android:onClick="choiceColor"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@color/colorAccent"
android:onClick="choiceColor"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#ff5"
android:onClick="choiceColor"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="@+id/operation"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@color/colorAccent"
android:gravity="center">
<Button
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="撤销"
android:onClick="goBack"
/>
<Button
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="清空"
android:onClick="clear"/>
<Button
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="橡皮擦"
android:onClick="eraser"/>
<Button
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="上一步"
android:onClick="lastStep"/>
<Button
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="保存"
android:onClick="save"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
private DrawBoardView boardView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取画板视图对象
boardView=findViewById(R.id.Board);
//获取滑动条视图对象
final Slider slider = findViewById(R.id.slider);
// new Timer().schedule(new TimerTask() {
// @Override
// public void run() {
// slider.setProgress(
// (float) (slider.getProgress()+0.01));
// }
// },0,100);
slider.setMax(30);
slider.setStyle(Slider.SLIDER);
slider.setOnSliderChangeListener(new Slider.OnSliderChangeListener() {
@Override
public void progressChanged(float progress) {
}
});
slider.setProgress(boardView.getLineSize());
}
@Override
protected void onResume() {
super.onResume();
//设置横竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
void text(){
/*SeekBar slider=findViewById(R.id.slider);
//关键帧动画
//补间动画:旋转 平移 缩放 渐变
//属性动画:ObjectAnimator
//ValueAnimator 在给定值区间动画
//ViewPropertyAnimator animator().动画 快速动画
//slider.animate().rotation(90);
//监听滑动条的滑动事件
slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
//进度值改变
//System.out.println("change");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//监听已经开始拖拽
//System.out.println("start");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//停止拖拽 手放开
//System.out.println("stop");
}
});
*/
}
//选颜色
public void choiceColor(View view) {
//获取按钮上的背景颜色
ColorDrawable drawable = (ColorDrawable)view.getBackground();
//获取颜色
boardView.setLineColor(drawable.getColor());
}
//撤销
public void goBack(View view) {
boardView.removeLast();
}
//清空
public void clear(View view) {
boardView.removeAll();
}
//橡皮擦
public void eraser(View view) {
ColorDrawable drawable = (ColorDrawable)
boardView.getBackground();
if (boardView!=null) {
boardView.setLineColor(drawable.getColor());
}
}
//保存
public void save(View view) {
}
//还原
public void lastStep(View view) {
boardView.returnTolastStep();
}
}
Slider
public class Slider extends View {
private int lineSize = 6; //线条的粗细
private int lineColor = Color.BLACK;//默认线条颜色
private Paint linePaint;
private Paint circlePaint; //小圆点的画笔
private int thumbColor = Color.MAGENTA; //小圆点的颜色
private int cx; //中心点x
private int cy; //中心点y
private int radius; //小圆点半径
private int thumbScale = 4; //小圆点缩放尺寸基数
private float position; //记录触摸点的坐标
private Paint progressPaint; //进度条进度的画笔
private int progressColor = Color.MAGENTA;//颜色
public static int PROGERSS=0;//进度条
public static int SLIDER=1;//滑动条
private int style=PROGERSS;//默认样式
public int max=100;//设置最大值
public float progress;//进度值
//滑动改变
private OnSliderChangeListener onSliderChangeListener;
public Slider(Context context) {
super(context);
}
public Slider(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
//背景线
linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint.setColor(lineColor);
linePaint.setStrokeWidth(lineSize);
//小圆点
circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(thumbColor);
circlePaint.setStyle(Paint.Style.FILL);
//进图条
progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
progressPaint.setColor(progressColor);
progressPaint.setStrokeWidth(lineSize);
}
@Override
protected void onDraw(Canvas canvas) {
if (getWidth() > getHeight()){
//横着
//画背景条
canvas.drawLine(0,
getHeight()/2,
getWidth(),
getHeight()/2,
linePaint);
if (position > 0) {
//画进度条
canvas.drawLine(0,
getHeight() / 2,
position,
getHeight() / 2,
progressPaint);
}
radius = getHeight()/thumbScale;
cy = getHeight()/2;
//确定cx的值
if (position < radius){
cx = radius;
}else if (position > getWidth()-radius){
cx = getWidth()-radius;
}else{
cx = (int) position;
}
}else{
//竖着
canvas.drawLine(getWidth()/2,
0,
getWidth()/2,
getHeight(),
linePaint);
if (position > 0){
canvas.drawLine(getWidth()/2,
0,
getWidth()/2,
position,
progressPaint);
}
radius = getWidth()/thumbScale;
cx = getWidth()/2;
//确定中心点cy的值
if (position < radius){
cy = radius;
}else if(position > getHeight()-radius){
cy = getHeight()-radius;
}else{
cy = (int) position;
}
}
//画小圆点
if (style==SLIDER) {
canvas.drawCircle(cx, cy, radius, circlePaint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//小圆点放大
thumbScale = 2;
if (getWidth() > getHeight()){
//横向时 y不变 x改变
position = event.getX();
}else{
//竖着时 x不变 y改变
position = event.getY();
}
callback();
break;
case MotionEvent.ACTION_MOVE:
//获取当前触摸点的值 X Y
if (getWidth() > getHeight()){
//横向时 y不变 x改变
position = event.getX();
if (position <0){
position = 0;
} else if (position > getWidth()){
position = getWidth();
}
}else{
//竖着时 x不变 y改变
position = event.getY();
if (position <0){
position = 0;
} else if (position > getHeight()){
position = getHeight();
}
}
callback();
break;
case MotionEvent.ACTION_UP:
thumbScale = 4;
break;
}
if (style==SLIDER) {
invalidate();
}
return true;
}
private void callback(){
if (onSliderChangeListener !=null){
if (getWidth()>getHeight()){
progress=position/getWidth();
}else {
progress=position/getHeight();
}
onSliderChangeListener.progressChanged(progress*max);
}
}
public int getStyle() {
return style;
}
public void setStyle(int style) {
this.style = style;
}
public float getProgress() {
return progress;
}
public void setPrpgress(int progress){
//计算比例
this.progress= (float) (progress*1.0/max);
setProgress(this.progress);
}
public void setProgress(float progress) {
this.progress = progress;
if (progress<1.001) {
//将进度值转化为控件中的尺寸位置
if (getWidth() > getHeight()) {
position = progress * getWidth();
} else {
position = progress * getHeight();
}
}
invalidate();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
//将进度值转化为控件中的尺寸位置
if (getWidth() > getHeight()) {
position = progress * getWidth();
} else {
position = progress * getHeight();
}
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public interface OnSliderChangeListener{
void progressChanged(float progress);
}
public void setOnSliderChangeListener(OnSliderChangeListener onSliderChangeListener) {
this.onSliderChangeListener = onSliderChangeListener;
}
}
DrawBoardView
public class DrawBoardView extends View {
private ArrayList<Graph> graphs;//操作的
private ArrayList<Graph> orginaGraphs;//原始的
private int lineColor= Color.BLACK;
private int lineSize=15;
Path mPath;
public DrawBoardView(Context context) {
super(context);
init();
}
public DrawBoardView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
//初始化数组
graphs=new ArrayList<>();
orginaGraphs=new ArrayList<>();
setBackgroundColor(Color.WHITE);
}
@Override
protected void onDraw(Canvas canvas) {
//遍历数组
Iterator<Graph> iterator=graphs.iterator();
while (iterator.hasNext()){
//从集合中获取一个图形对象
Graph line=iterator.next();
//绘制图形
canvas.drawPath(line.path,line.paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//创建当前这条线的Paint,Path
Paint mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(lineColor);
mPaint.setStrokeWidth(lineSize);
mPaint.setStyle(Paint.Style.STROKE);
mPath=new Path();
//设置起点
mPath.moveTo(event.getX(),event.getY());
//保存信息
Graph temp=new Graph(mPaint,mPath);
graphs.add(temp);
orginaGraphs.add(temp);
break;
case MotionEvent.ACTION_MOVE:
//连接Path终点到当前触摸点的线
mPath.lineTo(event.getX(),event.getY());
break;
case MotionEvent.ACTION_UP:
invalidate();
break;
}
return true;
}
//创建私有类来管理图形的画板和路径
private class Graph{
Paint paint;
Path path;
public Graph(Paint paint,Path path){
this.paint=paint;
this.path=path;
}
}
public int getLineSize() {
return lineSize;
}
public void setLineSize(int lineSize) {
this.lineSize = lineSize;
}
public int getLineColor() {
return lineColor;
}
public void setLineColor(int lineColor) {
this.lineColor = lineColor;
}
//撤销
public void removeLast(){
if (graphs.size()>0){
graphs.remove(graphs.size()-1);
invalidate();
}
}
//清空
public void removeAll(){
graphs.clear();
invalidate();
}
//还原上一步
public void returnTolastStep(){
//判断缓存中是否有
if (graphs.size()<orginaGraphs.size()){
//获取上一步的索引值
int index=graphs.size()-1+1;
//从缓存中获取index 添加到操作数组中
graphs.add(orginaGraphs.get(index));
//刷新
invalidate();
}
}
}
最后我用此画板项目制作了一幅杰作,不要表扬,不要羡慕,其实你也可以!