728x90
728x170
MainActivity.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | package org.androidtown.mymultitouch; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; /** * Created by kss34 on 2017-03-13. */ public class MyView extends View { private static final String TAG = "MyView"; float curX1; float curY1; float curX2; float curY2; float oldX1; float oldY1; float oldX2; float oldY2; float diffX1; float diffY1; Paint paint; Bitmap bitmap; Bitmap mBitmap; Canvas mCanvas; public MyView(Context context) { super(context); init(context); } public MyView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { paint = new Paint(); Resources res = context.getResources(); bitmap = BitmapFactory.decodeResource(res, R.drawable.beach); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (w > 0 && h > 0) { newImage(w, h); redraw(); } } private void newImage(int w, int h) { mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(); mCanvas.setBitmap(mBitmap); } private void redraw() { mCanvas.drawColor(Color.WHITE); mCanvas.drawBitmap(bitmap, diffX1, diffY1, paint); invalidate(); } @Override protected void onDraw(Canvas canvas) { if (mBitmap != null) { canvas.drawBitmap(mBitmap, 0, 0, null); } } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); int pointerCount = event.getPointerCount(); curX1 = event.getX(0); curY1 = event.getY(0); if (pointerCount > 1) { curX2 = event.getX(1); curY2 = event.getY(1); } if (action == MotionEvent.ACTION_DOWN) { Log.d(TAG, "손가락이 눌렸습니다. : " + pointerCount + ", " + curX1 + ", " + curY1 + ", " + curX2 + ", " + curY2); } else if (action == MotionEvent.ACTION_MOVE) { Log.d(TAG, "손가락이 움직여졌습니다. : " + pointerCount + ", " + curX1 + ", " + curY1 + ", " + curX2 + ", " + curY2); diffX1 = curX1; diffY1 = curY1; invalidate(); redraw(); } else if (action == MotionEvent.ACTION_UP) { Log.d(TAG, "손가락이 떼졌습니다. : " + pointerCount + ", " + curX1 + ", " + curY1 + ", " + curX2 + ", " + curY2); } oldX1 = curX1; oldY1 = curY1; oldX2 = curX2; oldY2 = curY2; return true; } } | cs |
MyView.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | package org.androidtown.mymultitouch; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; /** * Created by kss34 on 2017-03-13. */ public class MyView extends View { private static final String TAG = "MyView"; float curX1; float curY1; float curX2; float curY2; float oldX1; float oldY1; float oldX2; float oldY2; float diffX1; float diffY1; Paint paint; Bitmap bitmap; Bitmap mBitmap; Canvas mCanvas; public MyView(Context context) { super(context); init(context); } public MyView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { paint = new Paint(); Resources res = context.getResources(); bitmap = BitmapFactory.decodeResource(res, R.drawable.beach); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (w > 0 && h > 0) { newImage(w, h); redraw(); } } private void newImage(int w, int h) { mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(); mCanvas.setBitmap(mBitmap); } private void redraw() { mCanvas.drawColor(Color.WHITE); mCanvas.drawBitmap(bitmap, diffX1, diffY1, paint); invalidate(); } @Override protected void onDraw(Canvas canvas) { if (mBitmap != null) { canvas.drawBitmap(mBitmap, 0, 0, null); } } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); int pointerCount = event.getPointerCount(); curX1 = event.getX(0); curY1 = event.getY(0); if (pointerCount > 1) { curX2 = event.getX(1); curY2 = event.getY(1); } if (action == MotionEvent.ACTION_DOWN) { Log.d(TAG, "손가락이 눌렸습니다. : " + pointerCount + ", " + curX1 + ", " + curY1 + ", " + curX2 + ", " + curY2); } else if (action == MotionEvent.ACTION_MOVE) { Log.d(TAG, "손가락이 움직여졌습니다. : " + pointerCount + ", " + curX1 + ", " + curY1 + ", " + curX2 + ", " + curY2); diffX1 = curX1; diffY1 = curY1; invalidate(); redraw(); } else if (action == MotionEvent.ACTION_UP) { Log.d(TAG, "손가락이 떼졌습니다. : " + pointerCount + ", " + curX1 + ", " + curY1 + ", " + curX2 + ", " + curY2); } oldX1 = curX1; oldY1 = curY1; oldX2 = curX2; oldY2 = curY2; return true; } } | cs |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="org.androidtown.mymultitouch.MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" tools:layout_editor_absoluteX="146dp" tools:layout_editor_absoluteY="16dp" /> <org.androidtown.mymultitouch.MyView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:text="Button" android:layout_below="@+id/button" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart='true' android:background="#ffbbccee" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> | cs |
app/res/drawable/beach.png
소스 출처 : Do It! 안드로이드 앱 프로그래밍 (정재곤 지음, 이지스퍼블리싱)
728x90
그리드형(광고전용)
'Programming > Android' 카테고리의 다른 글
이벤트 처리 방법 (0) | 2017.05.05 |
---|---|
상수 (Constant) (0) | 2017.05.03 |
숫자와 문자열을 서로 바꿔주는 메서드 (0) | 2017.05.03 |
색 (Color) (0) | 2017.05.03 |
매니페스트 (Manifest) (0) | 2017.01.25 |
위험 권한의 세부 정보 (0) | 2017.01.25 |
수명주기 (0) | 2017.01.24 |
레이아웃 인플레이션 (0) | 2017.01.18 |