当前位置: 代码迷 >> Android >> Android(java)学习笔记238:多媒体之图片画画夹案例
  详细解决方案

Android(java)学习笔记238:多媒体之图片画画夹案例

热度:80   发布时间:2016-04-27 23:01:48.0
Android(java)学习笔记238:多媒体之图片画画板案例
1.首先我们编写布局文件activity_main.xml如下:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context="com.himi.painter.MainActivity" >
  6. <ImageView
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:id="@+id/iv" />
  10. </RelativeLayout>
2.初步编写MainActivity.java,如下:
  1. package com.himi.painter;
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.os.Bundle;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. import android.view.View.OnTouchListener;
  11. import android.widget.ImageView;
  12. public class MainActivity extends Activity {
  13. private ImageView iv;
  14. private Canvas canvas;
  15. private Paint paint;
  16. private Bitmap bitmap;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. iv = (ImageView) findViewById(R.id.iv);
  22. //创建一个空白的图片,以图片为模板创建一个画板
  23. bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
  24. //创建画板
  25. canvas = new Canvas(bitmap);
  26. //创建画笔
  27. paint = new Paint();
  28. paint.setColor(Color.BLACK);
  29. canvas.drawColor(0x22888888);
  30. iv.setImageBitmap(bitmap);
  31. iv.setOnTouchListener(new OnTouchListener() {
  32. //手在屏幕上的初始化坐标
  33. int startX;
  34. int startY;
  35. public boolean onTouch(View v, MotionEvent event) {
  36. switch (event.getAction()) {
  37. case MotionEvent.ACTION_DOWN://手指按下
  38. startX = (int) event.getX();
  39. startY = (int) event.getY();
  40. break;
  41. case MotionEvent.ACTION_MOVE://手指滑动
  42. int newX = (int) event.getX();
  43. int newY = (int) event.getY();
  44. canvas.drawLine(startX, startY, newX, newY, paint);
  45. //重新更新UI
  46. iv.setImageBitmap(bitmap);
  47. break;
  48. case MotionEvent.ACTION_UP://手指离开屏幕
  49. break;
  50. }
  51. return true ;//true ---表示监听事件处理完了, false---表示没有出来完监听事件
  52. }
  53. });
  54. }
  55. }
运行效果如下:

这个画板程序是有问题的,就是他开始固定了划线的起始点(就是我们刚刚接触屏幕的点),然后就像如图那样好像散射一样。这是不行的。我们要不断地更新我们的划线的起始点;

MainActivity修改如下:
  1. package com.himi.painter;
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.os.Bundle;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. import android.view.View.OnTouchListener;
  11. import android.widget.ImageView;
  12. public class MainActivity extends Activity {
  13. private ImageView iv;
  14. private Canvas canvas;
  15. private Paint paint;
  16. private Bitmap bitmap;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. iv = (ImageView) findViewById(R.id.iv);
  22. //创建一个空白的图片,以图片为模板创建一个画板
  23. bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
  24. //创建画板
  25. canvas = new Canvas(bitmap);
  26. //创建画笔
  27. paint = new Paint();
  28. paint.setColor(Color.BLACK);
  29. canvas.drawColor(0x22888888);
  30. iv.setImageBitmap(bitmap);
  31. iv.setOnTouchListener(new OnTouchListener() {
  32. //手在屏幕上的初始化坐标
  33. int startX;
  34. int startY;
  35. public boolean onTouch(View v, MotionEvent event) {
  36. switch (event.getAction()) {
  37. case MotionEvent.ACTION_DOWN://手指按下
  38. startX = (int) event.getX();
  39. startY = (int) event.getY();
  40. break;
  41. case MotionEvent.ACTION_MOVE://手指滑动
  42. int newX = (int) event.getX();
  43. int newY = (int) event.getY();
  44. canvas.drawLine(startX, startY, newX, newY, paint);
  45. //重新更新UI
  46. iv.setImageBitmap(bitmap);
  47. //很重要,重新给开始坐标赋值
  48. startX = (int) event.getX();
  49. startY = (int) event.getY();
  50. break;
  51. case MotionEvent.ACTION_UP://手指离开屏幕
  52. break;
  53. }
  54. return true ;//true ---表示监听事件处理完了, false---表示没有出来完监听事件
  55. }
  56. });
  57. }
  58. }
这时候画图板就正常了,如下:

3.画图板只能一种颜色,太单调,我们完善一下:
其中 activity_main.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context="com.himi.painter.MainActivity" >
  7. <ImageView
  8. android:id="@+id/iv"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content" />
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:orientation="horizontal" >
  15. <View
  16. android:id="@+id/red"
  17. android:layout_width="20dip"
  18. android:layout_height="20dip"
  19. android:background="#ff0000"/>
  20. <View
  21. android:id="@+id/green"
  22. android:layout_width="20dip"
  23. android:layout_height="20dip"
  24. android:background="#00ff00"/>
  25. <View
  26. android:id="@+id/blue"
  27. android:layout_width="20dip"
  28. android:layout_height="20dip"
  29. android:background="#0000ff" />
  30. </LinearLayout>
  31. <!-- android:max="20" 进度条范围对应于 这里是针对画笔粗细范围设置为0~20 -->
  32. <SeekBar
  33. android:id="@+id/seekBar1"
  34. android:max="100"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content" />
  37. <Button
  38. android:onClick="save"
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content"
  41. android:text="保存图片"/>
  42. </LinearLayout>

