0x00 :
模拟Windows画图软件,简单的画图和颜色选择
0x01 :
package com.itheima.scrawl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import com.itheima.scrawl.R;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
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.Matrix;
import android.graphics.Paint;
import android.util.Log;
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;
public class MainActivity extends Activity implements OnClickListener, OnSeekBarChangeListener, OnTouchListener {
private static final String logTAG = "MainActivity";
private Canvas canvas;
private Matrix matrix;
private Paint paint;
private Bitmap blankBitmap;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
// 1.查找原图
// 2.创建拷贝
//Log.v(logTAG, iv.getHeight() + " # " + iv.getWidth()); //需要等ImageView加载完成才能正确
blankBitmap = Bitmap.createBitmap(320, 307, Bitmap.Config.ARGB_8888);
// 3.创建画布
canvas = new Canvas(blankBitmap);
canvas.drawColor(Color.WHITE);
// 4.
matrix = new Matrix();
// 5.
paint = new Paint();
// 6.
canvas.drawBitmap(blankBitmap, matrix, paint);
findViewById(R.id.btn_Red).setOnClickListener(this);
findViewById(R.id.btn_Green).setOnClickListener(this);
findViewById(R.id.btn_Blue).setOnClickListener(this);
findViewById(R.id.btn_Purple).setOnClickListener(this);
findViewById(R.id.btn_Yellow).setOnClickListener(this);
((SeekBar)findViewById(R.id.sb_PaintWidth)).setOnSeekBarChangeListener(this);
iv.setOnTouchListener(this);
paint.setStrokeWidth(5);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int arg0, MenuItem arg1) {
switch (arg1.getItemId()) {
case R.id.menuItem_Clear:
Log.v(logTAG, "清空图像");
if (canvas == null || paint == null || matrix == null) {
Log.v(logTAG, "Clear Bitmap Error!");
return false;
}
canvas.drawColor(Color.WHITE);
iv.setImageBitmap(blankBitmap);
break;
case R.id.menuItem_Save:
Log.v(logTAG, "Save");
OutputStream outputstream;
try {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), + System.currentTimeMillis() + ".jpg");
outputstream = new FileOutputStream(file);
blankBitmap.compress(CompressFormat.JPEG, 100, outputstream);
outputstream.close();
// 4.4之后也可以使用!
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
} catch (IOException e) {
e.printStackTrace();
}
break;
default:
Log.v(logTAG, "Menu Error!");
break;
}
// // 4.4之后无法使用!
// Intent intent = new Intent();
// intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
// intent.setData(Uri.parse("file://"));
// sendBroadcast(intent);
return super.onMenuItemSelected(arg0, arg1);
}
//颜色选择器的点击事件
@Override
public void onClick(View v) {
if (paint == null)
return;
switch (v.getId()) {
case R.id.btn_Red:
paint.setColor(Color.RED);
Log.v(logTAG, "Red!");
break;
case R.id.btn_Green:
paint.setColor(Color.GREEN);
Log.v(logTAG, "Green!");
break;
case R.id.btn_Blue:
paint.setColor(Color.BLUE);
Log.v(logTAG, "Blue!");
break;
case R.id.btn_Purple:
paint.setColor(0xffff00ff);
Log.v(logTAG, "Purpel!");
break;
case R.id.btn_Yellow:
paint.setColor(Color.YELLOW);
Log.v(logTAG, "Yellow!");
break;
default:
Log.v(logTAG, "颜色选择错误!");
break;
}
}
//画笔粗细的点击事件
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// Log.v(logTAG, "onProgressChanged!");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Log.v(logTAG, "onStartTrackingTouch!");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.v(logTAG, "onStopTrackingTouch!");
paint.setStrokeWidth(seekBar.getProgress());
}
//画布的监听
float startx = 0;
float starty = 0;
float stopx = 0;
float stopy = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (canvas == null || paint == null || matrix == null) {
Log.v(logTAG, "init Error!");
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startx = event.getX();
starty = event.getY();
stopx = startx;
stopy = starty;
break;
case MotionEvent.ACTION_MOVE:
startx = stopx;
starty = stopy;
stopx = event.getX();
stopy = event.getY();
break;
case MotionEvent.ACTION_UP:
break;
default:
Log.v(logTAG, "获取焦点失败!");
break;
}
Log.v(logTAG, startx + " # " + starty + " # " + stopx + " # " + stopy);
canvas.drawLine(startx, starty, stopx, stopy, paint);
iv.setImageBitmap(blankBitmap);
return true;
}
}
0x03 :
参考:
http://www.jb51.net/article/46023.htm
http://blog.csdn.net/x605940745/article/details/18040027
//当拖动条发生变化时调用该方法
public void onProgressChanged(SeekBar seekBar,int progress,Boolean fromUser)
{System.out.println(progress);}
//当用户开始滑动滑块时调用该方法(即按下鼠调用一次)
public void onStartTrackingTouch(SeekBar seekBar){System.out.println(“start:=>”+seekBar.getProgress());}
//当用户结束对滑块滑动时,调用该方法(即松开鼠标)
public void onStopTrackingTouch (SeekBar seekBar)
{System.out.println(“stop:=>”+seekBar.getProgress());}}
为SeekBar绑定监听器:
seekBar.SetOnSeekBarChangeListener(new SeekBarListener());实现接口时用this代替new SeekBarListener())
http://blog.csdn.net/lsyz0021/article/details/51214082
//要想对菜单项设置点击事件需要复写一个方法
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
//获取当前点击的菜单项的Id
switch (item.getItemId()) {
case R.menu.action_back:
Toast.makeText(getApplicationContext(), "后退", 0).show();
break;
case R.menu.action_go:
Toast.makeText(getApplicationContext(), "前进", 0).show();
break;
case R.menu.refresh:
Toast.makeText(getApplicationContext(), "刷新", 0).show();
break;
}
return super.onMenuItemSelected(featureId, item);
}
http://www.cnblogs.com/salam/archive/2011/04/04/2005329.html