/**
* 在圖片上作畫(huà)
*
* @author admin
* @time 2015年2月8日 上午1:12:18
*/
public class MainActivity extends Activity implements OnTouchListener {
private ImageView mImageView;
private Bitmap bitmap;
private Canvas canvas;
private float downX;
private float downY;
private Paint paint;
private EditText etPaintWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到控件
mImageView = (ImageView) findViewById(R.id.iv);
etPaintWidth = (EditText) findViewById(R.id.et_paintWidth);
// 為圖片控件設(shè)置觸摸事件
mImageView.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 按下
if (bitmap == null) {
// 創(chuàng)建一張空白圖片,寬,高和ImageView一樣
bitmap = Bitmap.createBitmap(mImageView.getWidth(),
mImageView.getHeight(), Config.ARGB_8888);
// 把空白圖片設(shè)置給Canvas畫(huà)畫(huà)板, canvas所繪制的東西都繪制到當(dāng)前圖片Bitmap上
canvas = new Canvas(bitmap);
// 創(chuàng)建一個(gè)畫(huà)筆
paint = new Paint();
// 設(shè)置畫(huà)筆的顏色
paint.setColor(Color.RED);
// 設(shè)置畫(huà)筆畫(huà)線時(shí)的粗細(xì)
String spaintWidth = etPaintWidth.getText().toString().trim();
int paintWidth = Integer.valueOf(spaintWidth);
paint.setStrokeWidth((float) paintWidth);
// 給畫(huà)布畫(huà)一個(gè)背景色
canvas.drawColor(Color.YELLOW);
}
downX = event.getX();
downY = event.getY();
System.out.println("按下了...:X:" + downX + ",Y:" + downY);
break;
case MotionEvent.ACTION_MOVE:
float moveX = event.getX();
float moveY = event.getY();
// 開(kāi)始畫(huà)線, 這一行代碼執(zhí)行完畢, Bitmap中有一條線了.
canvas.drawLine(downX, downY, moveX, moveY, paint);
mImageView.setImageBitmap(bitmap);
downX = moveX;
downY = moveY;
break;
case MotionEvent.ACTION_UP:
System.out.println("抬起了.......");
break;
}
return true;
}
/**
* 將手繪的圖片保存到SD卡中
*
* @param v
*/
public void save(View v) {
if (bitmap != null) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("/mnt/sdcard/hehe.jpg");
bitmap.compress(CompressFormat.JPEG, 100, fos);
Toast.makeText(this, "保存成功", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
<RelativeLayout 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" >
<EditText
android:id="@+id/et_paintWidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入畫(huà)筆的粗細(xì)度" />
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/et_paintWidth" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="save"
android:text="保存" />
</RelativeLayout>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者