其次是MainActivity.java
  1. package com.himi.painter;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.graphics.Bitmap.CompressFormat;
  8. import android.graphics.Canvas;
  9. import android.graphics.Color;
  10. import android.graphics.Paint;
  11. import android.net.Uri;
  12. import android.os.Bundle;
  13. import android.os.Environment;
  14. import android.os.SystemClock;
  15. import android.view.MotionEvent;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.view.View.OnTouchListener;
  19. import android.widget.ImageView;
  20. import android.widget.SeekBar;
  21. import android.widget.SeekBar.OnSeekBarChangeListener;
  22. import android.widget.Toast;
  23. public class MainActivity extends Activity implements OnClickListener {
  24. private ImageView iv;
  25. private Canvas canvas;
  26. private Paint paint;
  27. private Bitmap bitmap;
  28. private View red,green,blue;
  29. private SeekBar seekBar1;
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. iv = (ImageView) findViewById(R.id.iv);
  35. seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
  36. red = (View) findViewById(R.id.red);
  37. green = (View) findViewById(R.id.green);
  38. blue = (View) findViewById(R.id.blue);
  39. red.setOnClickListener(this);
  40. green.setOnClickListener(this);
  41. blue.setOnClickListener(this);
  42. seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  43. public void onStopTrackingTouch(SeekBar seekBar) {//停止拖动进度条,调用的方法
  44. int size = seekBar.getProgress();
  45. //设置画笔的粗细
  46. paint.setStrokeWidth(size);
  47. }
  48. public void onStartTrackingTouch(SeekBar seekBar) {//刚刚接触进度条,调用的方法
  49. // TODO 自动生成的方法存根
  50. }
  51. public void onProgressChanged(SeekBar seekBar, int progress,
  52. boolean fromUser) {//拖动进度条过程中,调用的方法
  53. // TODO 自动生成的方法存根
  54. }
  55. });
  56. //创建一个空白的图片,以图片为模板创建一个画板
  57. bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
  58. //创建画板
  59. canvas = new Canvas(bitmap);
  60. //创建画笔
  61. paint = new Paint();
  62. paint.setColor(Color.BLACK);
  63. canvas.drawColor(0x22888888);
  64. iv.setImageBitmap(bitmap);
  65. iv.setOnTouchListener(new OnTouchListener() {
  66. //手在屏幕上的初始化坐标
  67. int startX;
  68. int startY;
  69. public boolean onTouch(View v, MotionEvent event) {
  70. switch (event.getAction()) {
  71. case MotionEvent.ACTION_DOWN://手指按下
  72. startX = (int) event.getX();
  73. startY = (int) event.getY();
  74. break;
  75. case MotionEvent.ACTION_MOVE://手指滑动
  76. int newX = (int) event.getX();
  77. int newY = (int) event.getY();
  78. canvas.drawLine(startX, startY, newX, newY, paint);
  79. //重新更新UI
  80. iv.setImageBitmap(bitmap);
  81. //很重要,重新给开始坐标赋值
  82. startX = (int) event.getX();
  83. startY = (int) event.getY();
  84. break;
  85. case MotionEvent.ACTION_UP://手指离开屏幕
  86. break;
  87. }
  88. return true ;//true ---表示监听事件处理完了, false---表示没有出来完监听事件
  89. }
  90. });
  91. }
  92. public void onClick(View v) {
  93. switch (v.getId()) {
  94. case R.id.red:
  95. paint.setColor(Color.RED);
  96. Toast.makeText(this, "设置画笔颜色为红色", 0).show();
  97. break;
  98. case R.id.green:
  99. paint.setColor(Color.GREEN);
  100. Toast.makeText(this, "设置画笔颜色为绿色", 0).show();
  101. break;
  102. case R.id.blue:
  103. paint.setColor(Color.BLUE);
  104. Toast.makeText(this, "设置画笔颜色为蓝色", 0).show();
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. //保存图片
  111. public void save(View view) {
  112. try {
  113. File file = new File(Environment.getExternalStorageDirectory(),SystemClock.uptimeMillis()+".png");
  114. FileOutputStream stream = new FileOutputStream(file);
  115. //第一个参数是保存的图片类型;第二个参数是画质(100是完美画质),第三个参数是输出流
  116. //保存图片类型为PNG支持透明度
  117. bitmap.compress(CompressFormat.PNG, 100, stream);
  118. stream.close();
  119. Toast.makeText(this, "保存成功,文件路径为:"+file.getAbsolutePath(), 0).show();
  120. //Android手机系统自带"图库",里面只有开机的时候 或者是 插入SD卡时候才会扫描里面的资源,这里我们可以欺骗系统,模拟发送SD插入的广播给系统
  121. Intent intent = new Intent();
  122. intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
  123. intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
  124. sendBroadcast(intent);
  125. } catch (Exception e) {
  126. // TODO 自动生成的 catch 块
  127. e.printStackTrace();
  128. Toast.makeText(this, "保存失败,请检查路径", 0).show();
  129. }
  130. }
  131. }







  相关解决方案