如何在android中单击Listener来增加/减少STROKE宽度

时间:2013-07-30 06:20:08

标签: android touch onclicklistener

我在android中制作绘画应用程序。但我有一个问题,我想改变STROKE clickListeners上的宽度或画笔大小增加或减少。但问题是这样 我选择新的宽度刷第一次不改变他的宽度和第二次当我 绘制画笔的宽度是改变的。所以我能做什么才能解决问题。请帮忙。 这是我的代码

int Stork_Width=0; 

        but1 = (Button) dialog.findViewById(R.id.but1);
                            // if button is clicked, close the custom dialog
                            but1.setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    //idd=mMaindialog.get(position).imagename;

                                    Stork_Width=10;
                                    mPaint.setStrokeWidth(Stork_Width);
                                    dialog.dismiss();
                                }




                            });

                           but2 = (Button) dialog.findViewById(R.id.but2);
                            // if button is clicked, close the custom dialog
                            but2.setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    Stork_Width=15;
                                    mPaint.setStrokeWidth(Stork_Width);
                                    dialog.dismiss();
                                }
                            });

                            but3 = (Button) dialog.findViewById(R.id.but3);
                            // if button is clicked, close the custom dialog
                            but3.setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    Stork_Width=25;
                                    mPaint.setStrokeWidth(Stork_Width);
                                    dialog.dismiss();
                                }
                            });



now i create the method of Brushes  :
and strore the path in Hashmap also:

private Map<Path, Integer> colorwidth = new HashMap<Path, Integer>();

private Paint createBrush(final int width) {
            // TODO Auto-generated method stub
            mPaint1 = new Paint();
            mPaint1.setColor(colorPicked);
            mPaint1.setAntiAlias(true);
            mPaint1.setDither(true);
            mPaint1.setStyle(Paint.Style.STROKE);
            mPaint1.setStrokeJoin(Paint.Join.ROUND);
            mPaint1.setStrokeCap(Paint.Cap.ROUND);
             mPaint1.setStrokeWidth(width);
            return mPaint1;
        }

ontouch activity::


@Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // touch_start(x, y);
                // invalidate();
                undonePaths.clear();
                mPath.reset();
                mPath.moveTo(x, y);
                mX = x;
                mY = y;
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                // touch_move(x, y);
                // invalidate();
                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;
                }
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                // touch_up();
                // invalidate();
                mPath.lineTo(mX, mY);
                mMaindialog.add(mPath);
                colorsMap.put(mPath, slll);
                colorwidth.put(mPath, Stork_Width);
                mPath = new Path();
                mPath.reset();
                invalidate();
                break;
            }
            return true;

1 个答案:

答案 0 :(得分:1)

使用此,
它对我很好

float scale = getResources().getDisplayMetrics().density;
        int sizeInDp = (int) (20*scale + 0.5f);
        width11=sizeInDp;