【发布时间】:2009-10-13 18:03:28
【问题描述】:
当我使用图库小部件时,如何让图像在被选中时显示放大和发光以及在被未选中时缩小和不发光?
我看过的所有教程都有这个效果但是我看不到...
我必须将某种动画附加到图库吗?
【问题讨论】:
-
我也在做同样的事情,但它在某些安卓设备上不起作用,如果你在画廊视图中实现了这个动画,请给我发代码。
标签: android image android-animation gallery
当我使用图库小部件时,如何让图像在被选中时显示放大和发光以及在被未选中时缩小和不发光?
我看过的所有教程都有这个效果但是我看不到...
我必须将某种动画附加到图库吗?
【问题讨论】:
标签: android image android-animation gallery
希望这会有所帮助。我设法使用Gallery 小部件“模拟”缩小/增长解决方案。由于他们删除了getScale(),事情变得有点复杂。我认为这根本不是最好的解决方案,但至少我可以忍受。
我发现Gallery 对焦点的管理非常糟糕。因此,第一种方法是在显示的ImageView 上添加焦点更改侦听器,但那里没有运气。焦点在那里是一团糟……就所选图像而言,它不是当前聚焦的视图。我已经向 android-developers 邮件列表发送了一封邮件,内容是关于 API doc 上的一些错误(关于 focusSearch()method 和一些焦点内容)。
这是我对这个问题的解决方案:
构建动画资源以“增长”图像:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="1.10"
android:fromYScale="1.0"
android:toYScale="1.10"
android:duration="300"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fillAfter="false"/>
如果你不明白这意味着什么,那么你应该继续阅读this
这将是我们的“成长”效果,您需要将其保存在:res/anim/grow.xml 或任何适合您的名称(但始终在 res/anim 目录中)。
您可以按照here 的资源指南创建Gallery 视图。每次Gallery 对象调用getView() 时,ImageAdapter 都会构建一个ImageView。您可以实施的一种解决方法是在getView() 方法中添加一行,用position 标识View,这样:
...
i.setId(position);
...
将该行添加到ImageAdpater 对象的getView() 方法后,您就可以在侦听器中明确识别该视图,例如:
g.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected (AdapterView<?> parent, View v, int position, long id) {
Animation grow = AnimationUtils.loadAnimation(YourActivity.this, R.anim.grow);
View sideView = parent.findViewById(position - 1);
if (sideView != null)
((ImageView)sideView).setLayoutParams(new Gallery.LayoutParams(150, 100));
sideView = parent.findViewById(position + 1);
if (sideView != null)
((ImageView)sideView).setLayoutParams(new Gallery.LayoutParams(150, 100));
v.startAnimation(grow);
v.setLayoutParams(new Gallery.LayoutParams(170, 150));
}
public void onNothingSelected (AdapterView<?> parent) {
System.out.println("NOTHING SELECTED");
}
});
注意:您可能会注意到动画和布局参数中的所有值都是由我选择的。这是因为我不会为你清理代码。而且,这只是此小部件或 android 视图系统的 BAD 焦点问题的解决方法。如果焦点没问题,那么,您需要做的就是设置一个焦点更改侦听器,当它获得焦点/未聚焦时,它会产生 gros/shrink。
我希望这可以帮助您找到解决问题的方法,
问候,
新编辑:这是我设置的监听器,我还在getView()方法中添加了i.clearAnimation()这一行:
private class SelectListener implements AdapterView.OnItemSelectedListener {
private Animation grow;
private int last;
public SelectListener() {
grow = AnimationUtils.loadAnimation(RouteGallery.this, R.anim.grow);
last = 0;
}
public void onItemSelected (AdapterView<?> parent, View v, int position, long id) {
View sideView = parent.findViewById(last);
if (sideView != null && last != position)
sideView.clearAnimation();
v.startAnimation(grow);
last = position;
}
public void onNothingSelected (AdapterView<?> parent) {
}
}
【讨论】:
您需要使用 ImageSwitcher。 The ImageSwitcher has methods for setting the in and out animations (when image is selected and deselected, or selected and replaced).
following link 有一个很好的教程,介绍如何将它与 Gallery 结合使用。
【讨论】:
我实现了类似这样的动画:
final Animation shrink = AnimationUtils.loadAnimation(activity, R.anim.shrink);
shrink.setFillAfter(true);
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// This iterates through the views which are currently displayed on screen.
for (int k=0; k<gallery.getChildCount(); k++){
// Do whatever else you want, here.
// This is how you get a particular element of a view
ImageView background = (ImageView) gallery.getChildAt(k).findViewById(R.id.menu_item_background);
//clear animation
gallery.getChildAt(k).clearAnimation();
}
// Scale the selected one
view.startAnimation(shrink);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});
【讨论】: