1

我正在android中制作绘画应用程序。但是我有一个问题我想在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;
4

1 回答 1

1

使用这个,
它对我很好

float scale = getResources().getDisplayMetrics().density;
        int sizeInDp = (int) (20*scale + 0.5f);
        width11=sizeInDp;
于 2013-08-07T07:08:18.753 回答