Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javaandroid中快速更改图像_Java_Android_Image_Onclicklistener - Fatal编程技术网

在javaandroid中快速更改图像

在javaandroid中快速更改图像,java,android,image,onclicklistener,Java,Android,Image,Onclicklistener,我在点击时快速更改图像时遇到问题。我制作了一个图像按钮,我想在单击按钮时更改图像并返回到上一个图像。我找了很多,但找不到解决办法 public class Buttonone extends Activity { ImageButton btonImage; boolean isPressed = true; MediaPlayer mp; boolean isPlay = false; @Override protected void onCre

我在点击时快速更改图像时遇到问题。我制作了一个图像按钮,我想在单击按钮时更改图像并返回到上一个图像。我找了很多,但找不到解决办法

public class Buttonone extends Activity {
    ImageButton btonImage;
    boolean isPressed = true;
    MediaPlayer mp;
    boolean isPlay = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button_one);

        btonImage = (ImageButton) findViewById(R.id.btonImage);
        btonImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                playSound();toggleImage();
                }

        });
    }

    private void toggleImage() {
        // TODO Auto-generated method stub
        btonImage.setImageResource(R.drawable.image2);
    }
}
这是图像按钮的布局xml

    <ImageButton
android:id="@+id/btonImage"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="@null"
android:background="#080808"   
android:src="@drawable/image1" />

设置内容视图(右布局按钮)


这是你的问题。您应该设置整个布局并处理单击事件

将src设置为如下所示的可绘制

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/imgButtonBack"/>

根据需要更改图像

代码的当前行为是什么?您希望如何更改图像?更改图像颜色?大小像素?使用一些过滤器?“更改”太笼统了我想将整个图像更改为另一个图像并返回到原始图像我正在使用imagebutton,例如,如果单击图像按钮是鼠标图像,则鼠标图像会出现,然后再次出现鼠标图像。否不能解决问题,图像更改,但上一个图像不会返回。将鼠标图像放在上次出现的位置,即
presence\u audio\u busy
,并将鼠标图像放在android:state\u pressed=“true”drawable中。现在这个按钮会显示鼠标图像,当按下时会显示鼠标图像,当你离开它时,会再次显示鼠标图像
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:drawable="@android:drawable/presence_video_away"
       android:state_pressed="true" />
   <item android:drawable="@android:drawable/presence_audio_busy"
      android:state_focused="true" />
   <item android:drawable="@android:drawable/presence_audio_busy" />
</selector>