Android可绘制画布 - 更改边框设计和颜色

时间:2013-08-20 10:32:47

标签: android drawing android-canvas

我是Canvas的新手,在Android中绘图,我只是接受了代码from this website我设法调整它,但是我发现很难改变边框样式和边框颜色。

我希望边框只是一个简单的黑色方形边框,没有奇特的波浪和色谱。

这是onCreate函数中的代码,我已经设法尽量减少边框的波动,但仍然有微小的颠簸和转动。

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);
     setContentView(R.layout.activity_main);

     float[] outerR = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 };

     RectF inset = new RectF(6, 6, 6, 6);
     float[] innerR = new float[] { 12, 12, 0, 0, 12, 12, 0, 0 };

     mDrawables = new ShapeDrawable[2];

     mDrawables[0] = new ShapeDrawable(new RoundRectShape(outerR, inset, null));
     //mDrawables[0].getPaint().setShader(makeSweep());

     mDrawables[1] = new ShapeDrawable(new RoundRectShape(outerR, inset, innerR));
     //mDrawables[1].getPaint().setShader(makeLinear());

     PathEffect mPathEffect1 = new DiscretePathEffect(1, 1);
     PathEffect mPathEffect2 = new CornerPathEffect(1);

     mDrawables[0].getPaint().setPathEffect(new ComposePathEffect(mPathEffect2, mPathEffect1));
     RelativeLayout layout = (RelativeLayout) findViewById(R.id.myDrawing);
     layout.setBackgroundDrawable(mDrawables[0]);
     activity = this; 


     //mView = new DrawingView(this);
     mView = new SignatureView(activity, null);

     layout.addView(mView, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

     init();

     mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
     mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
 }

抱歉,我知道这可能是一项简单的任务,但我完全被难倒了。

编辑 - 好吧我设法让它变成黑色,我只是删除了mDrawables [0/1] .getPaint()行并且它变成了黑色,我现在唯一的问题就是让它完全笔直。

1 个答案:

答案 0 :(得分:0)

想出来。

对于边框的颜色,请注释掉以下行: mDrawables[0].getPaint().setShader(makeSweep());mDrawables[1].getPaint().setShader(makeLinear());

之后,问题是DiscretePathEffect。将该行代码更改为: PathEffect mPathEffect1 = new DiscretePathEffect(1, 0);

显然,DiscretePathEffect的两个参数决定了这条线的“波浪形”。