Android 如何在单击时将按钮的效果赋予ImageView?
出于许多原因,我不得不将ImageView定义为按钮,因为它看起来更好。但是,当我单击它时,它没有按钮的效果,我该怎么做呢?如果您想将“按下”的视觉状态添加到Android 如何在单击时将按钮的效果赋予ImageView?,android,imageview,android-button,Android,Imageview,Android Button,出于许多原因,我不得不将ImageView定义为按钮,因为它看起来更好。但是,当我单击它时,它没有按钮的效果,我该怎么做呢?如果您想将“按下”的视觉状态添加到图像视图中,请使用选择器作为可绘制状态 在drawables文件夹中,创建一个新的xml文件,如下所示: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"
图像视图中,请使用选择器作为可绘制状态
在drawables文件夹中,创建一个新的xml文件,如下所示:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/myDrawable" />
<item android:state_pressed="true" android:drawable="@drawable/myPressedDrawable" />
</selector>
将上述可绘制设置为图像源将显示“myDrawable”作为默认图像,并在用户按下视图时显示“myPressedDrawable”。您可以将选择器设置为背景,但是使用图像按钮更好,因为它本质上就是为了实现这一点而设计的
仅供参考,这里是ImageButton的样式定义
<style name="Widget.ImageButton">
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:scaleType">center</item>
<item name="android:background">@android:drawable/btn_default</item>
</style>
真的
真的
居中
@android:drawable/btn\u默认值
几天前我收到了同样的请求。这是我的动画和振动解决方案
在ressource文件中,创建2个文件:
res/anim/anim\u scale\u down.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true"
android:fillEnabled="true">
<scale
android:fromXScale="1"
android:toXScale="0.95"
android:fromYScale="1"
android:toYScale="0.95"
android:pivotX="50%"
android:pivotY="50%"
android:duration="100"
/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<scale
android:fromXScale="0.95"
android:toXScale="1"
android:fromYScale="0.95"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:duration="50"
/>
</set>
在onCreate
中,使用动画资源设置成员:
_animDown = AnimationUtils.loadAnimation(_context, R.anim.anim_scale_down);
_animUp = AnimationUtils.loadAnimation(_context, R.anim.anim_scale_up);
然后声明一个onTouchListener:
private View.OnTouchListener _animListener = new View.OnTouchListener()
{
@Override
public boolean onTouch(final View view, MotionEvent event) {
switch(event.getAction()) {
case (android.view.MotionEvent.ACTION_DOWN) :
view.startAnimation(_animDown);
Vibrator v = (Vibrator)_context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(25);
return false;
case (android.view.MotionEvent.ACTION_UP):
view.startAnimation(_animUp);
return false;
case (android.view.MotionEvent.ACTION_CANCEL) :
view.startAnimation(_animUp);
}
return false;
}
};
将touchListener设置为您的按钮:
myButton.setOnTouchListener(_animListener);
不要忘记在清单中设置振动权限:
<uses-permission android:name="android.permission.VIBRATE" />
您是在谈论onClick方法还是视觉按下状态?使用ImageButton,它会扩展ImageView,是的clickable@pskink我不能用那个,因为图像被删除了。@zhelon你说的“图像被删除了”是什么意思?@pskink配置错误(谷歌traslate烂)@Yjvay我不理解最后一段“将上述可绘制设置为图像源将显示‘myDrawable’作为默认图像,并在用户按下视图时显示‘myPressedDrawable’。“格雷特解决方案。与selectorGreat解决方案相比,我更喜欢它,谢谢,但是如何知道是否启用了系统振动?换句话说,如果系统启用振动,我想播放振动。如果振动未激活,或者设备没有振动模块,则此代码工作正常。我在Nexus7上试过。
<uses-permission android:name="android.permission.VIBRATE" />