마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

TouchListener와 handler 사용법

0 추천

이게 처음이고, 

이게 마지막입니다. 이렇게 버튼을 꾹 누르면 게이지가 올라가고,

떼면 거기서 멈추는 게이지를 만들고 싶습니다. 

 

일단 소소코드는 아래와 같이 입력했습니다. xml은 쉬운데, 액티비티쪽이 어렵네요.

터치리스너쪽에서 막힙니다. 

 
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;

public class MainActivity extends AppCompatActivity {
    ImageView img;
    Button start;
    AnimationDrawable ani;

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


        img = findViewById(R.id.img);
        start = findViewById(R.id.start);
        img.setBackgroundResource(R.drawable.animation);
        ani = (AnimationDrawable) img.getBackground();



        start.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:

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

                        case MotionEvent.ACTION_UP:
                        ani.stop();
                        break;
                }

                return true;
            }
        });
    }


}



oldblueberry (140 포인트) 님이 2018년 7월 26일 질문

1개의 답변

0 추천
터치의 액션이 down일때 animation drawable을 start() 하시면 될 것 같습니다.
원조안드로이드 (58,190 포인트) 님이 2018년 7월 27일 답변
...