使用 onFling 更改 Imagebutton 图像

Change Imagebutton Image using onFling

我只是在玩 Android Studio,所以我一直在硬编码手势完成时我希望看到的变化。所以我知道该按钮不会从 jabong 更改为 amazon.com(尽管提供一点帮助使该动态也有帮助!)。

但在此之前,我遇到的麻烦是在执行 onFling 操作时更改 ImageButton 的图像。

原图在drawable文件夹里,我想改成的图也在drawable文件夹里。

我的代码如下。

public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener {

private TextView expiry;
private EditText JabongImpact;
private GestureDetectorCompat gestureDetector;
private ImageButton offer_image;
private Button storebutton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    JabongImpact = (EditText) findViewById(R.id.JabongImpact);
    expiry = (TextView) findViewById(R.id.expiry);
    offer_image = (ImageButton) findViewById(R.id.offer_image);
    this.gestureDetector = new GestureDetectorCompat(this, this);
    Button mybutton = (Button) findViewById(R.id.storebutton);

    mybutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("http://www.jabong.com");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });
    offer_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("http://www.jabong.com");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }

    });

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    JabongImpact.setText("Impact shop with Amazon today!");
    expiry.setText("expires: tomorrow!");
    storebutton.setText("AMAZON.IN");
    return true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    this.gestureDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

您可以通过这种方式更改图像

offer_image.setImageResource(R.drawable.<your drawable image>);