Android:画廊视图中的动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1561938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:16:37  来源:igfitidea点击:

Android: Animation in Gallery View?

androidimageandroid-animationgallery

提问by Legend

When I use the Gallery widget, how do I get the images to say scale up& glowon being selected and scaled down& un-glowon being unselected?

当我使用图库控件,我怎么得到的图像要说扩大焕发上被选中,并缩小联合国焕发的是未选中的

All tutorials I've seen have this effect but I'm not able to see it...

我看过的所有教程都有这个效果,但我看不到......

Is there some kind of an animationthat I have to attach to the Gallery?

是否有某种动画我必须附加到画廊

回答by Sebastian

Hope this is helpful. I manage to "simulate" the shrink/grow solution with the Gallerywidget. Since they removed the getScale(), things get a little bit complicated. I think that this it's not the best solution at all, but at least I can live with it.

希望这是有帮助的。我设法使用Gallery小部件“模拟”收缩/增长解决方案。由于他们删除了getScale(),事情变得有点复杂。我认为这根本不是最好的解决方案,但至少我可以接受它。

What I have found is that Gallerymanages the focus EXTREMELY BAD. So, the first approach was to add a focus change listener on the ImageViewdisplayed, but no luck there. Focus is a MESS there... in terms that, the selected image it's not the currently focused view. I have sent a mail to android-developers mailing list about some error on API doc (regarding the focusSearch()method and some focus contants).

我发现它Gallery管理的焦点非常糟糕。因此,第一种方法是在ImageView显示上添加一个焦点更改侦听器,但没有运气。那里的焦点是一团糟......就这一点而言,所选图像不是当前聚焦的视图。我已经向 android-developers 邮件列表发送了一封邮件,内容是关于 API 文档的一些错误(关于focusSearch()方法和一些焦点内容)。

Here's my solution to this problem:

这是我对这个问题的解决方案:

Build an animation resource to 'grow' the image:

构建动画资源以“增长”图像:

<?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"/>

If you don't get what that means then you should proceed to read this

如果你不明白这意味着什么,那么你应该继续阅读这个

That will be our 'grow' effect, and you will need to save it in: res/anim/grow.xmlor whatever name it suites you (but always in res/anim dir).

这将是我们的“增长”效果,您需要将其保存在:res/anim/grow.xml或适合您的任何名称中(但始终在 res/anim 目录中)。

You can follow the resource guide from hereto create a Galleryview. The ImageAdapterbuilds an ImageViewevery time that the Galleryobject calls getView(). One workaround you could implement is adding a line to the getView()method that identifies a Viewwith a position, this way:

您可以按照此处的资源指南创建Gallery视图。将ImageAdapter建立一个ImageView使每一次Gallery对象调用getView()。您可以实施的一种解决方法是向getView()方法中添加一行,View用 a标识a position,如下所示:

...
i.setId(position);
...

With that line added to the getView()method of the ImageAdpaterobject, you can then unequivocally identify that view within a listener, for instance:

将该行添加到对象的getView()方法中后ImageAdpater,您就可以在侦听器中明确标识该视图,例如:

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");

    }
    });

NOTE: You may notice that all the values from animation and from layout parameters has been choosen by me at hand. This is because i'm not going to clean code for you. And, this is just a workaround the BAD focus issue with this widget or the android view system. If focus were OK, then, all you need to do is set a focus change listener that makes the gros/shrink when it got focus/unfocused.

注意:您可能会注意到动画和布局参数中的所有值都是我手头选择的。这是因为我不会为你清理代码。而且,这只是此小部件或 android 视图系统的 BAD 焦点问题的解决方法。如果焦点没问题,那么您需要做的就是设置一个焦点更改侦听器,使焦点在获得焦点/未聚焦时缩小/缩小。

I hope this may help you to find a way around for your problem,

我希望这可以帮助您找到解决问题的方法,

Regards,

问候,

New EDIT: This is the listener I have set, I also added the line i.clearAnimation()in the getView()method:

新的编辑:这是我设置的,我还增加了行听者i.clearAnimation()getView()方法:

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) {
    }
}

回答by I82Much

You need to use an ImageSwitcher. The ImageSwitcher has methods for setting the in and out animations (when image is selected and deselected, or selected and replaced).

您需要使用 ImageSwitcher。The ImageSwitcher has methods for setting the in and out animations (when image is selected and deselected, or selected and replaced).

The following linkhas a good tutorial on how to use it in conjunction with the Gallery.

下面的链接,对如何与画廊一起使用它一个很好的教程。

回答by ayorhan

I implemented a similar animation like this:

我实现了一个类似的动画:

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) {}

    });