Reputation: 3893
When I'd ran this code, it did nothing, I'm sure that this event is called when I touch the button. but It doesn't change the opacity of imageView.
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 anything wrong with my code?
Upvotes: 3
Views: 3479
Reputation: 3503
Try this code, its using ViewPropertyAnimator :
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.animate().alpha(0.2f).setDuration(1000);
}
};
Its always good to set a duration, so the Animation knows how long its supposed to run.
EDIT : You might want to attach it to a MotionEvent if you are using 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 a Button its best to use an OnClickListener instead of onTouchListener, if you want to attach an Image you have to 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);
}
};
Upvotes: 4
Reputation: 868
**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;
}
});
}
}
Upvotes: 0
Reputation: 7081
It's because you're passing in an id when the method requires the View
. Try passing in the View
.
So basically change ObjectAnimator.ofFloat(R.id.imgTest, "alpha", 0.2f);
to ObjectAnimator.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();
}
});
Upvotes: 2
Reputation: 1398
Is there any reason you're using ObjectAnimator? Can you do:
AlphaAnimation anim = new AlphaAnimation(startAlphaValue, endAlphaValue);
anim.setDuration(duration);
view.startAnimation(anim);
Upvotes: 0