涂鸦
public class MainActivity extends Activity implements OnClickListener {
private View v_red;
private View v_green;
private View v_blue;
private SeekBar seekBar;
private Button clear;
private ImageView iv;
private Paint paint;
private Bitmap newBitmap;
private Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newBitmap = Bitmap.createBitmap(320, 320, Bitmap.Config.ARGB_8888);
canvas = new Canvas(newBitmap);
canvas.drawColor(Color.WHITE);
v_blue = findViewById(R.id.blue);
v_green = findViewById(R.id.green);
v_red = findViewById(R.id.red);
seekBar = (SeekBar) findViewById(R.id.sb);
clear = (Button) findViewById(R.id.clear);
iv = (ImageView) findViewById(R.id.iv);
iv.setOnTouchListener(new OnTouchListener() {
int startX,startY,stopX,stopY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = (int) event.getX();
startY = (int) event.getY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
stopX = (int) event.getX();
stopY = (int) event.getY();
canvas.drawLine(startX, startY, stopX, stopY, paint);
iv.setImageBitmap(newBitmap);
startX = (int) event.getX();
startY = (int) event.getY();
break;
}
return true;
}
});
v_red.setOnClickListener(this);
v_green.setOnClickListener(this);
v_blue.setOnClickListener(this);
clear.setOnClickListener(this);
paint = new Paint();
paint.setStrokeWidth(10);
paint.setColor(Color.BLACK);
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) {
}
});
}
@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.green:
paint.setColor(Color.GREEN);
Toast.makeText(MainActivity.this, "当前画笔颜色为绿色", 0).show();
break;
case R.id.blue:
paint.setColor(Color.BLUE);
Toast.makeText(MainActivity.this, "当前画笔颜色为蓝色", 0).show();
break;
case R.id.clear:
canvas.drawColor(Color.WHITE);
iv.setImageBitmap(newBitmap);
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.save) {
try {
File file = new File(Environment.getExternalStorageDirectory(),SystemClock.currentThreadTimeMillis()+".jpg");
FileOutputStream stream = new FileOutputStream(file);
newBitmap.compress(CompressFormat.JPEG, 100, stream);
Toast.makeText(MainActivity.this, "保存成功", 0).show();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "保存失败", 0).show();
}
}
return super.onOptionsItemSelected(item);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择画笔颜色" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<View
android:id="@+id/red"
android:background="#ff0000"
android:layout_width="50dp"
android:layout_height="50dp"/>
<View
android:id="@+id/green"
android:layout_toRightOf="@id/red"
android:background="#00ff00"
android:layout_width="50dp"
android:layout_height="50dp"/>
<View
android:id="@+id/blue"
android:layout_toRightOf="@id/green"
android:background="#0000ff"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:layout_alignParentRight="true"
android:id="@+id/clear"
android:text="Clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout >
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="20"
android:progress="5" />
<ImageView
android:id="@+id/iv"
android:src="@drawable/ic_launcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/save"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>
修改图片的色调
public class MainActivity extends Activity {
private SeekBar seekBar;
private ImageView iv;
private Canvas canvas;
private Paint paint;
private Bitmap bitmap,newBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.sb);
iv = (ImageView) findViewById(R.id.iv);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int result = (int) (seekBar.getProgress()/50.0f);
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] {
1*result, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, new Matrix(), paint);
iv.setImageBitmap(newBitmap);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bb);
newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
canvas = new Canvas(newBitmap);
paint = new Paint();
canvas.drawBitmap(bitmap, new Matrix(), paint);
iv.setImageBitmap(newBitmap);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="青<------------------------------->红" />
<SeekBar
android:id="@+id/sb"
android:max="100"
android:progress="50"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iv"
android:src="@drawable/ic_launcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>