我在我的应用程序中使用以下用于绘画的CustomVIEw:
package com.test.testing;import androID.content.Context;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Paint;import androID.graphics.Path;import androID.graphics.Paint.Style;import androID.vIEw.MotionEvent;import androID.Widget.TextVIEw;public class CustomVIEw extends TextVIEw { Paint paint; Path path; float x = 0; float y = 0; private int cWhite = color.WHITE; public CustomVIEw(Context context) { super(context); paint = new Paint(); path= new Path(); paint.setAlpha(255); paint.setcolor(cWhite); paint.setStyle(Style.stroke); paint.setstrokeWIDth(20); } protected voID onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawPath(path,paint); canvas.drawCircle(x, y, 10, paint); } public boolean ontouchEvent(MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: path.moveto(event.getX(), event.getY()); path.lineto(event.getX(), event.getY()); break; case MotionEvent.ACTION_MOVE: x = event.getX(); y = event.getY(); path.lineto(x, y); invalIDate(); break; case MotionEvent.ACTION_UP: path.lineto(event.getX(), event.getY()); break; case MotionEvent.ACTION_CANCEL: break; default: break; } return true; }}
我设置了一个FrameLayout,它保留了用于绘制XML的画布:
<FrameLayout androID:ID="@+ID/vIEwd" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="3" androID:orIEntation="vertical" ></FrameLayout>
我在我的Activity中调用以获取视图以使用户能够绘制:
layout = (FrameLayout)findVIEwByID(R.ID.vIEwd);//layout.removeAllVIEws();vIEw = new CustomVIEw(Activity.this);vIEw.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));layout.addVIEw(vIEw);
我有不同的颜色选项,用户可以选择在我的Activity中的对话框中更改颜色描边颜色:
public voID colorHandle() { // custom dialog final Dialog dialog = new Dialog(this); dialog.setContentVIEw(R.layout.colorlayout); dialog.setTitle("Choose a Drawing color"); button btnWH = (button) dialog.findVIEwByID(R.ID.btnWhite); button btnBL = (button) dialog.findVIEwByID(R.ID.btnBlack); button btnBLU = (button) dialog.findVIEwByID(R.ID.btnBlue); button btnCY = (button) dialog.findVIEwByID(R.ID.btnCyan); button btnDG = (button) dialog.findVIEwByID(R.ID.btnDkGray); button btnGR = (button) dialog.findVIEwByID(R.ID.btnGray); button btnGRE = (button) dialog.findVIEwByID(R.ID.btnGreen); button btnLG = (button) dialog.findVIEwByID(R.ID.btnLtGray); button btnMG = (button) dialog.findVIEwByID(R.ID.btnmagenta); button btnRD = (button) dialog.findVIEwByID(R.ID.btnRed); button btnYE = (button) dialog.findVIEwByID(R.ID.btnYellow); if (btnWH != null) { btnWH.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { vIEw.paint.setcolor(color.WHITE); dialog.dismiss(); } }); } if (btnBL != null) { btnBL.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { vIEw.paint.setcolor(color.BLACK); dialog.dismiss(); } }); } if (btnBLU != null) { btnBLU.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { vIEw.paint.setcolor(color.BLUE); dialog.dismiss(); } }); } if (btnCY != null) { btnCY.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { vIEw.paint.setcolor(color.CYAN); dialog.dismiss(); } }); } if (btnDG != null) { btnDG.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { vIEw.paint.setcolor(color.DKGRAY); dialog.dismiss(); } }); } if (btnGR != null) { btnGR.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { vIEw.paint.setcolor(color.GRAY); dialog.dismiss(); } }); } if (btnGRE != null) { btnGRE.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { vIEw.paint.setcolor(color.GREEN); dialog.dismiss(); } }); } if (btnLG != null) { btnLG.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { vIEw.paint.setcolor(color.LTGRAY); dialog.dismiss(); } }); } if (btnMG != null) { btnMG.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { vIEw.paint.setcolor(color.magenta); dialog.dismiss(); } }); } if (btnRD != null) { btnRD.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { vIEw.paint.setcolor(color.RED); dialog.dismiss(); } }); } if (btnYE != null) { btnYE.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { vIEw.paint.setcolor(color.YELLOW); dialog.dismiss(); } }); } dialog.show();}
一切都很好,除非我每次选择一种新颜色,之前绘制的任何东西也会改变为新选择的颜色.无论接下来选择什么新颜色,我如何保持之前绘制的内容不变?
解决方法:
您将为每个touchEvent添加一个路径.然后使用Paint颜色的当前值绘制Path.这就是为什么你看到所有东西都用一种颜色绘制的原因.
您需要为每个颜色更改创建单独的路径和颜色,然后按顺序绘制它们,更改每个drawPath()调用的Paint的颜色
我不这么认为.突破路径并不是那么糟糕.
List<Pair<Path, Integer>> path_color_List = new ArrayList<Pair<Path,Integer>>()
然后每次你改变颜色.获取当前路径并查看.paint.getcolor并将其保存到列表中.
path_color_List.add( new Pair.create(path, vIEw.paint.getcolor());path = new Path();
然后在你的draw()中迭代path_color_List,每次都设置新的颜色
for (Pair<Path,Integer> path_clr : path_color_List ){ paint.setcolor(path_clr.second); canvas.drawPath( path_clr.first, paint);}
接下来是你拥有的最后一个drawPath()
总结以上是内存溢出为你收集整理的java – 更改颜色而不影响以前绘制的任何内容全部内容,希望文章能够帮你解决java – 更改颜色而不影响以前绘制的任何内容所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)