Bitmap2
画布
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = (ImageView) this.findViewById(R.id.iv);
// 获取一张背景图片
Bitmap bitmap = BitmapFactory
.decodeFile("mnt/sdcard/DCIM/Camera/IMG_20131121_162239.jpg");
// 创建一张空白图片
Bitmap blankBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.RGB_565);
// 创建一个画布
Canvas canvas = new Canvas(blankBitmap);
// 在画布上画上背景图
canvas.drawBitmap(bitmap, new Matrix(), new Paint());
// 创建一只画笔
Paint paint = new Paint();
paint.setTextSize(200);
paint.setColor(Color.RED);
// 在画布上添加文字
canvas.drawText("伟哥威武!!", 260, 260, paint);
iv.setImageBitmap(blankBitmap);
}
}
图片画画板
public class MainActivity extends Activity {
private ImageView iv;
private Bitmap baseBitmap;
private Canvas canvas;
private Paint paint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.iv = (ImageView) this.findViewById(R.id.iv);
// 创建一张空白图片
baseBitmap = Bitmap.createBitmap(480, 640, Bitmap.Config.ARGB_8888);
// 创建一张画布
canvas = new Canvas(baseBitmap);
// 画布背景为灰色
canvas.drawColor(Color.GRAY);
// 创建画笔
paint = new Paint();
// 画笔颜色为红色
paint.setColor(Color.RED);
// 宽度5个像素
paint.setStrokeWidth(5);
iv.setOnTouchListener(new OnTouchListener() {
int startX;
int startY;
@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_MOVE:
// 获取手移动后的坐标
int stopX = (int) event.getX();
int stopY = (int) event.getY();
// 在开始和结束坐标间画一条线
canvas.drawLine(startX, startY, stopX, stopY, paint);
// 实时更新开始坐标
startX = (int) event.getX();
startY = (int) event.getY();
iv.setImageBitmap(baseBitmap);
break;
}
return true;
}
});
}
public void save(View view) {
try {
File file = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
OutputStream stream = new FileOutputStream(file);
baseBitmap.compress(CompressFormat.JPEG, 100, stream);
stream.close();
// 模拟一个广播,通知系统sdcard被挂载
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment
.getExternalStorageDirectory()));
sendBroadcast(intent);
Toast.makeText(this, "保存图片成功", 0).show();
} catch (Exception e) {
Toast.makeText(this, "保存图片失败", 0).show();
e.printStackTrace();
}
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
扒掉人物衣服
public class MainActivity extends Activity {
private ImageView iv_after;
private ImageView iv_before;
private Bitmap beforeBitmap;
private Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.iv_after = (ImageView) this.findViewById(R.id.after);
this.iv_before = (ImageView) this.findViewById(R.id.before);
// decodeResource()创建出来的图片属性为只读
Bitmap after = BitmapFactory.decodeResource(getResources(),
R.drawable.after);
Bitmap before = BitmapFactory.decodeResource(getResources(),
R.drawable.before);
// 创建一张可编辑的空白图片
this.beforeBitmap = Bitmap.createBitmap(before.getWidth(),
before.getHeight(), before.getConfig());
this.canvas = new Canvas(beforeBitmap);
this.canvas.drawBitmap(before, new Matrix(), new Paint());
this.iv_after.setImageBitmap(after);
this.iv_before.setImageBitmap(beforeBitmap);
this.iv_before.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX();
int y = (int) event.getY();
for (int i = -18; i <= 18; i++) {
for (int j = -18; j <= 18; j++) {
if (x + i >= 0 && y + j >= 0
&& x + i < beforeBitmap.getWidth()
&& y + j < beforeBitmap.getHeight()) {
beforeBitmap.setPixel(x + i, y + j,
Color.TRANSPARENT);
}
}
}
iv_before.setImageBitmap(beforeBitmap);
break;
}
return true;
}
});
}
}
<FrameLayout 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"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/after"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/before"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
posted on 2013-03-28 11:52 Freedom000 阅读(130) 评论(0) 编辑 收藏 举报