温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android如何制作画画板

发布时间:2021-06-22 11:24:26 来源:亿速云 阅读:143 作者:小新 栏目:移动开发

这篇文章主要为大家展示了“Android如何制作画画板”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Android如何制作画画板”这篇文章吧。

本文实例为大家分享了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如何制作画画板”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI