使用onFling更改Imagebutton图像

时间:2015-08-31 06:22:27

标签: java android imagebutton gesture

我刚刚玩Android Studio,所以我只是硬编码我想在手势完成时看到的变化。所以我知道按钮不会从jabong变为amazon.com(尽管有一些帮助使得这种动态也会有所帮助!)。

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

原始图像位于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;
}

1 个答案:

答案 0 :(得分:1)

你可以用这种方式改变图像

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