※ 화면을 터치하여 앱의 기능을 구현해 보자
- 레이아웃에서 TextView나 Button 등은 모두 클래스이다. 그래서 우리가 CustomView 클래스를
만들어서 레이아웃에 적용시킬 수 있다(패키지명+클래스이름)
그렇기 때문에 MainActivity에서 인스턴스화 해서 아이디를 설정해 주면 그 위젯에 대한 인스턴스가 된다.
CustomView
public class CustomView extends View{
public final static int CURRENT_CANVAS =0;
public final static int NEW_CANVAS =1;
private Context mContext;
ArrayList<Dot> dots = new ArrayList<Dot>();
private Paint paint;
public CustomView(Context context) {
super(context);
initPaint(CURRENT_CANVAS);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint(CURRENT_CANVAS);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint(CURRENT_CANVAS);
}
public void SetContext(Context context){
this.mContext = context;
}
@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
//캔버스 배경색 설정
canvas.drawColor(Color.WHITE);
for(int i =0; i< dots.size(); i++){
if(dots.get(i).isMovState()){
canvas.drawLine(dots.get(i-1).getX(), dots.get(i-1).getY(), dots.get(i).getX(), dots.get(i).getY(), paint);
}
}
}
public void initPaint(int i){
dots.clear();
paint = null;
paint = new Paint();
paint.setColor(Color.GREEN);
//선굵기 설정
paint.setStrokeWidth(2);
//곡선 부드럽게 설정
paint.setAntiAlias(true);
if(i == NEW_CANVAS){
/*onDraw 호출 함수 : onDraw를 바로 호출해도 되지만 여러가지 문제로 발생할 수 있으므로
* invalidate() 호출해서 onDraw를 콜하는게 정석
*/
invalidate();
}
}
@Override
public boolean onTouchEvent ( MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
dots.add(new Dot(event.getX(),event.getY(),false));
return true;
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
dots.add(new Dot(event.getX(),event.getY(),true));
invalidate();
return true;
}
return false;
}
}
MainActivity
public class MainActivity extends Activity {
CustomView customView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customView = (CustomView) findViewById(R.id.customView);
customView.SetContext(MainActivity.this);
button = (Button) findViewById(R.id.bt_new_canvas);
button.setOnClickListener(onclick);
}
OnClickListener onclick = new OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bt_new_canvas:
customView.initPaint(CustomView.NEW_CANVAS);
break;
default :
break;
}
}
};
}
Dot
public class Dot {
private float x,y;
private boolean movState;
public Dot(float x, float y, boolean movState) {
// TODO Auto-generated constructor stub
this.x = x;
this.y = y;
this.movState = movState;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public boolean isMovState() {
return movState;
}
public void setMovState(boolean movState) {
this.movState = movState;
}
}
Acitvity_MAin
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.androidex.MainActivity" >
<com.example.androidex.CustomView
android:id="@+id/customView"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="9"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
<Button
android:id="@+id/bt_new_canvas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Canvas"
/>
</LinearLayout>
</LinearLayout>
'플밍 is 뭔들 > 안드로이드_인강' 카테고리의 다른 글
07-2. 액티비티 생명주기 테스트 및 로그 활용법 (0) | 2016.11.27 |
---|---|
07-1. 액티비티 (0) | 2016.11.27 |
06-1. 이벤트 & 리스너 (0) | 2016.11.27 |
05-4. 어댑터 뷰 (0) | 2016.11.27 |
05-3. 어댑터 뷰 (0) | 2016.11.27 |