How to install an onclick listener to view animated images

I am developing an application with an animated representation of an image moving to the right from the center. When you click on the image, OnClick () will be called. But when I click on the screen in the path of moving the image (next to the image), OnClick () also turns off. Please tell me how to set the click audience for viewing images only. My code is:

  ll = new LinearLayout(this);
            ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            ll.setOrientation(LinearLayout.VERTICAL);

            ll.setGravity(Gravity.CENTER);
            imageView=new ImageView(getApplicationContext());
            imageView.setImageResource(R.drawable.moveimage1);
    int width=getWindowManager().getDefaultDisplay().getWidth()/2;
    System.out.println("width==="+width);
            moveLefttoRight = new TranslateAnimation(width, 0, 0, 0);
            moveLefttoRight.setDuration(3000);
            moveLefttoRight.setRepeatCount(TranslateAnimation.INFINITE);  // animation repeat count
            moveLefttoRight.setRepeatMode(2);  

            imageView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_LONG).show();
                    System.out.println("Clicked");
                }
            });

imageView.startAnimation(moveLefttoRight);
 ll.addView(imageView);

        setContentView(ll);
+4
source share
1 answer

onclick . , , , . :.

ScheduledExecutorService executor = Executors
        .newScheduledThreadPool(1);

// Execute the run() in Worker Thread every REFRESH_RATE
// milliseconds
mMoverFuture = executor.scheduleWithFixedDelay(new Runnable() {
    @Override
    public void run() {
        // TODO - implement movement logic.
        if (moveWhileOnScreen()) {
            stop(false);
        }
        else {
            postInvalidate();
        }

    }
}, 0, REFRESH_RATE, TimeUnit.MILLISECONDS);

onclick, .

0

Source: https://habr.com/ru/post/1530370/


All Articles