Android实现简单画图画板
本文实例为大家分享了Android实现简单画图画板的具体代码,供大家参考,具体内容如下
效果如图:
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:id="@+id/iv" android:layout_width="600px" android:layout_height="900px" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentStart="true" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_gravity="center_horizontal" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="red" android:text="红色" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="green" android:text="绿色" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="brush" android:text="刷子" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="eraser" android:text="橡皮擦" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="save" android:text="保存" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="cancel" android:text="取消" /> </LinearLayout> </RelativeLayout>
MainActivity.java
package com.example.yulongji.android10; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class MainActivity extends Activity { private ImageView iv; //原图 private Bitmap bitsrc; //拷贝图 private Bitmap bitcopy; private Canvas canvas; private Paint paint; private int startX; private int startY; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); setBitmap(); // 设置触摸侦听 iv.setOnTouchListener(new View.OnTouchListener() { // 触摸屏幕时,触摸事件产生时,此方法调用 @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { // 用户手指摸到屏幕 case MotionEvent.ACTION_DOWN: startX = (int) event.getX(); startY = (int) event.getY(); break; // 用户手指正在滑动 case MotionEvent.ACTION_MOVE: int x = (int) event.getX(); int y = (int) event.getY(); canvas.drawLine(startX, startY, x, y, paint); // 每次绘制完毕之后,本次绘制的结束坐标变成下一次绘制的初始坐标 startX = x; startY = y; iv.setImageBitmap(bitcopy); break; // 用户手指离开屏幕 case MotionEvent.ACTION_UP: break; } // true:告诉系统,这个触摸事件由我来处理 // false:告诉系统,这个触摸事件我不处理,这时系统会把触摸事件传递给imageview的父节点 return true; } }); } public void setBitmap() { // 加载画画板的背景图 bitsrc = BitmapFactory.decodeResource(getResources(), R.drawable.bg); // 创建图片副本 // 1.在内存中创建一个与原图一模一样大小的bitmap对象,创建与原图大小一致的白纸 bitcopy = Bitmap.createBitmap(bitsrc.getWidth(), bitsrc.getHeight(), bitsrc.getConfig()); // 2.创建画笔对象 paint = new Paint(); // 3.创建画板对象,把白纸铺在画板上 canvas = new Canvas(bitcopy); // 4.开始作画,把原图的内容绘制在白纸上 canvas.drawBitmap(bitsrc, new Matrix(), paint); // 5.将绘制的图放入imageview中 iv.setImageBitmap(bitcopy); } public void red(View view){ paint.setColor(Color.RED); } public void green(View view){ paint.setColor(Color.GREEN); } public void brush(View view){ paint.setStrokeWidth(8); } public void cancel(View view){ setBitmap(); } public void eraser(View view){ paint.setColor(Color.rgb(243,243,243)); paint.setStrokeWidth(30); } public void save(View view){ String path = Environment.getExternalStorageDirectory() + "/" + "2.png"; File file = new File(path); try { FileOutputStream fos = new FileOutputStream(file); bitcopy.compress(Bitmap.CompressFormat.PNG, 100, fos); } catch (FileNotFoundException e) { e.printStackTrace(); } // 发送sd卡就绪广播 Intent intent = new Intent(); intent.setAction(Intent.ACTION_MEDIA_MOUNTED); intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory())); sendBroadcast(intent); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)