您的位置:首页 > 移动开发 > Android开发

Android基础学习笔记-简单实现绘画板

2018-01-27 09:13 465 查看
前言:这是一个初学者的笔记.....感觉在学校搞来搞去的一大堆课程,很多东西学了不久又会忘记,这个假期在家看视频学习,准备将学习过程的一些实例放到博客里,这样自己再查看代码也就方便一点了。
黑马Android视频学习笔记,Android简单实现绘画板笔记
主要涉及内容:OnTouchListener触摸监听、Canvas、Paint、Bitmap保存图片


界面截图




public class MainActivity extends Activity implements View.OnClickListener{

private ImageView iv;
private Paint paint;
private Canvas canvas;
private TextView tv_red;
private TextView tv_green;
private TextView tv_blue;
private Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = findViewById(R.id.iv);
tv_red = findViewById(R.id.tv_red);
tv_green = findViewById(R.id.tv_green);
tv_blue = findViewById(R.id.tv_blue);
tv_red.setOnClickListener(this);
tv_green.setOnClickListener(this);
tv_blue.setOnClickListener(this);

//1.获取屏幕大小
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
//2.创建一张和屏幕大小一样纸
bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
//3.创建画板,将纸放入
canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(10); //画笔宽度

iv.setOnTouchListener(new View.OnTouchListener() {
int startX;
int startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN: //按下动作
Log.d("MainActivity", "onTouch: 按下");
startX = (int) event.getRawX();  //获取按下位置距离屏幕左边缘的距离
startY = (int) event.getRawY();  //获取按下位置激励屏幕上边缘的距离
break;
case MotionEvent.ACTION_MOVE:  //移动动作
Log.d("MainActivity", "onTouch: 移动");
int endX = (int) event.getRawX(); //获取移动后的坐标
int endY = (int) event.getRawY();
//将起始坐标和画笔传入画板,画直线
canvas.drawLine(startX,startY,endX,endY,paint);
//重新定义起始位置
startX = (int) event.getRawX();
startY = (int) event.getRawY();
iv.setImageBitmap(bitmap);
break;
case MotionEvent.ACTION_UP:
Log.d("MainActivity", "onTouch: 放开");
}

return true;
}
});
}
/*
* TextView修改Paint画笔颜色*/
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_red:
paint.setColor(Color.RED);
break;
case R.id.tv_green:
paint.setColor(Color.GREEN);
break;
case R.id.tv_blue:
paint.setColor(Color.BLUE);
break;
}
}

//保存图片
public void saveImage(View v) {
try {
File file = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");     //Android4.0之后保存需要手动授权
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); //100表示图片输出质量
fos.close();
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
//模拟SD卡cherub事件
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
sendBroadcast(intent);
}catch (Exception e){
e.printStackTrace();
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
}


记得注册SD卡读写权限

布局文件代码


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zran.ontouch.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv"
android:background="#ffffff"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_red"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#ff0008"
/>
<TextView
android:id="@+id/tv_green"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#2bff00"
/>
<TextView
android:id="@+id/tv_blue"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#0400ff"
/>
</LinearLayout>
<Button
android:onClick="saveImage"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存图片"/>

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: