Android画画板的制作方法

所属分类: 软件编程 / Android 阅读数: 71
收藏 0 赞 0 分享

本文实例为大家分享了Android画画板展示的具体代码,供大家参考,具体内容如下

main.xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
  android:layout_width="match_parent" android:layout_height="match_parent"
  tools:context="com.example.demo.MainActivity">

  <ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/bg"
    />

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    >
    <Button
      android:id="@+id/red"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="红色"
      android:onClick="onplay"
      />
    <Button
      android:id="@+id/green"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="绿色"
      android:onClick="onplay"
      />
    <Button
      android:id="@+id/root"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="刷子"
      android:onClick="onplay"
      />
    <Button
      android:id="@+id/save"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="保存"
      android:onClick="onplay"
      />
    <Button
      android:id="@+id/finish"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="涂漆"
      android:onClick="onplay"
      />
  </LinearLayout>

</RelativeLayout>

main布局

/*
画板canvas  画板paint 手势识别器
整体思路:因为我是图片是作画,实际是对图片进行修改,起到画图的效果
1.原图,白纸,画笔,画板
2.根据手势识别进行作画

 */
public class MainActivity extends AppCompatActivity {
private Bitmap bitmap;
  private Canvas canvas;
private ImageView iv;
  private int startx;
  private int starty;
  private Paint paint;
  private Bitmap bmSrc;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //加载原图
    bmSrc = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
    //创建白纸,宽,高,图片的参数
     bitmap = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
    //创建画板,参数是白纸对象
    canvas = new Canvas(bitmap);
    //创建画笔
    paint = new Paint();
    //在纸上作画
    iv=(ImageView)findViewById(R.id.iv);
    canvas.drawBitmap(bmSrc,new Matrix(), paint);
    //-----------------手势识别器和画笔结合的知识-------------------
  //给控件设置手势适配器,可以得到用户在这个控件上所做的手势
    iv.setOnTouchListener(new View.OnTouchListener() {


      //当用户手在这个控件时,指的就是用户的手对控件滑动,按下,松开的三种场景,自动回调
      @Override
      public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()){
          case MotionEvent.ACTION_DOWN://按下时回调一次
            //获取用户手指按下时的坐标
            startx = (int) motionEvent.getX();
            starty = (int) motionEvent.getY();
            break;
          case MotionEvent.ACTION_MOVE://手指滑动时,不停地调用
            int newx = (int) motionEvent.getX();
            int newy = (int) motionEvent.getY();
            //在背景图画线
            canvas.drawLine(startx,starty,newx,newy, paint);
            startx=newx;
            starty=newy;
            iv.setImageBitmap(bitmap);
            break;
          case MotionEvent.ACTION_UP://松开时回调一次

            break;
        }
        //事情分发机制
        //true:iv处理该触摸事件
        //false:iv不处理该触摸事件,事件传递给上一级
        return true;
      }
    });
  }
  public void onplay(View view){
   switch (view.getId()){
     case R.id.red:
       paint.setColor(Color.RED);
       break;
     case R.id.green:
       paint.setColor(Color.GREEN);
       break;
     case R.id.root:
       paint.setStrokeWidth(5);
       break;
     case R.id.save:
       if(SaveViewUtil.saveScreen(iv)){
         Toast.makeText(this, "截图成功", Toast.LENGTH_SHORT).show();
       }else{
         Toast.makeText(this, "截图失败,请检查sdcard是否可用", Toast.LENGTH_SHORT).show();
       }
       break;
     //涂漆
     case R.id.finish:
       canvas.drawRect(new Rect(0,0,width,height), paint);
       break;
     }

   }
  }

这是一个把画的图存储SD卡的工具类

public class SaveViewUtil {
  
  private static final File rootDir = new File(Environment.getExternalStorageDirectory()+File.separator);

  /**保存截图的方法*/
  public static boolean saveScreen(View view){
   //判断sdcard是否可用
   if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
     return false;
   }
   if(!rootDir.exists()){
     rootDir.mkdir();
   }
   view.setDrawingCacheEnabled(true);
   view.buildDrawingCache();
   Bitmap bitmap = view.getDrawingCache();
   try {
     bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(new File(rootDir,System.currentTimeMillis()+".jpg")));
     return true;
   } catch (FileNotFoundException e) {
     e.printStackTrace();
     return false;
   }finally{
     view.setDrawingCacheEnabled(false);
     bitmap = null;
   }
  }
}
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多