버튼을 눌렀을 때 게이지가 올라가고, 떼면 도중에 멈추게 하도록 만들고 싶습니다.
구글링을 이용해서 코드를 작성했습니다. 그런데 문제는
아래의 버튼을 누르면 1초내로 게이지가 다 차고, 버튼을 다시 눌러도
다시 원상태로 돌아가지 않습니다.
package andromeda.android.button1;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button start;
AnimationDrawable ani;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = findViewById(R.id.start);
img = findViewById(R.id.img);
img.setBackgroundResource( R.drawable.animation );
ani = (AnimationDrawable)img.getBackground();
start.setOnTouchListener(touch);
}
View.OnTouchListener touch = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
handler.sendEmptyMessage(0);
break;
case MotionEvent.ACTION_UP :
handler.sendEmptyMessage(1);
break;
}
return true;
}
};
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0 :
img.setBackgroundResource( R.mipmap.a1 );
img.setBackgroundResource( R.mipmap.a2 );
img.setBackgroundResource( R.mipmap.a3 );
img.setBackgroundResource( R.mipmap.a4 );
img.setBackgroundResource( R.mipmap.a5 );
img.setBackgroundResource( R.mipmap.a6 );
img.setBackgroundResource( R.mipmap.a7 );
img.setBackgroundResource( R.mipmap.a8 );
img.setBackgroundResource( R.mipmap.a9 );
img.setBackgroundResource( R.mipmap.a10 );
ani.start();
break;
case 1:
ani.stop();
break;
}
}
};
}