A sentimental robot

OnTouch 본문

Android Studio

OnTouch

GOD03219 2018. 2. 6. 11:42
package com.example.i310_54.myapplication08;

import android.graphics.Color;
import android.os.CountDownTimer;
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.LinearLayout;
import android.widget.ProgressBar;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

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

final Button button=findViewById(R.id.button1);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()){

case MotionEvent.ACTION_DOWN:{
Button button2 = findViewById(R.id.button2);
float x = button2.getX();
x += 20.0f;
button2.setX(x);
}break;

case MotionEvent.ACTION_MOVE:{
Button button2 = findViewById(R.id.button2);
int w = button2.getWidth();
w += 10;
button2.setWidth(w);
}break;
case MotionEvent.ACTION_UP:{
Button button2 = findViewById(R.id.button2);
Random rnd=new Random();
int r=rnd.nextInt(256);
int g=rnd.nextInt(256);
int b=rnd.nextInt(256);
button2.setBackgroundColor(Color.argb(0xff,r,g,b));
}break;
}
return false;
}
});
}
}

'Android Studio' 카테고리의 다른 글

My xml  (0) 2018.02.06
Customizing  (0) 2018.02.06
RelativeLayout  (0) 2018.02.06
Handler  (0) 2018.02.05
GridLayout2  (0) 2018.02.05