gpt4 book ai didi

安卓 : When Button Down Change Pic Consecutive

转载 作者:行者123 更新时间:2023-11-29 21:48:13 25 4
gpt4 key购买 nike

我想在 android 4.2 上按下按钮时连续更改图片此代码仅更改一次

代码:

public void addListenerOnButton() {


b1 = (Button) findViewById(R.id.b1);

i = (ImageView) findViewById(R.id.iv);

b1.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {

while (event.getAction() == MotionEvent.ACTION_DOWN){
ii++;
if (ii%2==1) i.setImageResource(R.drawable.pic1); else i.setImageResource(R.drawable.pic2);

return true;

}

return false;

}

});

}

最佳答案

检查下面的代码。它应该产生你想要的结果

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

final Button button = (Button) findViewById(R.id.button);
final ImageView image = (ImageView) findViewById(R.id.image);
final AnimationDrawable animation = new AnimationDrawable();

animation.addFrame(getResources().getDrawable(R.drawable.pic1), 200);
animation.addFrame(getResources().getDrawable(R.drawable.pic2), 200);
animation.setOneShot(false);

image.setImageDrawable(animation);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
animation.start();
button.setPressed(true);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
animation.stop();
button.setPressed(false);
}

return true;
}
});
}

关于安卓 : When Button Down Change Pic Consecutive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15174909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com