简体   繁体   English

android imageview onClick动画

[英]android imageview onClick animation

I guess this is kind of an odd question but I have tried setting onClicklistener on an ImageView and it has worked. 我想这是一个奇怪的问题,但我已经尝试在ImageView上设置onClicklistener并且它已经工作了。 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. 我的意思是如果你有一些人在其他移动环境(如Apple iPhone)上工作,那么当我们点击其他环境中的图像时,它会对图像产生影响,以便用户可以理解图像已被点击。

I have tried setting alpha using setalpha method but it doesn't work. 我尝试使用setalpha方法设置alpha但它不起作用。 Though the same thing is working fine on onFocusListener implementation. 虽然同样的事情在onFocusListener实现上工作正常。 Can some1 suggest a different way to modify the image on click... some1可以建议一种不同的方式来修改点击图像...

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! 谢谢!

   <?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方法中,更改将是可见的...

You'll want to use a drawable that contains different images for the different states you want to support. 您将需要使用包含不同图像的drawable,以用于您要支持的不同状态。 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. 将此文件命名为img.xml或其他内容并将其放入drawable目录中,然后将ImageView的图像设置为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. @drawable/img_at_rest是您尝试使用的原始图像,而@drawable/img_pressed@drawable/img_focused是用于各自状态的图像。 You can also use solid colors instead of images if it's appropriate for your use case. 如果它适合您的用例,您也可以使用纯色而不是图像。

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

不确定它是否有效,但你试过setSelected() http://developer.android.com/reference/android/widget/ImageView.html#setSelected ( boolean)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM