在活动中实现onclicklistener不会产生任何结果

6l7fqoea  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(320)

在我的活动中,我试图使用 onClick 而不是重写 onTouchEvent 方法,当我实现 OnClickListener 并覆盖 onClick 方法,我没有结果。但是,使用 onTouchEvent 方法和应用我的逻辑就行了。
我能用什么 onClick 在我的活动中?

public class PixelParticle extends View{

    private float locationX;
    private float locationY;
    private int sizeX;
    private int sizeY;
    private Paint color;
    private Rect rect;
    private int paintcolor;

    public PixelParticle(Context context, float locationX, float locationY, int sizeX, int sizeY, int paintcolor) {
        super(context);

        this.locationX = locationX;
        this.locationY = locationY;
        this.sizeX = sizeX;
        this.sizeY = sizeY;
        this.paintcolor = paintcolor;

        rect = new Rect();
        color = new Paint();

    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);

        Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, sizeX, sizeY, true);
        canvas.drawBitmap(myBitmap,locationX -(resizedBitmap.getWidth() / 2) ,locationY - (resizedBitmap.getHeight() / 2), null);

    }

}

..

public class MainActivity extends Activity implements OnClickListener{

    private int pixelColor;
    private float x;
    private float y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //removes title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //sets view to full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

        public boolean onTouchEvent(MotionEvent event) {
            x = event.getX();
            y = event.getY();
            pixelColor = Color.RED;
            PixelParticle pp = new PixelParticle(this, x, y, 50, 50, pixelColor);
            setContentView(pp);

            return false;
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        } 

}

当我发表评论时 onTouchEvent 并在 onClick ,什么也没发生。
以及xml布局

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:onClick="onClick" >

</RelativeLayout>
w8ntj3qf

w8ntj3qf1#

您可以检查触发了哪种触摸事件,并使用该事件确定是否重新绘制。

public boolean onTouchEvent(MotionEvent event) {

    if(event.getAction() == MotionEvent.ACTION_UP) {
        // Do your drawing here..
        return false;
    }
    return true;
}

希望有帮助。

相关问题