지우개를 사용하는 예제입니다.
canvas를 bitmap으로 나누고 setXfermode를 통해서 지우개 기능을 추가하는 내용인데..
너무 단순하면 허전해서 몇 가지 기능을 넣었습니다.
1. option menu를 통해 pen <-> eraser 간 전환
2. 이미지를 가져와서 canvas 에 그리기
2-1. Manifest.Permission을 가져오는 기능
2-2. ACTION_PICK으로 image Uri를 얻어오는 기능
2-3. image uri에서 bitmap을 가져와서 canvas에 그리는 기능
사진이 안올라가네요. 사진을 올릴 때 TestView에 Paint 내용을 설명하겠습니다.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "paul";
private static final int REQ_CODE_SELECT_IMAGE=200;
private static final int REQUEST_EXTERNAL_STORAGE = 201;
private static final int PICK_IMAGE_ID = Menu.FIRST;
private static final int SET_TO_PEN_ID = Menu.FIRST+1;
private static final int SET_TO_ERASER_ID = Menu.FIRST+2;
TestView testView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
testView = new TestView(this);
setContentView(testView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, PICK_IMAGE_ID,0, "pick image");
menu.add(0, SET_TO_PEN_ID,0, "set to pen");
menu.add(0, SET_TO_ERASER_ID,0, "set to eraser");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case PICK_IMAGE_ID:
verifyStoragePermission();
break;
case SET_TO_PEN_ID:
testView.setToPen();
break;
case SET_TO_ERASER_ID:
testView.setToEraser();
break;
}
return super.onOptionsItemSelected(item);
}
//// 이미지가져오는 코드 그냥 사용하자..///////////
String[] PERMISSIONS_STOARGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private void verifyStoragePermission(){
int readPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if(readPermission != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, PERMISSIONS_STOARGE, REQUEST_EXTERNAL_STORAGE );
}else{
pickImageFromGallery();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_EXTERNAL_STORAGE){
if(resultCode == RESULT_OK){
pickImageFromGallery();
}
}else if(requestCode == REQ_CODE_SELECT_IMAGE){
if(resultCode == RESULT_OK){
try{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
testView.setBitmp(bitmap);
}catch (Exception e){
}
}
}
}
private void pickImageFromGallery(){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(MediaStore.Images.Media.CONTENT_TYPE);
intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQ_CODE_SELECT_IMAGE);
}
}
TestView.java
public class TestView extends View {
private static final String TAG = "paul";
Paint mPenPaint;
Paint mTransPaint;
PorterDuffXfermode clear = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
Bitmap mBitmap1;
Canvas mCanvas1;
Bitmap mBitmap2;
Canvas mCanvas2;
Paint mBitmapPaint;
float x;
float y;
Path mPath = new Path();
public TestView(Context context) {
super(context);
this.mPenPaint = new Paint(Paint.DEV_KERN_TEXT_FLAG);
this.mPenPaint.setAntiAlias(true);
this.mPenPaint.setColor(Color.BLUE);
this.mPenPaint.setStrokeWidth(5f);
this.mPenPaint.setStyle(Paint.Style.STROKE);
this.mTransPaint = new Paint(Paint.DEV_KERN_TEXT_FLAG);
this.mTransPaint.setAntiAlias(true);
this.mTransPaint.setColor(Color.TRANSPARENT);
this.mTransPaint.setStrokeWidth(5f);
this.mTransPaint.setStyle(Paint.Style.STROKE);
mPath = new Path();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap1 = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mBitmap2 = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas1 = new Canvas(mBitmap1);
mCanvas2 = new Canvas(mBitmap2);
}
public void setToPen(){
mPenPaint.setXfermode(null);
}
public void setToEraser(){
mPenPaint.setXfermode(clear);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap2,0,0, mBitmapPaint);
canvas.drawBitmap(mBitmap1,0,0, mBitmapPaint);
canvas.drawPath(mPath,mTransPaint );
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
x = event.getX(0);
y = event.getY(0);
mPath.moveTo(x, y);
return true;
}else if(event.getAction() == MotionEvent.ACTION_MOVE){
x = event.getX(0);
y = event.getY(0);
mPath.lineTo(x, y);
mCanvas1.drawPath(mPath, mPenPaint);
invalidate();
}else if(event.getAction() == MotionEvent.ACTION_UP){
mCanvas1.drawPath(mPath, mPenPaint);
mPath.reset();
invalidate();
}
return super.onTouchEvent(event);
}
public void setBitmp(Bitmap bitmap){
mCanvas2.drawBitmap(bitmap,0,0,null );
}
}
'android code' 카테고리의 다른 글
android 에서 lombok 설정하기 (0) | 2018.02.05 |
---|---|
PopupWindow 예제 (0) | 2016.08.23 |
canvas에서 selector 적용하기 (0) | 2016.08.10 |
안드로이드 파일 복사 퍼미션 (0) | 2016.07.31 |
서버에 이미지 업로드 하는 예제 (0) | 2016.07.29 |