0

I am doing app like drawing app... All thing is working good.. But Now i want to add width of pencil..

For that ii have done

mPaint.setStrokeWidth(seekbarvalue);

But the problem is that it applying to untire drawing but i want to apply it on the next drawing only... What i should so?

My code is

private void touch_start(float x, float y) {
            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;

        }

        private void touch_move(float x, float y) {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
                mX = x;
                mY = y;
            }
        }

        private void touch_up() {
            mPath.lineTo(mX, mY);

            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            mPath = new Path();
            paths.add(new PathPoints(mPath, color, false));

        }

        private void drawText(int x, int y) {
            Log.v(TAG, "Here");
            Log.v(TAG, "X " + x + " Y " + y);
            this.x = x;
            this.y = y;
            paths.add(new PathPoints(color, textToDraw, true, x, y));
            // mCanvas.drawText(textToDraw, x, y, mPaint);
        }

        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            isTouched = true;
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (!isTextModeOn) {
                    touch_start(x, y);
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (!isTextModeOn) {
                    touch_move(x, y);
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isTextModeOn) {
                    drawText((int) x, (int) y);
                    invalidate();
                } else {
                    touch_up();
                    invalidate();
                }
                break;
            }
            return true;
        }

And this is seekbaar

sekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                if (fromUser) {

                 mPaint.setStrokeWidth(progress);
                }
            }
        });

But it applies on entire drawing

and this is my ondraw mwthod

protected void onDraw(Canvas canvas1) {
            canvas = canvas1;

            // === canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            invalidate();
            mPaint.setStrokeWidth(GlobalVar.getSeekvalue());
            invalidate();
            canvas.drawBitmap(Transparent, 0, 0, mBitmapPaint);
            c2.drawBitmap(Bitmap2, 0, 0, mBitmapPaint);
            // canvas.drawBitmap(Transparent, 0, 0, mBitmapPaint);
            // canvas.drawBitmap(Transparent, 0, 0, null);
            // c2.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            for (PathPoints p : paths) {
                mPaint.setColor(p.getColor());

                Log.v("", "Color code : " + p.getColor());
                if (p.isTextToDraw()) {
                    canvas.drawText(p.textToDraw, p.x, p.y, mPaint);
                } else {
                    // if(isTouched)
                    // {
                    // canvas.drawPath(p.getPath(), mPaint);

                    c2.drawPath(p.getPath(), mPaint);

what is i need to do?

4

2 回答 2

1

在 PathPoint 对象中设置另一个字段并将其命名为 strokWidth 和:

private void touch_up() {
        mPath.lineTo(mX, mY);
        int strokWidth = GlobalVar.getSeekvalue();  
        // commit the path to our offscreen
        mPaint.setStrokeWidth(strokWidth);
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath = new Path();
        paths.add(new PathPoints(mPath, color, false));

    }

并改变每一个

paths.add(new PathPoints(mPath, color, false));

paths.add(new PathPoints(mPath, color, false,strokWidth));

现在在 ondraw 方法和 for 循环中:

for (PathPoints p : paths) {
            mPaint.setColor(p.getColor());
            mPaint.setStrokeWidth(p.getStrokWidth());

现在我认为这些行没用:

invalidate();
mPaint.setStrokeWidth(GlobalVar.getSeekvalue());
invalidate();
于 2014-08-07T08:21:36.503 回答
1

您可以使用具有不同笔画的油漆池(重要:)将其制成离散的部分。宽度为 4 像素、8 像素、12 像素、14 像素、18 像素和 22 像素。

 Paint basePaint=new Paint();

 SparseArray<Paint>poolPaint=new SparseArray<Paint>();

 Paint currPaint; //<- that's your paint that change width stroke on progressbar seek




 sekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
             // make it discrete not like here for every single width is not worthy
            int widthPaint=progress;
            if (fromUser) {
                if((currPaint=poolPaint.get(widthPaint))==null){
                      Paint newPaint=  new Paint(basePaint).setStrokeWidth(widthPaint)
                      poolPaint.put(widthPaint,newPaint);
                      currPaint=newPaint;  
                 }


            }
        }
    });
于 2014-08-07T07:54:16.357 回答