0

在此处输入图像描述我正在尝试使用 SPen 的颜色选择器更改请求,我不想使用他们提供的我想使用我自己的。

这是我下面的代码,颜色选择器出现了,但是当我选择一种颜色时,它不会改变 PEN 的输入

    public class numberTrace extends Activity implements OnInitListener, ColorPickerDialog.OnColorChangedListener {


             private Paint mPaint; 

Context mContext = null;
private RelativeLayout mCanvasContainer;
private SCanvasView mSCanvas;
private ImageView mEraserBtn;
private ImageView mNextBtn;
private ImageView mPenBtn;
public int currentNumber;
int[] myImageList = { R.drawable.numbers0, R.drawable.numbers1,
        R.drawable.numbers2, R.drawable.numbers3, R.drawable.numbers4,
        R.drawable.numbers5, R.drawable.numbers6, R.drawable.numbers7,
        R.drawable.numbers8, R.drawable.numbers9 };

int cIndex = 0;
float speechRate = 1 / 4;
private TextToSpeech tts;
String[] speakNumbers = { "Number zero", "Number one", "Number two", "Number three", "Number four",
        "Number five", "Number six", "Number seven", "Number eight", "Number nine" }; 

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.trace); // This is calling the layout in the
                                    // main.xml file if I change the
                                    // main.xml file name then I would

    // Create the SCanvasView
    tts = new TextToSpeech(this, this);
    mContext = this;
    mCanvasContainer = (RelativeLayout) findViewById(R.id.canvas_container);
    mSCanvas = new SCanvasView(mContext);
    mCanvasContainer.addView(mSCanvas);

    mEraserBtn = (ImageView) findViewById(R.id.eraserBtn);
    mNextBtn = (ImageView) findViewById(R.id.nextBtn);
    mPenBtn = (ImageView) findViewById(R.id.penBtn);


    mPaint = new Paint();
   // on button click



    // setting the background of the canvas
    mSCanvas.setBackgroundResource(R.drawable.numbers0);

    SCanvasInitializeListener mSCanvasListener = new SCanvasInitializeListener() {

        public void onInitialized() {
            // --------------------------------------------
            // Start SCanvasView/CanvasView Task Here
            // --------------------------------------------
            // Application Identifier Setting
            mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_PEN);
            // Get the direction of contents(Canvas)
            if (mSCanvas.getWidth() > mSCanvas.getHeight()) {
            } else {
            }
        }
    };
    // ------------------------------------------------
    // History Change
    // ------------------------------------------------
    HistoryUpdateListener mHistoryListener = new HistoryUpdateListener() {

        public void onHistoryChanged(boolean undoable, boolean redoable) {
            mNextBtn.setEnabled(undoable);

        }
    };
    // ------------------------------------------------
    // OnSettingStrokeChangeListener Listener
    // ------------------------------------------------
    SettingStrokeChangeListener mSettingStrokeChangeListener = new SettingStrokeChangeListener() {
        public void onClearAll(boolean bClearAllCompleted) {
            // If don't set eraser mode, then change to pen mode
            // automatically.
            if (bClearAllCompleted)

                mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_ERASER);
        }

        public void onEraserWidthChanged(int eraserWidth) {
        }

        public void onStrokeColorChanged(int strokeColor) {
        }

        public void onStrokeStyleChanged(int strokeStyle) {
        }

        public void onStrokeWidthChanged(int strokeWidth) {
        }

        public void onStrokeAlphaChanged(int strokeAlpha) {
        }
    };

    mSCanvas.setSCanvasInitializeListener(mSCanvasListener);
    mSCanvas.setSettingStrokeChangeListener(mSettingStrokeChangeListener);

    mSCanvas.setColorPickerColorChangeListener(new ColorPickerColorChangeListener(){
        @Override
        public void onColorPickerColorChanged(int nColor) {


                mPaint = new Paint();

            int nCurMode = mSCanvas.getCanvasMode();
            if(nCurMode==SCanvasConstants.SCANVAS_MODE_INPUT_PEN) {
                SettingStrokeInfo strokeInfo = mSCanvas.getSettingViewStrokeInfo();
                if(strokeInfo != null) {
                    strokeInfo.setStrokeColor(mPaint.getColor());   
                    mSCanvas.setSettingViewStrokeInfo(strokeInfo);
                }   
            }


        }           
    }); 


    mNextBtn.setOnClickListener(new OnClickListener() {
        int i = 1;

        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mSCanvas.clear();
            mSCanvas.setBackgroundResource(myImageList[i]);// calling the
                                                            // array to mvoe
                                                            // to the next



            // image

            String speakNow = speakNumbers[cIndex];
            tts.setSpeechRate(speechRate);
            tts.speak(speakNow, TextToSpeech.QUEUE_FLUSH, null);
            cIndex++;
            cIndex %= speakNumbers.length;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();

            }


            i++;
            if (i >= myImageList.length) {
                i = 0;

                // erasing the drawings on the array
            }

            mNextBtn.setEnabled(mSCanvas.isUndoable());
        }

    });

    mEraserBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int nBtnID = mEraserBtn.getId();
            // If the mode is not changed, open the setting view. If the
            // mode is same, close the setting view.
            if (nBtnID == mEraserBtn.getId()) {
                if (mSCanvas.getCanvasMode() == SCanvasConstants.SCANVAS_MODE_INPUT_ERASER) {

                    mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_ERASER);
                } else {
                    mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_ERASER);

                    mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_ERASER);
                }
            }

        }
    });

    mPenBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {


            mPaint = new Paint();
            new ColorPickerDialog(numberTrace.this, numberTrace.this, mPaint.getColor()).show();
            boolean bIsColorPickerMode = !mSCanvas.isColorPickerMode(); 
            mSCanvas.setColorPickerMode(bIsColorPickerMode);

            if (mPenBtn.equals(mPenBtn)) {
                mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_PEN);
                mSCanvas.getColorPickerColor(mPaint.getColor(), mPaint.getColor());
            }




        }
    });

    // Caution:
    // Do NOT load file or start animation here because we don't know canvas
    // size here.
    // Start such SCanvasView/CanvasView Task at onInitialized() of
    // SCanvasInitializeListener

    mSCanvas.setSCanvasInitializeListener(mSCanvasListener);
    mSCanvas.setHistoryUpdateListener(mHistoryListener);
    mNextBtn.setEnabled(false);
    // Caution:
    // Do NOT load file or start animation here because we don't know canvas
    // size here.
    // Start such SCanvasView/CanvasView Task at onInitialized() of
    // SCanvasInitializeListener

    mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_PEN);
    mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_ERASER);

    if (mSCanvas.getCanvasMode() == SCanvasConstants.SCANVAS_MODE_INPUT_PEN)
        ;
    {
        // Processed when the current mode of the SCanvasView is the Pen
        // mode

    }

}




