java - 使用 onFling 更改 Imagebutton 图像

标签 java android imagebutton gesture

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

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

原始图像在可绘制文件夹中,我想将其更改为的图像也是如此。

我的代码如下。

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>); 

关于java - 使用 onFling 更改 Imagebutton 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32305034/

相关文章:

java - 使用 session 和 cookie 的 Servlet 身份验证

Android,滑动面板布局与抽屉导航

Android 编辑文本 : How to disable cursor move on touch?

Android,删除自定义按钮边框

java - 如何在 Android Retrofit api 中为每个 api 使用 OkHttp 拦截器获取 api 请求和 api 响应时间?

java - 在Java中,如何从鼠标位置开始拖动图像?

android - 类型Play必须实现继承的抽象方法View.OnClickListener.onClick(View)

Android:如何设置 ImageButton 的 src 图像的高度/宽度?

java - 如何将自己的jar导入到android项目中(ADT 20)

android - NoClassDefFoundError : com. 谷歌.firebase.FirebaseOptions