在这个类中,我使用画布进行绘制,我想将其保存到设备上的位图图像中,以便以后查看。我还担心位置,因为我计划在同一个应用程序的图库视图中打开图像。
这是绘图类:
public class Drawing extends View {
private Paint paint = new Paint();
private Path path = new Path();
private ArrayList<Paint> paints = new ArrayList<Paint>();
private ArrayList<Path> paths = new ArrayList<Path>();
private int moodColor, brushColor = Color.WHITE;
Context context;
public Drawing(Context context) {
this( context, null);
context = this.context;
paints.add(paint);
paths.add(path);
}
public void setMoodColor(int moodColor) {
this.moodColor = moodColor;
switch (moodColor)
{
case 1:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.tense);
Toast.makeText(context, brushColor, Toast.LENGTH_SHORT).show();
}
case 2:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.nervous);
}
case 3:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.stressed);
}
case 4:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.upset);
}
case 5:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.alert);
}
case 6:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.excited);
}
case 7:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.elated);
}
case 8:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.happy);
}
case 9:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.sad);
}
case 10:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.depressed);
}
case 11:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.bored);
}
case 12:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.fatigued);
}
case 13:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.contented);
}
case 14:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.serene);
}
case 15:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.relaxed);
}
case 16:
{
brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.calm);
}
default:
brushColor = Color.BLACK;
//Toast.makeText(context, "Selected " + brushColor, Toast.LENGTH_SHORT).show();
}
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5f);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setColor(brushColor);
path = new Path();
paints.add(paint);
paths.add(path);
}
public Drawing(Context context, AttributeSet attrs) {
super(context, attrs);
this.setBackgroundColor(Color.WHITE);
paint.setColor(brushColor);
paint.setAntiAlias(true);
paint.setStrokeWidth(5f);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
Toast.makeText(context, "Click on 'Mood' to choose color", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY); return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY); break;
default:
return false;
}
invalidate(); return true;
}
}