public void onInit(int status) {
    // TODO Auto-generated method stub
    tts.setLanguage(Locale.US);
}
public void onDestroy() {
    super.onDestroy();
    tts.shutdown();
}




@Override
public void colorChanged(int color) {
    // TODO Auto-generated method stub
    SettingStrokeInfo strokeInfo = mSCanvas.getSettingViewStrokeInfo();
    if(strokeInfo != null) {
        strokeInfo.setStrokeColor(mPaint.getColor());

    }
    }

下面将是 colorPicker 类

                public class ColorPickerDialog extends Dialog {

                 public interface OnColorChangedListener {
                  void colorChanged(int color);
                  }

                     private OnColorChangedListener mListener;
                        private int mInitialColor;

                    private static class ColorPickerView extends View {
                    private Paint mPaint;
                    private Paint mCenterPaint;
                   private final int[] mColors;
                   private OnColorChangedListener mListener;

ColorPickerView(Context c, OnColorChangedListener l, int color) {
    super(c);
    mListener = l;
    mColors = new int[] {
        0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
        0xFFFFFF00, 0xFFFF0000
    };
    Shader s = new SweepGradient(0, 0, mColors, null);

    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setShader(s);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(32);

    mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mCenterPaint.setColor(color);
    mCenterPaint.setStrokeWidth(5);
}

private boolean mTrackingCenter;
private boolean mHighlightCenter;

@Override
protected void onDraw(Canvas canvas) {
    float r = CENTER_X - mPaint.getStrokeWidth()*0.5f;

    canvas.translate(CENTER_X, CENTER_X);

    canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
    canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint);

    if (mTrackingCenter) {
        int c = mCenterPaint.getColor();
        mCenterPaint.setStyle(Paint.Style.STROKE);

        if (mHighlightCenter) {
            mCenterPaint.setAlpha(0xFF);
        } else {
            mCenterPaint.setAlpha(0x80);
        }
        canvas.drawCircle(0, 0,
                          CENTER_RADIUS + mCenterPaint.getStrokeWidth(),
                          mCenterPaint);

        mCenterPaint.setStyle(Paint.Style.FILL);
        mCenterPaint.setColor(c);
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(CENTER_X*2, CENTER_Y*2);
}

private static final int CENTER_X = 100;
private static final int CENTER_Y = 100;
private static final int CENTER_RADIUS = 32;

private int floatToByte(float x) {
    int n = java.lang.Math.round(x);
    return n;
}
private int pinToByte(int n) {
    if (n < 0) {
        n = 0;
    } else if (n > 255) {
        n = 255;
    }
    return n;
}

private int ave(int s, int d, float p) {
    return s + java.lang.Math.round(p * (d - s));
}

private int interpColor(int colors[], float unit) {
    if (unit <= 0) {
        return colors[0];
    }
    if (unit >= 1) {
        return colors[colors.length - 1];
    }

    float p = unit * (colors.length - 1);
    int i = (int)p;
    p -= i;

    // now p is just the fractional part [0...1) and i is the index
    int c0 = colors[i];
    int c1 = colors[i+1];
    int a = ave(Color.alpha(c0), Color.alpha(c1), p);
    int r = ave(Color.red(c0), Color.red(c1), p);
    int g = ave(Color.green(c0), Color.green(c1), p);
    int b = ave(Color.blue(c0), Color.blue(c1), p);

    return Color.argb(a, r, g, b);
}

private int rotateColor(int color, float rad) {
    float deg = rad * 180 / 3.1415927f;
    int r = Color.red(color);
    int g = Color.green(color);
    int b = Color.blue(color);

    ColorMatrix cm = new ColorMatrix();
    ColorMatrix tmp = new ColorMatrix();

    cm.setRGB2YUV();
    tmp.setRotate(0, deg);
    cm.postConcat(tmp);
    tmp.setYUV2RGB();
    cm.postConcat(tmp);

    final float[] a = cm.getArray();

    int ir = floatToByte(a[0] * r +  a[1] * g +  a[2] * b);
    int ig = floatToByte(a[5] * r +  a[6] * g +  a[7] * b);
    int ib = floatToByte(a[10] * r + a[11] * g + a[12] * b);

    return Color.argb(Color.alpha(color), pinToByte(ir),
                      pinToByte(ig), pinToByte(ib));
}

private static final float PI = 3.1415926f;

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX() - CENTER_X;
    float y = event.getY() - CENTER_Y;
    boolean inCenter = java.lang.Math.sqrt(x*x + y*y) <= CENTER_RADIUS;

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mTrackingCenter = inCenter;
            if (inCenter) {
                mHighlightCenter = true;
                invalidate();
                break;
            }
        case MotionEvent.ACTION_MOVE:
            if (mTrackingCenter) {
                if (mHighlightCenter != inCenter) {
                    mHighlightCenter = inCenter;
                    invalidate();
                }
            } else {
                float angle = (float)java.lang.Math.atan2(y, x);
                // need to turn angle [-PI ... PI] into unit [0....1]
                float unit = angle/(2*PI);
                if (unit < 0) {
                    unit += 1;
                }
                mCenterPaint.setColor(interpColor(mColors, unit));
                invalidate();
            }
            break;
        case MotionEvent.ACTION_UP:
            if (mTrackingCenter) {
                if (inCenter) {
                    mListener.colorChanged(mCenterPaint.getColor());
                }
                mTrackingCenter = false;    // so we draw w/o halo
                invalidate();
            }
            break;
    }
    return true;
   }
  }

             public ColorPickerDialog(Context context,
                     OnColorChangedListener listener,
                     int initialColor) {
                     super(context);

                     mListener = listener;
                     mInitialColor = initialColor;
                 }

                     @Override
                    protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    OnColorChangedListener l = new OnColorChangedListener() {
                    public void colorChanged(int color) {
                     mListener.colorChanged(color);
                     dismiss();
       }
     };

       setContentView(new ColorPickerView(getContext(), l, mInitialColor));
       setTitle("Pick a Color");
      }

     }

