Android画图demo
如何在图片上画画呢?这里写了一个demo,供大家参考
一、先看一眼工程结构
工程结构:
二、自定义view
这个自定义view实现了保留轨迹的功能,代码如下
package picturegame.view;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.winton.picturegame.R;public class GameView extends View{private Paint paint = null; //private Bitmap originalBitmap = null;//原始图private Bitmap new1Bitmap = null;private Bitmap new2Bitmap = null;private float clickX =0;private float clickY=0;private float startX=0;private float startY=0;private boolean isMove = true;private boolean isClear = false;private int color =Color.RED;//默认画笔颜色为红色private float strokeWidth =2.0f;//默认画笔粗度public GameView(Context context) {this(context,null);// TODO Auto-generated constructor stub}public GameView(Context context,AttributeSet atts) {this(context,atts,0);// TODO Auto-generated constructor stub}public GameView(Context context,AttributeSet atts,int defStyle) {super(context,atts,defStyle);// TODO Auto-generated constructor stuboriginalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.default_pic).copy(Bitmap.Config.ARGB_8888, true);//加载一张背景new1Bitmap=originalBitmap.createBitmap(originalBitmap);}//清除函数public void clear(){isClear =true;new2Bitmap=originalBitmap.createBitmap(originalBitmap);invalidate();//重载}public void setStrokeWidth(float width){this.strokeWidth=width;initPaint();}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);canvas.drawBitmap(writer(new1Bitmap),0,0, null);}@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubclickX =event.getX();clickY=event.getY();if(event.getAction()==MotionEvent.ACTION_DOWN){isMove =false;invalidate();return true;}else if(event.getAction()==MotionEvent.ACTION_MOVE){isMove =true;invalidate();return true;}return super.onTouchEvent(event);}/** * @Title: writer * @Description: TODO(生成bitmap) * @param @param pic* @param @return 设定文件 * @return Bitmap 返回类型 * @throws */public Bitmap writer(Bitmap pic){initPaint();Canvas canvas =null;if(isClear){canvas=new Canvas(new2Bitmap);}else{canvas=new Canvas(pic);}if(isMove){canvas.drawLine(startX, startY, clickX, clickY, paint);//划线}startX = clickX;startY =clickY;if(isClear){return new2Bitmap;}return pic;}private void initPaint(){paint = new Paint();//新建画笔paint.setStyle(Style.STROKE);//设置为画线paint.setAntiAlias(true);//可以让线条圆滑一些paint.setColor(color);//设置画笔颜色paint.setStrokeWidth(strokeWidth);//设置画笔线条的粗细}/** * @Title: setColor * @Description: TODO(设置线条颜色的对外接口) * @param @param color 设定文件 * @return void 返回类型 * @throws */public void setColor(int color){this.color=color;initPaint();}}
三、主页面布局文件
主页面布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_horizontal"android:orientation="vertical" ><LinearLayout android:layout_width="match_parent"android:layout_height="50dp" android:orientation="horizontal" ><LinearLayout android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"><TextView android:id="@+id/tv_30"android:layout_width="30dp"android:layout_height="30dp"android:background="@drawable/bg_notifaction"/></LinearLayout><LinearLayout android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"><TextView android:id="@+id/tv_25"android:layout_width="25dp"android:layout_height="25dp"android:background="@drawable/bg_notifaction"/></LinearLayout><LinearLayout android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"><TextView android:id="@+id/tv_20"android:layout_width="20dp"android:layout_height="20dp"android:background="@drawable/bg_notifaction"/></LinearLayout><LinearLayout android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"><TextView android:id="@+id/tv_15"android:layout_width="15dp"android:layout_height="15dp"android:background="@drawable/bg_notifaction"/></LinearLayout><LinearLayout android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"><TextView android:id="@+id/tv_10"android:layout_width="10dp"android:layout_height="10dp"android:background="@drawable/bg_notifaction"/></LinearLayout><LinearLayout android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"><TextView android:id="@+id/tv_5"android:layout_width="5dp"android:layout_height="5dp"android:background="@drawable/bg_notifaction"/></LinearLayout><LinearLayout android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"><TextView android:id="@+id/tv_2"android:layout_width="2dp"android:layout_height="2dp"android:background="@drawable/bg_notifaction"/></LinearLayout></LinearLayout><picturegame.view.GameView android:layout_width="match_parent"android:layout_height="300dp"android:id="@+id/gameview"/><Button android:layout_width="200dp"android:layout_height="80dp"android:text="clear"android:textColor="@color/black"android:id="@+id/btn_clear"/></LinearLayout>
四、主Activity代码
package com.winton.picturegame;import picturegame.view.GameView;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;import com.winton.basemodule.BaseActivity;public class MainActivity extends BaseActivity implements OnClickListener {private GameView gameview = null;private Button clear = null;private TextView tv30,tv25,tv20,tv15,tv10,tv5,tv2;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic void initView() {// TODO Auto-generated method stubsetContentView(R.layout.activity_main);gameview=(GameView)findViewById(R.id.gameview);clear =(Button)findViewById(R.id.btn_clear);tv30=(TextView)findViewById(R.id.tv_30);tv25=(TextView)findViewById(R.id.tv_25);tv20=(TextView)findViewById(R.id.tv_20);tv15=(TextView)findViewById(R.id.tv_15);tv10=(TextView)findViewById(R.id.tv_10);tv5=(TextView)findViewById(R.id.tv_5);tv2=(TextView)findViewById(R.id.tv_2);}@Overridepublic void initListener() {// TODO Auto-generated method stubclear.setOnClickListener(this);tv30.setOnClickListener(this);tv25.setOnClickListener(this);tv20.setOnClickListener(this);tv15.setOnClickListener(this);tv10.setOnClickListener(this);tv5.setOnClickListener(this);tv2.setOnClickListener(this);}@Overridepublic void initData() {// TODO Auto-generated method stub}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(v==clear){gameview.clear();return;}if(v==tv30){gameview.setStrokeWidth(30f);return;}if(v==tv25){gameview.setStrokeWidth(25f);return;}if(v==tv20){gameview.setStrokeWidth(20f);return;}if(v==tv15){gameview.setStrokeWidth(15f);return;}if(v==tv10){gameview.setStrokeWidth(10f);return;}if(v==tv5){gameview.setStrokeWidth(5f);return;}if(v==tv2){gameview.setStrokeWidth(2f);return;}}}
五、效果
运行效果图如下
六、疑问
当线条变粗时,线条会出现如上图中不连续的问题。请问高手这个怎么处理呢?我猜测应该要运行算法,但还不知道怎么运行。
Android画图demo相关推荐
- Android画图方式
本文转载编辑自http://blog.chinaunix.net/u3/99423/showart_2203599.html Android画图方式 Android里面的画图分为2D和3D两种: 2D ...
- Android画图最基本的三个对象(Color,Paint,Canvas)
Android画图最基本的三个对象(Color,Paint,Canvas) 三个类都存放在 android.graphics包下 1) Color :颜色对象,相当于现实生活中的 '调料' 2) Pa ...
- Android API Demo程序框架
前言 android API Demo程序主要演示了Android常见API的使用方法, 本文将该程序的框架提取出来, 以作为自己研究和学习Android编程的一个示例收集框架,即将所有自己研究过的例 ...
- Android画图学习总结(四)——Animation(中)
在Android画图学习总结(四)--Animation(上)中详细介绍了Tween Animation的定义.使用,由于篇幅有限,很多中重要的方面没有说明,这篇文章一方面做个完整的总结说明,另外一方 ...
- Android 系统(254)---Android libphonenumber Demo 手机号码归属地
Android libphonenumber Demo 手机号码归属地 libphonenumber 是google 开源的库,提供手机号码格式化,来电归属地,运营商等多种功能十分强大,现在做个简单的 ...
- android demo示例代码,Android Service demo例子使用详解(示例代码)
Android Service demo例子使用详解\ 概述 Service 是 Android 的四大组件之一,它主要的作用是后台执行操作,Activity 属于带有 UI 界面跟用户进行交互,而 ...
- 2D Pose人体关键点实时检测(Python/Android /C++ Demo)
2D Pose人体关键点实时检测(Python/Android /C++ Demo) 目录 2D Pose人体关键点实时检测(Python/Android /C++ Demo) 1.人体关键点数据集 ...
- android 画图一
学习Android 有一段时间了,看完了Android SDK中的大部分文档,但是始终感觉自己还缺少很多,后来发现,Android SDK中只是介绍了Google自己开发的那一部分如何使用,Andro ...
- Android TV Demo 工程,其中包含 TV 常用的自定义控件,飞框效果实现,外边框效果实现,UI 控件焦点自动处理,使 TV 开发更简单,更高效。
TVLibraryDemo 项目地址:zhangtiansheng/TVLibraryDemo 简介:Android TV Demo 工程,其中包含 TV 常用的自定义控件,飞框效果实现,外边框效果 ...
最新文章
- springcloud(七):配置中心svn示例和refresh
- tf.boolean_mask
- x64dbg 修改为dll_c++笔记(dll 注入的实现)
- 从底层重学 Java 之两大浮点类型 GitChat链接
- Oracle中对象权限与系统权限revoke
- xp计算机管理窗口,XP系统设备管理器的打开技巧
- 房屋户型图设计工具Room Arranger for Mac
- 工厂物资管理E-R图
- 1.3.1 计算机的主要性能指标 (机器字长、数据通路带宽、主存容量、运算速度、吞吐量、响应时间、主频和时钟周期、CPI、CPU执行时间、MIPS、MFLOPS、GFLOPS、TFLOPS)
- 跟着团子学SAP PS:SAP PS模块常用报表介绍及增强建议
- Vue路由,关于地址栏没有问题,router-view是一片空白
- html中style写啥,style标签的作用
- python3 输出 26个大小写英文字母
- 2012 r2 万能网卡驱动_6款USB无线网卡评测
- svm公式推导及理解
- win10系统重装之u盘装系统,u盘安装win10系统
- 使用VUE做的个人简历
- 神经网络架构搜索(NAS)综述
- 形容人的内核是什么意思_请问甜文的内核是什么?
- 【情报百科】如何利用光影定位美国军事基地?