Android简易实战教程--第二十四话《画画板》

今天完成一个画画板。

首先来个布局:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"
android:onClick="red"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿色"
android:onClick="green"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刷子"
android:onClick="brush"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:onClick="save"
/>
</LinearLayout>
</RelativeLayout>

可见,要分红绿色,而且还要保存最后画的图片。

看一下主活动代码:

package com.itydl.paintban;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; 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.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; int startX;
int startY; private Canvas canvas; private Paint paint; private Bitmap bmCopy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //加载画画板的背景图
Bitmap bmSrc = BitmapFactory.decodeResource(getResources(), R.drawable.bg); bmCopy = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
paint = new Paint();
canvas = new Canvas(bmCopy);
//绘制
canvas.drawBitmap(bmSrc, new Matrix(), paint); iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bmCopy); //设置触摸侦听
iv.setOnTouchListener(new OnTouchListener() { //触摸屏幕时,触摸事件产生时,此方法调用
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
//用户手指摸到屏幕
case MotionEvent.ACTION_DOWN:
startX = (int) event.getX();
startY = (int) event.getY();
break;
//用户手指正在滑动
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX();
int y = (int) event.getY();
canvas.drawLine(startX, startY, x, y, paint);
//每次绘制完毕之后,本次绘制的结束坐标变成下一次绘制的初始坐标
startX = x;
startY = y;
iv.setImageBitmap(bmCopy);
break;
//用户手指离开屏幕
case MotionEvent.ACTION_UP:
break; }
//true:告诉系统,这个触摸事件由我来处理
//false:告诉系统,这个触摸事件我不处理,这时系统会把触摸事件传递给imageview的父节点
return true;
}
}); } public void red(View v){
paint.setColor(Color.RED);
}
public void green(View v){
paint.setColor(Color.GREEN);
}
public void brush(View v){
paint.setStrokeWidth(7);
}
public void save(View v){
File file = new File("sdcard/dazuo.png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bmCopy.compress(CompressFormat.PNG, 100, fos); //发送sd卡就绪广播
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
sendBroadcast(intent);
}
}

运行看一下结果:

Android简易实战教程--第二十四话《画画板》

画了个绿帽子,哈哈。欢迎关注本博客点击打开链接  http://blog.csdn.net/qq_32059827,每天花上5分钟,阅读一篇有趣的安卓小文哦。

上一篇:POJ 3683 Priest John's Busiest Day (2-SAT)


下一篇:python创建tcp服务端和客户端