我需要更改什么以及在哪里更改,以便 SPen 以 ColoPickerDialog 类选择的颜色写入?

再次一切正常,只需选择一种颜色,颜色文字就保持黑色。

4

1 回答 1

0

The way this was done was by Making a new settings View because as you can see above it was trying to get the settings view that was there, but I was not using the samsung settings view I was using my own color picking dialog. was trying to change the color of the stroke. The way this was done was by implementing the code below.

            @Override
public void colorChanged(int color) {
    // TODO Auto-generated method stub
    mPaint.setColor(color);
    final int pColor = mPaint.getColor();

    mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_PEN);



    int nCurMode = mSCanvas.getCanvasMode();
    if(nCurMode==SCanvasConstants.SCANVAS_MODE_INPUT_PEN) 
    {

                SettingStrokeInfo strokeInfo = new SettingStrokeInfo();



                    speakNow.setBackgroundColor(pColor);
                    strokeInfo.setStrokeColor(pColor);  

                    mSCanvas.setSettingStrokeInfo(strokeInfo);



    }


}

The Pen Button

                mPenBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (mPenBtn.equals(mPenBtn)) {
                mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_PEN);

            }
        mPaint = new Paint();

    new ColorPickerDialog(numberTrace.this, numberTrace.this, mPaint.getColor()).show();


            }
    });

That should help anyone else who is having the same problem.

于 2013-11-07T02:26:28.490 回答