最初,所有图像的alpha级别都设置为50%。 当我单击特定图像时,它将增长到120%,其alpha级别将变为100%(“点亮”效果)。 当我单击另一个图像时,先前选择的图像将缩小回到100%,其alpha级别将返回到50%,当前选择的图像将按上述方式增长。
在我的布局中,我有三个相同大小的图像排成一行。 我单击第一个图像,然后单击第二个图像,然后再次单击第一个图像。 好的,没有问题。 现在,我单击第三个图像,我遇到了第一个图像的奇怪弹跳问题。 有什么办法可以解决这个问题吗?
我已经尝试过:
1.使用image.setAlpha(...)来避免在shrink.xml中设置alpha级别,然后调用fillAfter =“true”,但不幸的是,那是一个API 11调用
2.仅将alpha标记的fillAfter属性设置为true在shrink.xml中
3.在缩小动画之后立即调用image.startAnimation(fadeOut),但看起来很可怕。
4.重写onAnimationEnd(),但这个调用从未到达(??)
shrink.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fillAfter="true">
<scale
android:fromXScale="1.2"
android:toXScale="1.0"
android:fromYScale="1.2"
android:toYScale="1.0"
android:duration="300"
android:pivotX="50%"
android:pivotY="50%"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.5"
android:duration="300"/>
</set>
grow.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fillAfter="true">
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="1.20"
android:fromYScale="1.0"
android:toYScale="1.20"
android:duration="300"
android:pivotX="50%"
android:pivotY="50%"
/>
<alpha
android:fromAlpha="0.5"
android:toAlpha="1.0"
android:duration="300"/>
</set>
fade_out.xml:
<?xml version="1.0" encoding="UTF-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="300"
android:fromAlpha="1.0"
android:toAlpha="0.5"
android:fillAfter="true">
</alpha>
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:src="@drawable/image1"/>
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:src="@drawable/image2"/>
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:src="@drawable/image3"/>
</LinearLayout>
Test.java:
public class Test extends Activity {
private View mSelected;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Animation fadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);
final Animation grow = AnimationUtils.loadAnimation(this, R.anim.grow);
final Animation shrink = AnimationUtils.loadAnimation(this, R.anim.shrink);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mSelected == v)
return;
if (mSelected != null)
mSelected.startAnimation(shrink);
mSelected = v;
mSelected.startAnimation(grow);
}
};
ImageView image1 = (ImageView)findViewById(R.id.image1);
image1.startAnimation(fadeOut);
image1.setOnClickListener(listener);
ImageView image2 = (ImageView)findViewById(R.id.image2);
image2.startAnimation(fadeOut);
image2.setOnClickListener(listener);
ImageView image3 = (ImageView)findViewById(R.id.image3);
image3.startAnimation(fadeOut);
image3.setOnClickListener(listener);
}}