import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private View red, green, yellow, purple, blue;
private SeekBar seekbar;
private Paint paint;
private ImageView iv;
/**
* 一个可以被修改的图片
*/
private Bitmap alterBitmap;
/**
* 画板
*/
private Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alterBitmap = Bitmap.createBitmap(320, 320, Bitmap.Config.ARGB_8888);
canvas = new Canvas(alterBitmap);
paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawColor(Color.WHITE);
paint.setStrokeWidth(5);
seekbar = (SeekBar) findViewById(R.id.seekbar);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
paint.setStrokeWidth(progress);
Toast.makeText(MainActivity.this, "画笔宽度为:" + progress, 0)
.show();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
red = findViewById(R.id.red);
red.setOnClickListener(this);
green = findViewById(R.id.green);
green.setOnClickListener(this);
yellow = findViewById(R.id.yellow);
yellow.setOnClickListener(this);
purple = findViewById(R.id.purple);
purple.setOnClickListener(this);
blue = findViewById(R.id.blue);
blue.setOnClickListener(this);
iv = (ImageView) findViewById(R.id.iv);
iv.setOnTouchListener(new OnTouchListener() {
int startX;
int startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("摸到");
startX = (int) event.getX();
startY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
System.out.println("移动");
int newX = (int) event.getX();
int newY = (int) event.getY();
canvas.drawLine(startX, startY, newX, newY, paint);
iv.setImageBitmap(alterBitmap);
startX = (int) event.getX();
startY = (int) event.getY();
break;
case MotionEvent.ACTION_UP:
System.out.println("放手");
break;
}
return true;
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.red:
paint.setColor(Color.RED);
Toast.makeText(MainActivity.this, "画笔红色", 0).show();
break;
case R.id.yellow:
paint.setColor(Color.YELLOW);
Toast.makeText(MainActivity.this, "画笔黄色", 0).show();
break;
case R.id.green:
paint.setColor(Color.GREEN);
Toast.makeText(MainActivity.this, "画笔绿色", 0).show();
break;
case R.id.purple:
paint.setColor(0xffff00ff);
Toast.makeText(MainActivity.this, "画笔紫色", 0).show();
break;
case R.id.blue:
paint.setColor(Color.BLUE);
Toast.makeText(MainActivity.this, "画笔蓝色", 0).show();
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.item_save) {
try {
File file = new File(Environment.getExternalStorageDirectory(),
SystemClock.currentThreadTimeMillis() + ".jpg");
FileOutputStream stream = new FileOutputStream(file);
alterBitmap.compress(CompressFormat.JPEG, 100, stream);
stream.close();
Toast.makeText(this, "保存成功", 0).show();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
sendBroadcast(intent);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "保存失败", 0).show();
}
}
return super.onOptionsItemSelected(item);
}
}