在线上画线(android) [英] Draw Line on Touch (android)

查看:103
本文介绍了在线上画线(android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是在Android手机上画一条跟随我的手指的线......我该怎么做?请帮助

我在Eclipse工作。

What I want to do is draw a line that will follow my finger on android phone...How can i do this? Please help
I work in Eclipse.

推荐答案

在这里查看一些讨论和代码...我认为这有助于你做到这一点你正确地遵循它。

http:/ /stackoverflow.com/questions/4300399/android-drawing-a-line-to-follow-your-finger [ ^ ]

https://groups.google.com/forum/?fromgroups#!topic/android-developers/yBbBaUDH1XI [1-25 ] [ ^ ]

http://stackoverflow.com/questions/ 7701340 / A ndroid-touch-drawing [ ^ ]
See some discussion and code here...i think this helps you to do it if you properly follow it.
http://stackoverflow.com/questions/4300399/android-drawing-a-line-to-follow-your-finger[^]
https://groups.google.com/forum/?fromgroups#!topic/android-developers/yBbBaUDH1XI[1-25][^]
http://stackoverflow.com/questions/7701340/android-touch-drawing[^]


手指触摸画线

Android中有许多应用程序可用于在屏幕上绘制内容。这是一个简单的应用程序,它将在您开始的触摸和触摸的最后一点之间在屏幕上绘制直线。



此应用程序将使用Bitmap,Canvas和Paint类。



位图类包含一些常用技巧用于处理和加载Bitmap对象,使您的用户界面(UI)组件保持响应,并避免超出应用程序内存限制。



Canvas类持有DRAW调用。要绘制一些东西,你需要四个基本组件:一个用于容纳像素的位图,一个用于托管绘图调用的Canvas(写入位图),一个绘图基元(例如Rect,Circle)和一个paint(用于描述颜色和绘图样式。



Paint类保存有关如何绘制几何图形,文本和位图的样式和颜色信息。



1.设计屏幕:



activity_touch_draw.xml

Draw line on finger touch
There are many application in Android you can use to draw something on screen. This is a simple application which will draw straight line on screen between touch you have started and the last point of your touch.

This application will use Bitmap, Canvas, and Paint class.

Bitmap class covers some common techniques for processing and loading Bitmap objects in a way that keeps your user interface (UI) components responsive and avoids exceeding your application memory limit.

Canvas class holds the DRAW calls. To draw something, you need four basic components: A bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Circle), and a paint (to describe the colours and styles for the drawing).

Paint class holds the style and colour information about how to draw geometries, text and bitmaps.

1. Design Screen:

activity_touch_draw.xml
<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" >
 
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />
 
</RelativeLayout>





ImageView将用作绘图板,你的手指将起作用铅笔。从开始触摸到用户拉动手指的终点将绘制一条直线。



TouchDraw.java



An ImageView will be used as drawing board and your finger will work as pencil. A straight line will drawn from starting touch to ending point where user pull up finger.

TouchDraw.java

package app.test;
 
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
 
public class Test extends Activity implements OnTouchListener {
  ImageView imageView;
  Bitmap bitmap;
  Canvas canvas;
  Paint paint;
  float downx = 0, downy = 0, upx = 0, upy = 0;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 
    imageView = (ImageView) this.findViewById(R.id.ImageView);
 
    Display currentDisplay = getWindowManager().getDefaultDisplay();
    float dw = currentDisplay.getWidth();
    float dh = currentDisplay.getHeight();
 
    bitmap = Bitmap.createBitmap((int) dw, (int) dh,
        Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);
    paint = new Paint();
    paint.setColor(Color.GREEN);
    imageView.setImageBitmap(bitmap);
 
    imageView.setOnTouchListener(this);
  }
 
  public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
      downx = event.getX();
      downy = event.getY();
      break;
    case MotionEvent.ACTION_MOVE:
      break;
    case MotionEvent.ACTION_UP:
      upx = event.getX();
      upy = event.getY();
      canvas.drawLine(downx, downy, upx, upy, paint);
      imageView.invalidate();
      break;
    case MotionEvent.ACTION_CANCEL:
      break;<a href=""></a>[<a href="" target="_blank">^</a>]
    default:
      break;
    }
    return true;
  }
}







首先你需要知道什么是屏幕的宽度和高度。您需要使用Display类对象来获取有关屏幕的详细信息。显示类提供有关显示大小和密度的信息。






First of all you'll need to know what is width and height of your screen. You'll need Display class object to get detail about screen. Display class provides information about the display size and density.

Display currentDisplay = getWindowManager().getDefaultDisplay();
float dw = currentDisplay.getWidth();
float dh = currentDisplay.getHeight();





currentDisplay对象将提供者显示宽度和高度。

位图对象将提供宽度和高度。

canvas对象将用于类绘制函数。你的实际线条将在画布上绘制。

画布对象实际上是画布上的一条线。



最后,用户将在屏幕上画线通过触摸ImageView对象。所以,我在ImageView对象上设置了OnTouchListener。



OnTouchListener是一个接口定义,用于在将触摸事件调度到视图时调用回调。



将触摸事件调度到视图时调用onTouch方法。这允许听众有机会在目标视图之前做出响应。

public boolean onTouch(View v,MotionEvent event)



v是对象从调度触摸的View类。

事件是MotionEvent的对象,包含有关事件的完整信息







currentDisplay object will provider display width and height.
bitmap object will of provided width and height.
canvas object will used to class draw functions. Your actual line will draw on canvas.
paint object will actually a line on the canvas.

Finally, user will draw on screen line by touching ImageView object. So, I have set OnTouchListener on ImageView object.

OnTouchListener is an interface definition for a callback to be invoked when a touch event is dispatched to the view.

onTouch method is called when a touch event is dispatched to a view. This allows a listeners to get chance to respond before the target view.
public boolean onTouch(View v, MotionEvent event)

v is object of View class from where touch is dispatched.
event is object of MotionEvent containing full information about event


int action = event.getAction();





getAction()返回正在执行的动作类型。在触摸线条的整个过程中,许多动作都会被称为。



ACTION_DOWN

ACTION_MOVE

ACTION_CANCEL

ACTION_UP



所以,我需要从一个名为ACTION_DOWN的动作开始绘制一条直到行动ACTION_UP的行。我已经开始了X& Y坐标,其中ACTION_DOWN操作被调用,并且我已经检索到结束X& Y调用ACTION_UP动作时进行协调。最后,我使用Canvas类的drawLine()方法绘制直线。



getAction() return the kind of action being performed. During entire process of drawing a line on touch many actions will be called like.

ACTION_DOWN
ACTION_MOVE
ACTION_CANCEL
ACTION_UP

So, I need to draw a line starting from action called ACTION_DOWN up to the action ACTION_UP. I have taken starting X & Y co-ordinate where ACTION_DOWN action called and again I have retrieved ending X & Y co-ordinate when ACTION_UP action called. Finally, I have used drawLine() method of Canvas class to draw straight line.


这篇关于在线上画线(android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