// 전체적인 소스 입니다.
package org.androidtown.tutorial.graphic;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* 부드러운 곡선으로 그려지도록 기능 추가
*
* @author Mike
*
*/
public class BestPaintBoardActivity extends Activity {
BestPaintBoard board;
Button colorBtn;
Button penBtn;
Button eraserBtn;
Button undoBtn;
Button saveBtn;
Button openBtn;
LinearLayout addedLayout;
Button colorLegendBtn;
TextView sizeLegendTxt;
int mColor = 0xff000000;
int mSize = 2;
int oldColor;
int oldSize;
boolean eraserSelected = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout toolsLayout = (LinearLayout) findViewById(R.id.toolsLayout);
LinearLayout boardLayout = (LinearLayout) findViewById(R.id.boardLayout);
colorBtn = (Button) findViewById(R.id.colorBtn);
penBtn = (Button) findViewById(R.id.penBtn);
eraserBtn = (Button) findViewById(R.id.eraserBtn);
undoBtn = (Button) findViewById(R.id.undoBtn);
saveBtn = (Button) findViewById(R.id.saveBtn);
openBtn = (Button) findViewById(R.id.openBtn);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
board = new BestPaintBoard(this);
board.setLayoutParams(params);
board.setPadding(2, 2, 2, 2);
boardLayout.addView(board);
// add legend buttons
LinearLayout.LayoutParams addedParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 48);
addedLayout = new LinearLayout(this);
addedLayout.setLayoutParams(addedParams);
addedLayout.setOrientation(LinearLayout.VERTICAL);
addedLayout.setPadding(8, 8, 8, 8);
LinearLayout outlineLayout = new LinearLayout(this);
outlineLayout.setLayoutParams(buttonParams);
outlineLayout.setOrientation(LinearLayout.VERTICAL);
outlineLayout.setBackgroundColor(Color.LTGRAY);
outlineLayout.setPadding(1, 1, 1, 1);
colorLegendBtn = new Button(this);
colorLegendBtn.setLayoutParams(buttonParams);
// colorLegendBtn.setText(" ");
colorLegendBtn.setBackgroundColor(mColor);
colorLegendBtn.setHeight(5);
outlineLayout.addView(colorLegendBtn);
addedLayout.addView(outlineLayout);
sizeLegendTxt = new TextView(this);
sizeLegendTxt.setLayoutParams(buttonParams);
// sizeLegendTxt.setText("Size : " + mSize);
sizeLegendTxt.setGravity(Gravity.CENTER_HORIZONTAL
| Gravity.CENTER_VERTICAL);
sizeLegendTxt.setTextSize(5);
sizeLegendTxt.setTextColor(Color.BLACK);
addedLayout.addView(sizeLegendTxt);
toolsLayout.addView(addedLayout);
colorBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ColorPaletteDialog.listener = new OnColorSelectedListener() {
public void onColorSelected(int color) {
mColor = color;
board.updatePaintProperty(mColor, mSize);
displayPaintProperty();
}
};
/** show color palette dialog */
Intent intent = new Intent(getApplicationContext(),
ColorPaletteDialog.class);
startActivity(intent);
}
});
penBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
PenPaletteDialog.listener = new OnPenSelectedListener() {
public void onPenSelected(int size) {
mSize = size;
board.updatePaintProperty(mColor, mSize);
displayPaintProperty();
}
};
// show pen palette dialog
Intent intent = new Intent(getApplicationContext(),
PenPaletteDialog.class);
startActivity(intent);
}
});
eraserBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
eraserSelected = !eraserSelected;
if (eraserSelected) {
colorBtn.setEnabled(false);
penBtn.setEnabled(false);
undoBtn.setEnabled(false);
saveBtn.setEnabled(false);
openBtn.setEnabled(false);
colorBtn.invalidate();
penBtn.invalidate();
undoBtn.invalidate();
oldColor = mColor;
oldSize = mSize;
mColor = Color.WHITE;
mSize = 15;
board.updatePaintProperty(mColor, mSize);
displayPaintProperty();
} else {
colorBtn.setEnabled(true);
penBtn.setEnabled(true);
undoBtn.setEnabled(true);
saveBtn.setEnabled(true);
openBtn.setEnabled(true);
colorBtn.invalidate();
penBtn.invalidate();
undoBtn.invalidate();
mColor = oldColor;
mSize = oldSize;
board.updatePaintProperty(mColor, mSize);
displayPaintProperty();
}
}
});
undoBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
board.undo();
}
});
saveBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// view가 업데이트 될때 뷰 이미지를 Drawing cache에 저장
board.setDrawingCacheEnabled(true);
board.buildDrawingCache(); // view 이미지 저장
// view 이미지를 bitmap 형태로 반환
Bitmap savebitmap = board.getDrawingCache();
Log.e("Save", savebitmap + "");
SimpleDateFormat formatter = new java.text.SimpleDateFormat(
"yy-MM-dd-h-mm");
Date today = new Date();
String strDate = formatter.format(today);
// 폴더 생성
String dirPath = "/sdcard/PaintBoard";
File file = new File(dirPath);
// 폴더 존재 유무 확인
if (!file.exists())
file.mkdirs();
// 파일 저장
try {
File hFile = new File("/sdcard/PaintBoard/" + strDate
+ ".png");
FileOutputStream fos;
fos = new FileOutputStream(hFile);
savebitmap.compress(CompressFormat.PNG, 70, fos);
fos.close();
Toast.makeText(getApplicationContext(), "저장되었습니다",
Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "저장실패",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "저장실패",
Toast.LENGTH_SHORT).show();
}
}
});
openBtn.setOnClickListener(new OnClickListener() {
int mSelect = 0;
String[] imgPath;
public void onClick(View v) {
imgPath = new String[5];
for (int i = 0; i < 5; i++) {
imgPath[i] = "/sdcard/PaintBoard/111.png";
}
new AlertDialog.Builder(BestPaintBoardActivity.this)
.setTitle("Open할 파일을 선택")
.setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(
new String[] { "CustomViewSave4",
"CustomViewSave3", "CustomViewSave2",
"CustomViewSave1", "CustomViewSave0" },
mSelect, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
mSelect = which;
}
})
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
switch (mSelect) {
case 0:
importCanvas(imgPath[4]);
break;
case 1:
importCanvas(imgPath[3]);
break;
case 2:
importCanvas(imgPath[2]);
break;
case 3:
importCanvas(imgPath[1]);
break;
case 4:
importCanvas(imgPath[0]);
break;
}
}
}).setNegativeButton("Cancel", null).show();
// TODO Auto-generated method stub
}
});
}
public void importCanvas(String path) {
Bitmap bit = BitmapFactory.decodeFile(path);
board.bit = bit;
board.invalidate();
Toast.makeText(getApplicationContext(), "불러오기", Toast.LENGTH_SHORT)
.show();
}
public int getChosenColor() {
return mColor;
}
public int getPenThickness() {
return mSize;
}
private void displayPaintProperty() {
colorLegendBtn.setBackgroundColor(mColor);
// sizeLegendTxt.setText("Size : " + mSize);
addedLayout.invalidate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
여기서 빨간부분으로 작성된 불러오기 기능을 구현하는 중인데 아무 이미지도 불러와지지 않네요.
혹시 무엇이 잘못 된 것일까요?