As part of a much more complex piece of functionality I’ve become stuck on the ability to show a circle becoming opaque at a certain point of the screen.
I’ve gone as far as to create a test project where my Animation code is as follows:
View mLongPressImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLongPressImage = findViewById(R.id.onPressImage);
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startAnimation(v.getX(), v.getY());
}
});
}
private AlphaAnimation onPressAnimation;
private void startAnimation(final float f, final float g) {
Log.v("", "START");
mLongPressImage.setX(f);
mLongPressImage.setY(g);
onPressAnimation = new AlphaAnimation(0f, 1f);
onPressAnimation.setDuration(1000);
onPressAnimation.setFillAfter(true);
onPressAnimation.setFillEnabled(true);
mLongPressImage.startAnimation(onPressAnimation);
}
Logging show’s the correct event’s are happening.
View looks like so:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="PRESS ME" />
<ImageView
android:id="@+id/onPressImage"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#000000"
android:scaleType="fitXY"
android:src="@drawable/shape_circle_onpress" />
</FrameLayout>
Right now the Image simply appears at the coordinates after 1 second has passed.
Can anybody point out the hidden flaw in this extremely simple piece of functionality?
The above code seems to be technically correct which is why I was having such a big problem solving it.
The issue was caused by me using
setX()andsetY()andAnimationtogether – On my Xoom tablet (running 3.2 API 13) this caused some pretty funky behaviour even after switching toScaleAnimation.Removing the
setX()andsetY()removes the glitchy behaviour as does switching to a Galaxy Nexus (Running API 4.0 Icecream Sandwich). SettinghardwareAcceleratedtotrueinstead of removing thesetX()andsetY()also improved the problem (but didn’t fix it completely).If anyone know’s why this is I’ll be more than happy to upvote and change the accepted answer on this one. I’m going to chalk it up to a flakey Android API until then.