触摸事件在自定义扩展视图中不起作用

时间:2015-09-02 22:10:55

标签: java android

我有Activity.java和PaintView扩展View实现OnTouchListener接口。在这个程序中,我必须单击按钮(粉色和蓝色背景按钮)并选择一个形状并拖动到视图区域。

我需要帮助,我过去几天一直在努力。

     MainActivity.java
        package com.easyway2win;

        import android.app.Activity;
        import android.content.Context;
        import android.graphics.Color;
        import android.graphics.Paint;
        import android.os.Bundle;
        import android.support.v4.content.ContextCompat;
        import android.util.Log;
        import android.view.View;
        import android.widget.Button;
        import android.widget.TextView;



        public class MainActivity extends Activity {

            private Button butnPink,butnBlue;
            public PaintView paintView = null;

            @Override
            protected void onCreate(Bundle savedInstanceState) {


                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                butnPink = (Button)findViewById(R.id.pinkColor);
                butnBlue = (Button)findViewById(R.id.blueColor);
                View paintView = (View)findViewById(R.id.view1);
            }

            public void clickMe(View view){
                int intColor = 0;
                String hexColor = null;

                switch(view.getId()){

                case R.id.pinkColor:
                    intColor = getResources().getColor(R.color.pink);

                    hexColor = String.format("#%06X", (0xFFFFFF & intColor));
                    Log.d("Hi", "I am pink color code " + hexColor);

                    break;
                case R.id.blueColor:
                    intColor =  getResources().getColor(R.color.blue);
                  }

                PaintView paintView = new PaintView(this); 
                paintView.setPaintColour(intColor);

            }// end [ clickMe method ]
        }

package com.easyway2win;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;

PaintView.java

public class PaintView extends View implements OnTouchListener{

    private Paint paint;
    private Path path;

    private Bitmap buffer = null;
    private Canvas penCanvas = null;
    int colour = 0;

    public PaintView(Context context) {
        super(context);
            paint.setAntiAlias(true);
            paint.setColor(Color.WHITE);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeWidth(5f);


            // create the Paint and set its color        
            paint = new Paint();*/

           path = new Path();
    }


    public PaintView(Context context, AttributeSet attrs ){
        super(context, attrs);
    }

    public PaintView(Context context, AttributeSet attrs, int defStyle/*Context context, Rect rectangle, Paint paint*/) {
        super(context,attrs,defStyle);
        //  this.rectangle = rectangle;
        //  this.paint = paint;

    }

    public void setPaintColour(int colour){
        paint.setColor(colour);
        //Log.d("Hi", "I am in setPaintColour Method" );
    }

    @Override
    protected void onDraw(Canvas canvas) {
        /*  canvas.drawRect(rectangle,paint);*/
        canvas.drawPath(path, paint);

    }


    @Override
    public boolean onTouch(View v, MotionEvent evt) {

        int action = evt.getAction() & MotionEvent.ACTION_MASK;

        switch (action) {
        case MotionEvent.ACTION_DOWN:{
            //Toast.makeText(getContext(), "Action Down", Toast.LENGTH_LONG);
            //Log.d("Down", "Pointer Down");
            break;
        }

        case MotionEvent.ACTION_POINTER_DOWN : {
            Log.d("CV", "Other point down");
            break;
        }

        case MotionEvent.ACTION_POINTER_UP : {
            Log.d("CV", "Other point up");
            break;
        }

        case MotionEvent.ACTION_UP : {
            Log.d("CV", "Pointer up");
            break;
        }

        }
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

当您尝试在创建绘制对象之前调用其上的方法时,会发生空指针异常。需要先在PaintView构造函数中移动paint = new Paint();行,否则可以简单地在绘制声明的位置初始化它。

public class PaintView extends View implements OnTouchListener{

    private Paint paint;
    // alternate: private Paint paint = new Paint();
    private Path path;

    private Bitmap buffer = null;
    private Canvas penCanvas = null;
    int colour = 0;

    public PaintView(Context context) {
        super(context);

            // create the Paint and set its color        
            paint = new Paint();*/
            // alternate: skip this line and use the alternate above

            paint.setAntiAlias(true);
            paint.setColor(Color.WHITE);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeWidth(5f);




           path = new Path();
    }


看起来PaintView从未添加到您的布局中。该对象已创建,但未附加到UI中的任何内容。如果您对ViewGroup(LinearLayout等)有引用,则可以使用addView方法向其添加新视图。如果需要从布局中删除其他视图,请使用removeView