A sentimental robot

Customizing 본문

Android Studio

Customizing

GOD03219 2018. 2. 6. 13:57
package com.example.i310_54.myapplication08;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
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;

class Tiger extends View {

public Tiger(Context context) {
super(context);
}

@Override
protected void onDraw(Canvas canvas) {
Paint paint=new Paint(); // Create a tool to draw
paint.setColor(Color.RED);

//Canvas is where I draw
canvas.drawRect(100,200,300,400,paint); //draw a rectangle

}
}
public class MainActivity extends AppCompatActivity {

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

Tiger tiger = new Tiger(this);
setContentView(tiger);


}
}

 

package com.example.i310_54.myapplication08;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
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;

class Tiger extends View {
int x=0;
public Tiger(Context context) {
super(context);
}

@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.RED);
x+=20;
canvas.drawRect(100-x, 200-x, 300+x, 400+x, paint);


}
}

public class MainActivity extends AppCompatActivity {
String TAG = "EUN";

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

final Tiger tiger = new Tiger(this);
setContentView(tiger);

tiger.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
v.invalidate(); // invalidate 함수는 무조건 onDraw call
}
return false;
}
});


}
}

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

Thread를 이용한 UI  (0) 2018.02.07
My xml  (0) 2018.02.06
OnTouch  (0) 2018.02.06
RelativeLayout  (0) 2018.02.06
Handler  (0) 2018.02.05