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"/>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值