Android实现手绘功能
本文实例为大家分享了Android实现手绘功能的具体代码,供大家参考,具体内容如下
布局文件如下
<?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.example.administrator.main.DrawActivity"> <ImageView android:id="@+id/iv" android:layout_width="1200px" android:layout_height="1500px" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentStart="true" /> <LinearLayout android:id="@+id/linearLayout4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_gravity="center_horizontal" android:orientation="horizontal"> </LinearLayout> <Button android:id="@+id/btn_resume" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_marginEnd="79dp" android:text="重绘" /> <Button android:id="@+id/btn_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/linearLayout4" android:layout_marginStart="91dp" android:layout_toEndOf="@+id/linearLayout4" android:text="保存" /> </RelativeLayout>
Activity代码如下,其中线的颜色,宽度等属性都可以修改。
package com.example.administrator.main; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.net.Uri; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; public class DrawActivity extends AppCompatActivity { private ImageView iv; private Bitmap baseBitmap; private Button btn_resume; private Button btn_save; private Canvas canvas; private Paint paint; float radio; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_draw); radio = 10; iv = (ImageView) findViewById(R.id.iv); // 初始化一个画笔,笔触宽度为5,颜色为红色 paint = new Paint(); paint.setStrokeWidth(radio); paint.setColor(Color.BLACK); iv = (ImageView) findViewById(R.id.iv); btn_resume = (Button) findViewById(R.id.btn_resume); btn_save = (Button) findViewById(R.id.btn_save); btn_resume.setOnClickListener(click); btn_save.setOnClickListener(click); iv.setOnTouchListener(touch); } private View.OnTouchListener touch = new View.OnTouchListener() { // 定义手指开始触摸的坐标 float startX; float startY; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { // 用户按下动作 case MotionEvent.ACTION_DOWN: // 第一次绘图初始化内存图片,指定背景为白色 if (baseBitmap == null) { baseBitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.ARGB_8888); canvas = new Canvas(baseBitmap); canvas.drawColor(Color.WHITE); } // 记录开始触摸的点的坐标 startX = event.getX(); startY = event.getY(); break; // 用户手指在屏幕上移动的动作 case MotionEvent.ACTION_MOVE: // 记录移动位置的点的坐标 float stopX = event.getX(); float stopY = event.getY(); Thread t = new Thread(new Runnable() { @Override public void run() { radio += 0.1; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); t.start(); paint.setStrokeWidth(radio); //根据两点坐标,绘制连线 canvas.drawLine(startX, startY, stopX, stopY, paint); // 更新开始点的位置 startX = event.getX(); startY = event.getY(); // 把图片展示到ImageView中 iv.setImageBitmap(baseBitmap); break; case MotionEvent.ACTION_UP: radio = 5; break; default: break; } return true; } }; private View.OnClickListener click = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_save: saveBitmap(); break; case R.id.btn_resume: resumeCanvas(); break; default: break; } } }; /** * 保存图片到SD卡上 */ protected void saveBitmap() { try { // 保存图片到SD卡上 String fileName = "/sdcard/"+System.currentTimeMillis() + ".png"; File file = new File(fileName); FileOutputStream stream = new FileOutputStream(file); baseBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); Toast.makeText(DrawActivity.this, "保存图片成功", Toast.LENGTH_SHORT).show(); // Android设备Gallery应用只会在启动的时候扫描系统文件夹 // 这里模拟一个媒体装载的广播,用于使保存的图片可以在Gallery中查看 Intent intent = new Intent(); intent.setAction(Intent.ACTION_MEDIA_MOUNTED); intent.setData(Uri.fromFile(Environment .getExternalStorageDirectory())); sendBroadcast(intent); } catch (Exception e) { Toast.makeText(DrawActivity.this, "保存图片失败", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } // 手动清除画板的绘图,重新创建一个画板 protected void resumeCanvas() { if (baseBitmap != null) { baseBitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.ARGB_8888); canvas = new Canvas(baseBitmap); canvas.drawColor(Color.WHITE); iv.setImageBitmap(baseBitmap); Toast.makeText(DrawActivity.this, "清除画板成功,可以重新开始绘图", Toast.LENGTH_SHORT).show(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。