android imageview onClick 动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2270751/
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
android imageview onClick animation
提问by JaVadid
I guess this is kind of an odd question but I have tried setting onClicklistener on an ImageView and it has worked. But the problem is that the user cannot sense the click. I mean if some of u have worked on other mobile environs(like Apple iPhone) then when we click on an Image in other environs then it gives an effect on the image so that the user can understand that the image has been clicked.
我想这是一个奇怪的问题,但我尝试在 ImageView 上设置 onClicklistener 并且它起作用了。但问题是用户无法感觉到点击。我的意思是,如果你们中的一些人在其他移动环境(如 Apple iPhone)上工作,那么当我们在其他环境中单击图像时,它会对图像产生影响,以便用户可以理解图像已被单击。
I have tried setting alpha using setalpha
method but it doesn't work. Though the same thing is working fine on onFocusListener implementation. Can some1 suggest a different way to modify the image on click...
我曾尝试使用setalpha
方法设置 alpha但它不起作用。虽然同样的事情在 onFocusListener 实现上运行良好。有人可以建议一种不同的方式来修改点击图像...
I am new to android so haven't learnt the nuances of simple animation also... if there is any simple animation I can use for the same then please let me know.
我是 android 新手,所以还没有了解简单动画的细微差别......如果有任何简单的动画我可以使用,那么请告诉我。
Thanks!
谢谢!
回答by JaVadid
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.5"
android:duration = "300">
</alpha>
<scale
android:fromXScale = "1"
android:toXScale = "0.9"
android:fromYScale = "1"
android:toYScale = "0.9"
android:pivotX="50%"
android:pivotY="50%"
android:duration = "50">
</scale>
</set>
I don't know if this is the correct method but defining an animation as mentioned did the trick. Now we just have to give
我不知道这是否是正确的方法,但是如上所述定义动画就可以了。现在我们只需要给
public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(Context, R.anim.image_click));
//Your other coding if present
}
in the OnClick method and the change will be visible...
在 OnClick 方法中,更改将可见...
回答by emmby
You'll want to use a drawable that contains different images for the different states you want to support. Here's an example:
您将需要使用包含不同图像的可绘制对象来支持您想要支持的不同状态。下面是一个例子:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/img_pressed" />
<item android:state_focused="true" android:drawable="@drawable/img_focused" />
<item android:drawable="@drawable/img_at_rest" />
</selector>
Name this file img.xml or something and put it in your drawable directory, then set your ImageView's image to img.xml. @drawable/img_at_rest
is the original image you're trying to use, while @drawable/img_pressed
and @drawable/img_focused
are the images to use for their respective states. You can also use solid colors instead of images if it's appropriate for your use case.
将此文件命名为 img.xml 或其他名称并将其放在 drawable 目录中,然后将 ImageView 的图像设置为 img.xml。 @drawable/img_at_rest
是您尝试使用的原始图像,而@drawable/img_pressed
和@drawable/img_focused
是用于各自状态的图像。如果适合您的用例,您还可以使用纯色代替图像。
回答by Pierry
anim/anim_item.xml
动画/anim_item.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1."
android:duration="1000">
</alpha>
</set>
And add:
并添加:
myView.startAnimation(AnimationUtils.loadAnimation(context, R.anim.anim_item));
回答by Patrick Kafka
Not sure if it works, but have you tried setSelected() http://developer.android.com/reference/android/widget/ImageView.html#setSelected(boolean)
不确定它是否有效,但是您是否尝试过 setSelected() http://developer.android.com/reference/android/widget/ImageView.html#setSelected(boolean)