ColorPicker无法正常工作

时间:2018-07-04 19:12:56

标签: java android

因此,基本上我的应用程序中有一个绘画活动设置,该设置使用户可以用笔绘画并清除所做的事情。但是,当我更改笔的颜色时,无论我选择哪种颜色,它都将变为白色,并且只有笔触的笔尖才是正确的颜色。最初,颜色设置为黑色并且可以正常工作,但是当更改笔的颜色时,就会发生错误。

P.s。当我在画布上更改背景颜色时,它会正确更改

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_horizontal_margin"
tools:context=".DrawActivity"
>

<com.mukesh.DrawingView
    android:id="@+id/scratch_pad"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/buttons"
    />

<LinearLayout
    android:id="@+id/buttons"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/size_layout"
    android:gravity="center"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:id="@+id/save_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Save Image"
            />
        <Button
            android:id="@+id/load_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Load Image"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:id="@+id/eraser_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Eraser"
            />
        <Button
            android:id="@+id/background_color_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Background Color"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:id="@+id/pen_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Pen"
            />
        <Button
            android:id="@+id/pen_color_button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Pen Color"
            />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:id="@+id/size_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textNoSuggestions"
        android:text="Pen Size"
        />
    <SeekBar
        android:id="@+id/pen_size_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textNoSuggestions"
        android:text="Eraser Size"
        />
    <SeekBar
        android:id="@+id/eraser_size_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>
</RelativeLayout>

DrawActivity

public class DrawActivity extends AppCompatActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {

private Button mSaveButton, mPenButton, mEraserButton, mPenColorButton, mBackgroundColorButton,
        mLoadButton;

private DrawingView mDrawingView;
private SeekBar mPenSizeSeekbar, mEraserSeekbar;

private SensorManager mSensorManager;
private Sensor mAccelerometer;
private ShakeEvent mShakeDetector;

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_draw);
    setTitle("Draw");
    initializeUI();
    setListeners();


    //shake feature to restart paint canvas

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mShakeDetector = new ShakeEvent();
    mShakeDetector.setOnShakeListener(new ShakeEvent.OnShakeListener() {

        @Override
        public void onShake(int count) {
            /*
             * The following method, "handleShakeEvent(count):" is a stub //
             * method you would use to setup whatever you want done once the
             * device has been shook.
             */

            Intent intent = getIntent();
            finish();
            Toast.makeText(DrawActivity.this, "Paint canvas cleared", Toast.LENGTH_SHORT).show();
            startActivity(intent);
        }

    });
}

@Override
public void onResume() {
    super.onResume();
    // Add the following line to register the Session Manager Listener onResume
    mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
}

@Override
public void onPause() {
    // Add the following line to unregister the Sensor Manager onPause
    mSensorManager.unregisterListener(mShakeDetector);
    super.onPause();

}

private void setListeners() {
    mSaveButton.setOnClickListener(this);
    mPenButton.setOnClickListener(this);
    mEraserButton.setOnClickListener(this);
    mPenColorButton.setOnClickListener(this);
    mBackgroundColorButton.setOnClickListener(this);
    mPenSizeSeekbar.setOnSeekBarChangeListener(this);
    mEraserSeekbar.setOnSeekBarChangeListener(this);
    mLoadButton.setOnClickListener(this);
}

private void initializeUI() {
    mDrawingView = (DrawingView) findViewById(R.id.scratch_pad);
    mSaveButton = (Button) findViewById(R.id.save_button);
    mLoadButton = (Button) findViewById(R.id.load_button);
    mPenButton = (Button) findViewById(R.id.pen_button);
    mEraserButton = (Button) findViewById(R.id.eraser_button);
    mPenColorButton = (Button) findViewById(R.id.pen_color_button);
    mBackgroundColorButton = (Button) findViewById(R.id.background_color_button);
    mPenSizeSeekbar = (SeekBar) findViewById(R.id.pen_size_seekbar);
    mEraserSeekbar = (SeekBar) findViewById(R.id.eraser_size_seekbar);
}

@Override public void onClick(View view) {
    switch (view.getId()) {
        case R.id.save_button:
            mDrawingView.saveImage(Environment.getExternalStorageDirectory().toString(), "test",
                    Bitmap.CompressFormat.PNG, 100);
            break;
        case R.id.load_button:
            mDrawingView.loadImage(BitmapFactory.decodeResource(getResources(), R.raw.image));
            Log.d("saveImage", "quality cannot better that 100");
            break;
        case R.id.pen_button:
            mDrawingView.initializePen();
            break;
        case R.id.eraser_button:
            mDrawingView.initializeEraser();
            break;
        case R.id.pen_color_button:
            final ColorPicker colorPicker = new ColorPicker(DrawActivity.this, 100, 100, 100);
            colorPicker.setCallback(
                    new ColorPickerCallback() {
                        @Override public void onColorChosen(int color) {
                            mDrawingView.setPenColor(color);
                            colorPicker.dismiss();
                        }
                    });
            colorPicker.show();
            break;
        case R.id.background_color_button:
            final ColorPicker backgroundColorPicker = new ColorPicker(DrawActivity.this, 100, 100, 100);
            backgroundColorPicker.setCallback(
                    new ColorPickerCallback() {
                        @Override public void onColorChosen(int color) {
                            mDrawingView.setBackgroundColor(color);
                            backgroundColorPicker.dismiss();
                        }
                    });
            backgroundColorPicker.show();
            break;
    }
}

@Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
    switch (seekBar.getId()) {
        case R.id.pen_size_seekbar:
            mDrawingView.setPenSize(i);
            break;
        case R.id.eraser_size_seekbar:
            mDrawingView.setEraserSize(i);
            break;
    }
}

@Override public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override public void onStopTrackingTouch(SeekBar seekBar) {
}
}

0 个答案:

没有答案