问题描述
我创建使用下面的类(简单的2D图形的机器人)和徘徊,实现了位图可以独立移动我的视图中的两个位图。我呼吁motionevent方法这一点。
目前的问题,我不明白为什么只有一个对象是正确朝着以下code。例如与此code,只有而不是位图被移动,我想独立移动海誓山盟的两个位图。
scenrio:我可以用我的两个fingures,每个对象,独立移动的位图。但我不知道如何来实现这一目标。
公共类的TouchView扩展视图{
私人绘制对象交叉;
私人矩形crossBounds = NULL;
私人绘制对象不是;
私人矩形notBounds = NULL;
私人诠释X1,Y1,X2,Y2;
布尔标志= TRUE;
私人无效intialize()
{
INT W1 = cross.getIntrinsicWidth();
INT H1 = cross.getIntrinsicHeight();
X1 = 100;
Y1 = 100;
crossBounds =新的Rect(X1-W1 / 2,Y1-W1 / 2,X1 + W1 / 2,Y1 + H1 / 2);
INT W = not.getIntrinsicWidth();
INT H = not.getIntrinsicHeight();
X2 = 300;
Y2 = 300;
notBounds =新的Rect(×2-W / 2,Y 2瓦特/ 2,2 + W / 2,Y2 + H / 2);
}
公众的TouchView(上下文的背景下){
超(上下文);
//获取图像的再presentation
资源资源= context.getResources();
跨=(抽出式)resources.getDrawable(R.drawable.cross);
不=(抽出式)resources.getDrawable(R.drawable.not);
}
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
Log.i(TouchView.onTouchEvent,事件=+事件);
如果(event.getAction()== MotionEvent.ACTION_DOWN ||
event.getAction()== MotionEvent.ACTION_MOVE){
INT touchCounter = event.getPointerCount();
如果(touchCounter == 2&安培;&安培;的getHeight()== Y1){
INT W = cross.getIntrinsicWidth();
INT H = cross.getIntrinsicHeight();
X1 =(int)的event.getX();
crossBounds =新的Rect(X1-瓦特/ 2,Y1-瓦特/ 2,X1 + W / 2,Y1 + H / 2);
}
其他
{
INT W1 = not.getIntrinsicWidth();
INT H1 = not.getIntrinsicHeight();
X2 =(INT)event.getX();
notBounds =新的Rect(×2-W1 / 2,Y2-W1 / 2,2 + W1 / 2,Y2 + H1 / 2);
}
//请求系统重新绘制视图(调用的OnDraw在
//在将来的某个时候)
//从非UI线程调用postInvalidate代替
无效();
返回true;
}
返回false;
}
@覆盖
保护无效的OnDraw(帆布油画){
Log.i(TouchView.onDraw,);
// 背景
油漆bgPaint =新的油漆();
bgPaint.setColor(Color.WHITE);
canvas.drawPaint(bgPaint);
如果(旗==真){
intialize();
cross.setBounds(crossBounds);
cross.draw(画布);
not.setBounds(notBounds);
not.draw(画布);
标志= FALSE;
}
如果(crossBounds!= NULL){
cross.setBounds(crossBounds);
cross.draw(画布);
not.setBounds(notBounds);
not.draw(画布);
}
}
}
公共类SimpleDrag扩展视图{
私人最终诠释INVALID_INDEX = -1;
私人最终诠释mTotalItems = 5;
私人的ArrayList<矩形> mItemsCollection;
私人的ArrayList<点> mActiveDragPoints;
私人的ArrayList<矩形> mActiveRects;
私人油漆mPaint;
/ **
* @参数方面
*类型@返回SimpleDrag
*构造函数
* @Since 2013年2月19号
* @author rajeshcp
* /
公共SimpleDrag(上下文的背景下){
超(上下文);
在里面();
}
/ **
* @参数方面
* @参数ATTRS
*类型@返回SimpleDrag
*构造函数
* @Since 2013年2月19号
* @author rajeshcp
* /
公共SimpleDrag(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
在里面();
}
/ **
* @参数方面
* @参数ATTRS
* @参数defStyle
*类型@返回SimpleDrag
*构造函数
* @Since 2013年2月19号
* @author rajeshcp
* /
公共SimpleDrag(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
超(背景下,ATTRS,defStyle);
在里面();
}
/ *(非Javadoc中)
* @see android.view.View#的OnDraw(android.graphics.Canvas)
* @Since 2013年2月19号
* @author rajeshcp
* /
@覆盖
保护无效的OnDraw(帆布油画){
super.onDraw(画布);
canvas.drawColor(Color.BLUE,PorterDuff.Mode.CLEAR);
对于(矩形RECT:mItemsCollection)
{
canvas.drawRect(矩形,mPaint);
}
}
/ **
*类型为null @参数
*类型为null @返回
*功能,将初始化视图
* @Since二○一三年二月二十〇日
* @author rajeshcp
* /
私人无效的init()
{
mActiveRects =新的ArrayList<矩形>(mTotalItems);
mActiveDragPoints =新的ArrayList<点>(mTotalItems);
mItemsCollection =新的ArrayList<矩形>();
的for(int i = 0; I< mTotalItems;我++)
{
矩形矩形=新的Rect(I * 100,I * 100,第(i + 1)* 100,第(i + 1)* 100);
mItemsCollection.add(RECT);
}
mPaint =新的油漆(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.RED);
}
/ *(非Javadoc中)
* @see android.view.View#的onTouchEvent(android.view.MotionEvent)
* @Since 2013年2月19号
* @author rajeshcp
* /
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
最终诠释行动= event.getActionMasked();
最终诠释指针= event.getActionIndex();
开关(动作){
案例MotionEvent.ACTION_DOWN:
点触地得分=新的点((INT)event.getX(),(INT)event.getY());
lookForIntersection(着陆);
打破;
案例MotionEvent.ACTION_UP:
案例MotionEvent.ACTION_CANCEL:
mActiveDragPoints.removeAll(mActiveDragPoints);
mActiveRects.removeAll(mActiveRects);
打破;
案例MotionEvent.ACTION_MOVE:
诠释计数= 0;
对于(矩形RECT:mActiveRects)
{
点curretPoint =新的点((INT)event.getX(计数),(INT)event.getY(计数));
moveRect(curretPoint,mActiveDragPoints.get(计数),RECT);
算上++;
}
Log.d(的getClass()的getName(),活性Rects+ mActiveRects.size());
Log.d(的getClass()的getName(),活性点+ mActiveDragPoints.size());
无效();
打破;
案例MotionEvent.ACTION_POINTER_DOWN:
触地得分=新的点((INT)event.getX(指针),(INT)event.getY(指针));
lookForIntersection(着陆);
。//Log.d(getClass()的getName(),ACTION_POINTER_DOWN+指针);
打破;
案例MotionEvent.ACTION_POINTER_UP:
INT指数= getIntersectionRectIndex(新点((int)的event.getX(指针),(int)的event.getY(指针)));
如果(指数!= INVALID_INDEX)
{
矩形RECT = mItemsCollection.get(指数);
mActiveDragPoints.remove(mActiveRects.indexOf(矩形));
mActiveRects.remove(RECT);
}
打破;
默认:
打破;
}
返回true;
}
/ **
* @参数类型的点触地
*类型为null @返回
*功能,会发现
*相交矩形,并加入到
*活动收集
* @Since二○一三年二月二十〇日
* @author rajeshcp
* /
私人无效lookForIntersection(着陆点)
{
最终诠释指数= getIntersectionRectIndex(着陆);
如果(指数!= INVALID_INDEX)
{
最后的矩形RECT = mItemsCollection.get(指数);
如果(mActiveRects.indexOf(矩形)== INVALID_INDEX)
{
mActiveDragPoints.add(着陆);
mActiveRects.add(mItemsCollection.get(指数));
}
}
Log.d(的getClass()的getName(),活性Rects+ mActiveRects.size());
Log.d(的getClass()的getName(),活性点+ mActiveDragPoints.size());
}
/ **
* @参数类型为Point点
* int类型@返回
*函数返回的索引
*在RECT contaning给定点
* @Since二○一三年二月二十〇日
* @author rajeshcp
* /
私人诠释getIntersectionRectIndex(最终点对点)
{
INT指数= INVALID_INDEX;
对于(矩形RECT:mItemsCollection)
{
如果(rect.contains(point.x,point.y))
{
指数= mItemsCollection.indexOf(RECT);
打破;
}
}
返回指数;
}
/ **
* @参数类型点currentPoint
Point类型的*参数prevPoint
* @参数类型矩形的RECT
*类型为null @返回
*功能,将移动变化
德矩形的*界
* @Since二○一三年二月二十〇日
* @author rajeshcp
* /
私人无效moveRect(点currentPoint,点prevPoint,最后的矩形RECT)
{
INT xMoved = currentPoint.x - prevPoint.x;
INT yMoved = currentPoint.y - prevPoint.y;
rect.set(rect.left + xMoved,rect.top + yMoved,rect.right + xMoved,rect.bottom + yMoved);
mActiveDragPoints.set(mActiveDragPoints.indexOf(prevPoint),currentPoint);
}
}
希望这是你想要的,没有测试很多,但基本上这是为我工作,甚至你可以增加NOF项目通过改变mTotalItems。希望这会有所帮助。
I created two bitmap on my view using the following class (Simple 2D Graphics in android) and wandering to achieve that bitmap can move independently. I am calling motionevent method for this.
Current issue, i do not understand why does only one object is moving right in the following code. e.g. with this code, only "not" bitmap is moved, i would like to move both bitmaps independently of eachother.
scenrio: i can use my two fingures, one for each object, to move the bitmaps independently. but i don't know how to achieve this.
public class TouchView extends View {
private Drawable cross;
private Rect crossBounds = null;
private Drawable not;
private Rect notBounds = null;
private int x1, y1, x2, y2 ;
boolean flag = true;
private void intialize ()
{
int w1 = cross.getIntrinsicWidth();
int h1 = cross.getIntrinsicHeight();
x1 = 100;
y1 = 100;
crossBounds = new Rect(x1-w1/2, y1-w1/2, x1+w1/2, y1+h1/2);
int w = not.getIntrinsicWidth();
int h = not.getIntrinsicHeight();
x2 = 300;
y2 = 300;
notBounds = new Rect(x2-w/2, y2-w/2, x2+w/2, y2+h/2);
}
public TouchView(Context context) {
super(context);
// Get a representation of the image
Resources resources = context.getResources();
cross = (Drawable) resources.getDrawable(R.drawable.cross);
not = (Drawable) resources.getDrawable(R.drawable.not);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("TouchView.onTouchEvent", "event = " + event);
if(event.getAction() == MotionEvent.ACTION_DOWN ||
event.getAction() == MotionEvent.ACTION_MOVE) {
int touchCounter = event.getPointerCount();
if (touchCounter ==2 && getHeight ()==y1){
int w = cross.getIntrinsicWidth();
int h = cross.getIntrinsicHeight();
x1 = (int) event.getX();
crossBounds = new Rect(x1-w/2, y1-w/2, x1+w/2, y1+h/2);
}
else
{
int w1 = not.getIntrinsicWidth();
int h1 = not.getIntrinsicHeight();
x2 = (int) event.getX();
notBounds = new Rect(x2-w1/2, y2-w1/2, x2+w1/2, y2+h1/2);
}
// Request the system to redraw the view (call onDraw at
// some point in the future)
// From a non-UI thread, call postInvalidate instead
invalidate();
return true;
}
return false;
}
@Override
protected void onDraw(Canvas canvas) {
Log.i("TouchView.onDraw", "");
// Background
Paint bgPaint = new Paint();
bgPaint.setColor(Color.WHITE);
canvas.drawPaint(bgPaint);
if (flag == true){
intialize ();
cross.setBounds(crossBounds);
cross.draw(canvas);
not.setBounds(notBounds);
not.draw(canvas);
flag=false;
}
if(crossBounds != null) {
cross.setBounds(crossBounds);
cross.draw(canvas);
not.setBounds(notBounds);
not.draw(canvas);
}
}
}
public class SimpleDrag extends View {
private final int INVALID_INDEX = -1;
private final int mTotalItems = 5;
private ArrayList<Rect> mItemsCollection;
private ArrayList<Point> mActiveDragPoints;
private ArrayList<Rect> mActiveRects;
private Paint mPaint;
/**
* @param context
* @return of type SimpleDrag
* Constructor function
* @since Feb 19, 2013
* @author rajeshcp
*/
public SimpleDrag(Context context) {
super(context);
init();
}
/**
* @param context
* @param attrs
* @return of type SimpleDrag
* Constructor function
* @since Feb 19, 2013
* @author rajeshcp
*/
public SimpleDrag(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
/**
* @param context
* @param attrs
* @param defStyle
* @return of type SimpleDrag
* Constructor function
* @since Feb 19, 2013
* @author rajeshcp
*/
public SimpleDrag(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
/* (non-Javadoc)
* @see android.view.View#onDraw(android.graphics.Canvas)
* @since Feb 19, 2013
* @author rajeshcp
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLUE, PorterDuff.Mode.CLEAR);
for( Rect rect : mItemsCollection)
{
canvas.drawRect(rect, mPaint);
}
}
/**
* @param of type null
* @return of type null
* function which will initialize the view
* @since Feb 20, 2013
* @author rajeshcp
*/
private void init()
{
mActiveRects = new ArrayList<Rect>(mTotalItems);
mActiveDragPoints = new ArrayList<Point>(mTotalItems);
mItemsCollection = new ArrayList<Rect>();
for( int i = 0; i < mTotalItems; i++)
{
Rect rect = new Rect(i * 100, i * 100, (i + 1) * 100, (i + 1) * 100);
mItemsCollection.add(rect);
}
mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.RED);
}
/* (non-Javadoc)
* @see android.view.View#onTouchEvent(android.view.MotionEvent)
* @since Feb 19, 2013
* @author rajeshcp
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getActionMasked();
final int pointer = event.getActionIndex();
switch (action) {
case MotionEvent.ACTION_DOWN :
Point touchDown = new Point((int)event.getX(), (int)event.getY());
lookForIntersection(touchDown);
break;
case MotionEvent.ACTION_UP :
case MotionEvent.ACTION_CANCEL :
mActiveDragPoints.removeAll(mActiveDragPoints);
mActiveRects.removeAll(mActiveRects);
break;
case MotionEvent.ACTION_MOVE :
int count = 0;
for(Rect rect : mActiveRects)
{
Point curretPoint = new Point((int)event.getX(count), (int)event.getY(count));
moveRect(curretPoint, mActiveDragPoints.get(count), rect);
count++;
}
Log.d(getClass().getName(), "Active Rects" + mActiveRects.size());
Log.d(getClass().getName(), "Active Points" + mActiveDragPoints.size());
invalidate();
break;
case MotionEvent.ACTION_POINTER_DOWN :
touchDown = new Point((int)event.getX(pointer), (int)event.getY(pointer));
lookForIntersection(touchDown);
//Log.d(getClass().getName(), "ACTION_POINTER_DOWN" + pointer);
break;
case MotionEvent.ACTION_POINTER_UP :
int index = getIntersectionRectIndex(new Point((int)event.getX(pointer), (int)event.getY(pointer)));
if( index != INVALID_INDEX )
{
Rect rect = mItemsCollection.get(index);
mActiveDragPoints.remove(mActiveRects.indexOf(rect));
mActiveRects.remove(rect);
}
break;
default:
break;
}
return true;
}
/**
* @param touchDown of type Point
* @return of type null
* function which will find the
* intersecting rect and add to the
* active collection
* @since Feb 20, 2013
* @author rajeshcp
*/
private void lookForIntersection(Point touchDown)
{
final int index = getIntersectionRectIndex(touchDown);
if( index != INVALID_INDEX )
{
final Rect rect = mItemsCollection.get(index);
if( mActiveRects.indexOf(rect) == INVALID_INDEX )
{
mActiveDragPoints.add(touchDown);
mActiveRects.add(mItemsCollection.get(index));
}
}
Log.d(getClass().getName(), "Active Rects" + mActiveRects.size());
Log.d(getClass().getName(), "Active Points" + mActiveDragPoints.size());
}
/**
* @param point of type Point
* @return of type int
* function which will return the index of
* the rect contaning the given point
* @since Feb 20, 2013
* @author rajeshcp
*/
private int getIntersectionRectIndex(final Point point)
{
int index = INVALID_INDEX;
for(Rect rect : mItemsCollection)
{
if( rect.contains(point.x, point.y) )
{
index = mItemsCollection.indexOf(rect);
break;
}
}
return index;
}
/**
* @param currentPoint of type Point
* @param prevPoint of type Point
* @param rect of type Rect
* @return of type null
* function which will move the change the
* bounds of teh rect
* @since Feb 20, 2013
* @author rajeshcp
*/
private void moveRect(Point currentPoint, Point prevPoint, final Rect rect)
{
int xMoved = currentPoint.x - prevPoint.x;
int yMoved = currentPoint.y - prevPoint.y;
rect.set(rect.left + xMoved, rect.top + yMoved, rect.right + xMoved, rect.bottom + yMoved);
mActiveDragPoints.set(mActiveDragPoints.indexOf(prevPoint), currentPoint);
}
}
Hope this is what you want, haven't tested a lot, but basically this is working for me even you can increase the nof items by changing the mTotalItems. Hope this will help.
这篇关于同时双位图上MotionEvent期运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!