问题

I have a custom layout to draw a line based on touch input. I have it drawing the line but when the user touches the screen, the line disapeers and it draws a new line. What I want it to do is to draw a new line and leave the previous line there.

Here is my code:

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

public class DrawView extends View {

Paint paint = new Paint();

float startX;

float startY;

float stopX;

float stopY;

public DrawView(Context context, AttributeSet attrs) {

super(context, attrs);

paint.setAntiAlias(true);

paint.setStrokeWidth(6f);

paint.setColor(Color.BLACK);

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeJoin(Paint.Join.ROUND);

}

@Override

protected void onDraw(Canvas canvas) {

canvas.drawLine(startX, startY, stopX, stopY, paint);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

startX = event.getX();

startY = event.getY();

return true;

case MotionEvent.ACTION_MOVE:

stopX = event.getX();

stopY = event.getY();

break;

case MotionEvent.ACTION_UP:

stopX = event.getX();

stopY = event.getY();

break;

default:

return false;

}

Invalidate();

return true;

}

}

回答1:

You need to store all the lines instead of just the last one.

The following code is completely untested, but hopefully gives you the general idea.

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

import java.util.ArrayList;

class Line {

float startX, startY, stopX, stopY;

public Line(float startX, float startY, float stopX, float stopY) {

this.startX = startX;

this.startY = startY;

this.stopX = stopX;

this.stopY = stopY;

}

public Line(float startX, float startY) { // for convenience

this(startX, startY, startX, startY);

}

}

public class DrawView extends View {

Paint paint = new Paint();

ArrayList lines = new ArrayList();

public DrawView(Context context, AttributeSet attrs) {

super(context, attrs);

paint.setAntiAlias(true);

paint.setStrokeWidth(6f);

paint.setColor(Color.BLACK);

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeJoin(Paint.Join.ROUND);

}

@Override

protected void onDraw(Canvas canvas) {

for (Line l : lines) {

canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY, paint);

}

}

@Override

public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

lines.add(new Line(event.getX(), event.getY()));

return true;

}

else if ((event.getAction() == MotionEvent.ACTION_MOVE ||

event.getAction() == MotionEvent.ACTION_UP) &&

lines.size() > 0) {

Line current = lines.get(lines.size() - 1);

current.stopX = event.getX();

current.stopY = event.getY();

Invalidate();

return true;

}

else {

return false;

}

}

}

来源:https://stackoverflow.com/questions/16748146/android-canvas-drawline

android canvas 教程,Android Canvas drawLine相关推荐

  1. Android 相机教程,Android 相机教程

    Android相机教程 相机主要用于捕获图片和视频.我们可以通过使用相机API的方法来控制相机. Android通过以下两种方式提供了在相机上工作的功能: 通过相机意图 通过相机API 了解相机意图和 ...

  2. Android精通教程-Android入门简介

    前言 大家好,我是 Vic,今天给大家带来Android精通教程-Android入门简介的概述,希望你们喜欢 每日一句 If life were predictable it would cease ...

  3. android打开教程,Android 打开网络上pdf文件

    1.基本思路: 打开网络pdf 思路整体还是来源与图片的加载. android中加载网络图片的框架有很多个.如image-laoder, fresco.glide等,首先都是从内存中找图片,如果内存中 ...

  4. android+notepad教程,Android平台应用开发实例:Notepad

    在这个教程中将会建立一个简单列表界面,允许用户添加删除,但是不能编辑.包括如下的内容: ◆ListActivities的基础知识和如何建立菜单项. ◆如何使用SQLite 数据库访问数据. ◆如何使用 ...

  5. android开发教程,android开发入门教程

    所谓知己知彼才能百战百胜,想学好android就必须先了解 android是什么意思 android环境搭建 Android一词的本义指"机器人",同时也是Google于2007年 ...

  6. sqlite数据库android使用教程,Android开发教程之 SQLite数据库的使用

    在开发Android应用程序时经常需要存储数据,Android系统提供了SQLite数据库,还提供了SQLiteOpenHelper类,使我们可以很方便的操作数据库.通过一个例子来说明在Android ...

  7. android fragment 教程,Android app开发中的Fragment入门学习教程

    在Android3.0上开始引入了一个新概念叫Fragment.它有自己的布局文件,可以作为组件排布,也可以相互组合去实现不同的布局显示.使用Fragment可以重复利用代码,并且可以满足不同设备尺寸 ...

  8. android广播教程,Android学习笔记(广播机制)

    1.Android的广播机制介绍 收听收音机也是一种广播,在收音机中有很多个广播电台,每个广播电台播放的内容都不相同.接受广播时广播(发送方)并不在意我们(接收方)接收到广播时如何处理.好比我们收听交 ...

  9. android混淆教程,Android 实现代码混淆的实例

    Android 实现代码混淆的实例 1.简介 代码混淆(Obfuscated code)亦称花指令,是将计算机程序的代码,转换成一种功能上等价,但是难于阅读和理解的形式的行为. 混淆的目的是为了加大反 ...

  10. android 动画教程,Android 动画Animation

    动画分为视图动画(view animation)和属性动画(property animation),视图动画又分为帧动画和补间动画 视图动画控件(iv)点击事件(OnClickListener接口)触 ...

最新文章

  1. 会话技术CookieSession
  2. Dubbo背景和简介
  3. ubuntu 修改ssh登陆端口
  4. .NET Core 使用 Consul 服务注册发现
  5. 实现深拷贝的几种方法
  6. 洛谷-图的遍历-P2661-信息传递
  7. 创建启动oracle快捷方式,GNOME3创建连接OracleFS管理软件启动快捷方式
  8. linux内核工程师必须知道的三十道题
  9. C++可变长数组vector的使用
  10. 我终于会加载模块了 值得纪念!
  11. python敏感字替换_python实现敏感词过滤的几种方法
  12. 密码学笔记—栅栏密码
  13. ONLYOFFICE历史版本开发技术之二
  14. STM32Cube程序使用 DFU 烧写后Leave DFUMode无法运行程序
  15. OpenGl法向量计算
  16. 计算机显示u盘容量只有1m,为什么新买的U盘容量大小与实际显示大小不一样?...
  17. 使用cobra创建cli命令行工具
  18. Typora测试版过期无法正常使用
  19. html js 在线预览 pdf word xls等
  20. Android P 外置 SD 卡写入权限问题

热门文章

  1. 《UnityAPI.MovieTexture影片纹理》(Yanlz+Unity+SteamVR+云技术+5G+AI+VR云游戏+MovieTexture+audioClip+立钻哥哥++OK++)
  2. [原创]Ladon7.5大型内网渗透扫描器Cobalt Strike
  3. 基于(7,4 ) 线性分组码编码和 BPSK 调制
  4. 2021年认证杯SPSSPRO杯数学建模B题(第一阶段)依巴谷星表中的毕星团求解全过程文档及程序
  5. js 对中文字符的 解码 与 编码
  6. marlab中主成分得分怎么求_PCA(主成分分析) 和 SVD (奇异值分解)
  7. 逻辑回归:详细建模流程与例子代码
  8. 在Xperia XZ上刷入AOSP
  9. 8个正弦波逆变器带你感受生活中无处不在的科技魅力
  10. 基于avr atmega16单片机控制2路9g舵机进行0-180°旋转,1602显示运行状态,adc采样控制舵机转速。然后适合于初学avr单片机的朋友