Speed ​​up image animation on Android

When I ran this code it didn’t do anything, I’m pretty sure this event is fired when I touch the button. but it doesn't change the opacity of the image.

View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(R.id.imgTest, "alpha", 0.2f);
        fadeAltAnim.start();
    }
};


findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);

      

Is there something wrong with my code?

+3


source to share


4 answers


Try this code using ViewPropertyAnimator:

View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener()   {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {

    view.animate().alpha(0.2f).setDuration(1000);
    }
};

      

It's always a good idea to set a duration, so the animation knows how long it needs to run.

EDIT: you might want to attach it to the MotionEvent if you are using an onTouchListener, something like:



View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener()   {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if(motionEvent == MotionEvent.ACTION_DOWN)
        view.animate().alpha(0.2f).setDuration(1000);
    }
}; 

      

EDIT 2:

If you want to use the button "It is best to use OnClickListener instead of onTouchListener", if you want to attach an image, you must initiate it (for example, in an ImageView):

Button button = (Button) findViewById(R.id.your_button);
ImageView imageView = (ImageView) findViewById(R.id.imgTest);

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            imageView.animate().alpha(0.2f).setDuration(1000);

    }
};

      

+4


source


This is because you are passing the identifier when the method requires it View

. Try going to View

.

So basically change ObjectAnimator.ofFloat(R.id.imgTest, "alpha", 0.2f);

toObjectAnimator.ofFloat(view, "alpha", 0.2f);



Here's an example

findViewById(R.id.image).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(v, "alpha", 0.2f);
            fadeAltAnim.start();
        }
});

      

+2


source


Is there a reason why you are using ObjectAnimator? You can:

AlphaAnimation anim = new AlphaAnimation(startAlphaValue, endAlphaValue);
anim.setDuration(duration);
view.startAnimation(anim);

      

0


source


**try this** 

public class Animationtest extends Activity {

private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    imageView = (ImageView)findViewById(R.id.text);
    setAnimation();
}
private void setAnimation(){
    imageView.setOnTouchListener(new OnTouchListener() {

        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "touch", Toast.LENGTH_LONG).show();
             ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(imageView, "alpha", 0.2f);
                fadeAltAnim.start();
            return false;
        }
    });

}

      

}

0


source







All Articles