更改ImageView的来源后,按一下按钮一段时间按钮、来源、ImageView

2023-09-03 23:19:43 作者:懶得取名

我有两个按钮和一个ImageView的:

I have two buttons and one imageview:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"   
tools:context=".MainActivity"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" >

<ImageView
    android:id="@+id/dog"
    android:contentDescription="@string/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/dogsec" />

<ImageButton
    android:id="@+id/barkk"
    android:contentDescription="@string/desc"
    android:background="@android:color/transparent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/dog"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="45dp"
    android:src="@drawable/bark" />

<ImageButton
    android:id="@+id/rrrr"
    android:background="@android:color/transparent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/barkk"
    android:layout_below="@+id/barkk"
    android:layout_marginRight="41dp"
    android:layout_marginTop="15dp"
    android:contentDescription="@string/desc"
    android:src="@drawable/rrr" />

</RelativeLayout>

一)我需要做的ImageView改变其源到另一个(我指的是图像的默认状态是图一,它应该显示图片2,回到图一)按钮barkk点击。它不应该显示图片2,只要barkk是pressed。

a) I need to make Imageview change its' source to another one(I mean the image's default state is picture1, it should show picture2 and go back to picture1) on button "barkk" click. It should not show picture2 as long as barkk is pressed.

b)和我需要显示图片3,只要按钮RRRR是pssed $ P $。

b) and I need to display picture3 as long as button rrrr is pressed.

我使用完全相同的ImageView应根据情况的变化其源)或b),因为它是如上所述。

I use exactly ImageView which should change its' source depending on case a) or b) as it is described above.

请帮助我。在此先感谢

推荐答案

使用这种code。

myButton.setOnClickListener(new OnClickListener() {
@Override
public boolean onTouch(View v, MotionEvent event) 
  {
    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        myImageView.setImageResource(R.drawable.myNewImage);
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        myImageView.setImageResource(R.drawable.myNewImage);
    }
}
};