我在我的安卓应用中有一个类似按钮的imageView,在其中添加了onClick事件。但是,可能会发现当点击时,并没有给imageView添加可点击效果。如何实现这一点?
你可以使用类似以下的方法,使用单张图片来完成此操作:
//get the image view
ImageView imageView = (ImageView)findViewById(R.id.ImageView);
//set the ontouch listener
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
ImageView view = (ImageView) v;
//overlay is black with transparency of 0x77 (119)
view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.SRC_ATOP);
view.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
ImageView view = (ImageView) v;
//clear the overlay
view.getDrawable().clearColorFilter();
view.invalidate();
break;
}
}
return false;
}
});
我可能会把这个变成ImageView的子类(或者ImageButton,因为它也是ImageView的子类),以便更容易地重用,但这应该允许你对一个ImageView应用“选中”效果。
你可以为点击和未点击状态设计不同的图像,并将它们设置在 onTouchListener 中,如下所示。
final ImageView v = (ImageView) findViewById(R.id.button0);
v.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN: {
v.setImageBitmap(res.getDrawable(R.drawable.img_down));
break;
}
case MotionEvent.ACTION_CANCEL:{
v.setImageBitmap(res.getDrawable(R.drawable.img_up));
break;
}
}
return true;
}
});
更好的选择是定义一个选择器如下所示。<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/img_down" />
<item android:state_selected="false"
android:drawable="@drawable/img_up" />
</selector>
在事件中选择图片:
v.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
v.setSelected(arg1.getAction()==MotionEvent.ACTION_DOWN);
return true;
}
});
MotionEvent.ACTION_DOWN || MotionEvent.ACTION_MOVE
,这样我们按住屏幕时图像就会一直显示。 - Alaa M.setOnTouchListener(...)
。你可以在XML中创建 <selector ...>
,然后设置ImageView的可点击性,如 <ImageView ... android:clickable="true" android:focusable="true" android:src="@drawable/my_selector" />
。 - Pierreandroid:foreground
属性将包括所有视图,包括ImageView。请参阅此帖子。
android:foreground
来定义其内容的覆盖层。如果我们将android:foreground
设置为一个选择器(例如API级别11+的?android:attr/selectableItemBackground
),并将OnClickListener
附加到FrameLayout而不是ImageView上,那么图像将与我们的选择器可绘制物重叠 - 我们所需的单击效果!<FrameLayout
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="?android:attr/selectableItemBackground" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/yourImageFile" />
</FrameLayout>
final View imageButton = findViewById(R.id.imageButton);
imageButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
// do whatever we wish!
}
});
selectableItemBackground
属性仅在API级别11中添加,因此,如果您想在较旧的API级别中使用此解决方案,则必须使用另一个选择器。例如,对于支持API级别7的我的一个应用程序,我使用使用Android Holo Colors Generator工具生成的@drawable/list_selector_holo_light
资源。 - justasm在XML文件中使用 style="?android:borderlessButtonStyle",这将显示Android默认的点击效果。
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
style="?android:borderlessButtonStyle"
/>
android:adjustViewBounds="true"
取消填充。 - Hafez Divandaristyle="?android:attr/borderlessButtonStyle"
:https://developer.android.com/guide/topics/ui/controls/button#Borderless - Hafez Divandaristyle="?android:actionButtonStyle"
以实现可点击的 ImageView。 - palindrom可以直接使用ImageButton。
这是我的简单解决方法:
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Agrega porcentajes de cada fraccion de grafica pastel
Animation animFadein = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);
iv.startAnimation(animFadein);
});
在文件res/anim/fade_in.xml
中:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:duration="100"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
如果您希望在轻敲时出现涟漪效果,则可以使用以下代码:
<ImageView
...
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
...
</ImageView>
同样地,您可以为TextView实现点击效果。
<TextView
...
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
...
</TextView>
将可选背景设置为ImageView并添加一些填充。然后附加OnClickListener。
<ImageView
android:id="@+id/your_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_image"
android:padding="10dp"
android:background="?android:attr/selectableItemBackground"/>
定义选择器Drawable的选择
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/img_down" />
<item android:state_selected="false"
android:drawable="@drawable/img_up" />
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed ="true"
android:drawable="@drawable/img_down" />
<item android:state_pressed ="false"
android:drawable="@drawable/img_up" />
</selector>
这对我有用:
img.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
((ImageView)v).setImageAlpha(200);
break;
}
case MotionEvent.ACTION_MOVE:
{
// if inside bounds
if(event.getX() > 0 && event.getX() < v.getWidth() && event.getY() > 0 && event.getY() < v.getHeight())
{
((ImageView)v).setImageAlpha(200);
}
else
{
((ImageView)v).setImageAlpha(255);
}
break;
}
case MotionEvent.ACTION_UP:
{
((ImageView)v).setImageAlpha(255);
}
}
return true;
}
});
@编辑:正如Gunhan所说,使用setImageAlpha方法会存在向后兼容性问题。我使用了这个方法:
public static void setImageAlpha(ImageView img, int alpha)
{
if(Build.VERSION.SDK_INT > 15)
{
img.setImageAlpha(alpha);
}
else
{
img.setAlpha(alpha);
}
}
false
,视图的实际onClick()
事件将不会被触发。 - Nimrod Dayan