为什么材料颜色选择器不起作用?

时间:2016-03-09 17:23:30

标签: java android

我正在寻找一个颜色选择器,偶然发现了一个非常好的选择器。 Github页面是here。但是,在尝试使用它时,logcat会发出错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.education.clearmind.tutoria, PID: 6604
                  android.util.AndroidRuntimeException: requestFeature() must be called before adding content
                  at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:353)
                  at android.app.Dialog.requestWindowFeature(Dialog.java:1066)
                  at com.pes.androidmaterialcolorpickerdialog.ColorPicker.onCreate(ColorPicker.java:94)
                  at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
                  at android.app.Dialog.show(Dialog.java:274)
                  at com.education.clearmind.tutoria.WhiteboardActivity.openColorPicker(WhiteboardActivity.java:58)
                  at com.education.clearmind.tutoria.WhiteboardActivity$1.onClick(WhiteboardActivity.java:49)
                  at android.view.View.performClick(View.java:4789)
                  at android.view.View$PerformClick.run(View.java:19881)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5292)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

以下是 WhiteboardActivity 中应该实现颜色选择器的代码:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.education.clearmind.tutoria.ui.views.WhiteboardSurface;
import com.pes.androidmaterialcolorpickerdialog.ColorPicker;

import butterknife.Bind;
import butterknife.ButterKnife;

public class WhiteboardActivity extends Activity {

    private Button mChangeBrushColorButton;
    private ColorPicker mColorPicker;

    private Button mOkColorButton;

    private int optionsMenuNumberClicks = 0;

    @Bind(R.id.whiteboardRelativeLayout) RelativeLayout mWhiteboardRelativeLayout;
    @Bind(R.id.whiteboardSurface) WhiteboardSurface mWhiteboardSurface;
    @Bind(R.id.floatingActionBar) ImageButton mFloatingActionBarButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_whiteboard);

        initializeScreenUI();
        onFloatingActionBarClick();
    }

    public void initializeScreenUI() {
        ButterKnife.bind(this);
        mColorPicker = new ColorPicker(WhiteboardActivity.this, Color.RED, Color.GREEN, Color.BLUE);
        mOkColorButton = (Button) mColorPicker.findViewById(R.id.okColorButton);
    }

    public void onFloatingActionBarClick() {
        mFloatingActionBarButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openColorPicker();
            }
        });
    }

    public void openColorPicker() {
        mColorPicker.show();
        mOkColorButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectedColorRGB = mColorPicker.getColor();
                mWhiteboardSurface.changeBrushColor(selectedColorRGB);
                mColorPicker.dismiss();
            }
        });
    }
}

以下是 WhiteboardSurface View 中的代码:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.education.clearmind.tutoria.model.Brush;

/**
 * Created by tomfinet on 06/03/16.
 */
public class WhiteboardSurface extends View {

    private Paint mPaint = new Paint();
    private Path mPath = new Path();
    private Brush mBrush = new Brush(mPaint, mPath);

    // Constructor sets  up the paint and inherits the superclasses constructor
    public WhiteboardSurface(Context context, AttributeSet attrs) {
        super(context, attrs);
        // Default brush setup
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // draws the path with the selected paint
        canvas.drawPath(mBrush.getPath(), mBrush.getPaint());
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        /**
         * gets the xPos and yPos of the touch on the screen and saves them in variables so
         * that they can be manipulated into drawing lines and so on.
         */
        float xPos = motionEvent.getX();
        float yPos = motionEvent.getY();

        //TODO: Add the functionality to open options menu on touch event
        switch(motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath.moveTo(xPos, yPos);
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(xPos, yPos);
                break;
            default:
                return false;
        }

        invalidate();
        return true;
    }

    public void changeBrushColor(int colorRGB) {
        mPaint.setColor(colorRGB);
    }
}

有人可以告诉我我做错了什么以及如何解决它。 如果您还有其他需求,请随便询问。

干杯。

0 个答案:

没有答案