android imageview onClick animation

后端 未结 4 1645
生来不讨喜
生来不讨喜 2021-01-30 17:24

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 me

相关标签:
4条回答
  • 2021-01-30 17:53

    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.

    0 讨论(0)
  • 2021-01-30 18:04

    anim/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));
    
    0 讨论(0)
  • 2021-01-30 18:14
       <?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...

    0 讨论(0)
  • 2021-01-30 18:14

    Not sure if it works, but have you tried setSelected() http://developer.android.com/reference/android/widget/ImageView.html#setSelected(boolean)

    0 讨论(0)
提交回复
热议问题