如何在Android中单个画布上移动多个位图

4

我希望在同一个画布上移动多个位图。使用下面的代码,当触摸屏幕时,我可以移动一个位图,但是我无法识别位图上的触摸事件,因此我无法移动特定的位图。

public class DrawTopologyView extends View {
Paint paint = new Paint();
Bitmap zed_bitMap, lamp_on_bitmap, fan_on_bitmap, ac_on_bitmap;

int START_X = 5;
int START_Y = 5;

float x = 500-24,y=START_Y;

public DrawTopologyView(Context context) {
    super(context);
    zed_bitMap = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.zed);
    lamp_on_bitmap = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.lamp_on);
    fan_on_bitmap = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.fan_on);
    ac_on_bitmap = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.ac_on);

}

@Override
public void onDraw(Canvas canvas) {

    int CENTER_X = canvas.getWidth() / 2 - 24;
    int CENTER_Y = canvas.getHeight() / 2 - zed_bitMap.getHeight();

    int FULL_WIDTH = canvas.getWidth();
    int FULL_HEIGHT = canvas.getHeight();

    canvas.drawBitmap(zed_bitMap, CENTER_X, CENTER_Y, paint);
    paint.setStrokeWidth(3);
    paint.setPathEffect(new DashPathEffect(new float[] { 5, 5, 5, 5 }, 0));

    canvas.drawBitmap(ac_on_bitmap, START_X, START_Y, paint);
    canvas.drawLine(START_X + 48, START_Y + 48, CENTER_X + 10,
            CENTER_Y + 10, paint);

    canvas.drawLine(250, START_Y + 48, CENTER_X + 10, CENTER_Y + 10, paint);
    canvas.drawBitmap(lamp_on_bitmap, 250 - 24, START_Y, paint);

    canvas.drawLine(500, START_Y + 48, CENTER_X + 10, CENTER_Y + 10, paint);
    canvas.drawBitmap(fan_on_bitmap, x,y, paint);


    // canvas.drawLine(FULL_WIDTH-40,FULL_HEIGHT-120,CENTER_X+30,CENTER_Y+30,
    // paint);
    // canvas.drawBitmap(light_off_bitmap, FULL_WIDTH-60-24,FULL_HEIGHT-130,
    // paint);

}

public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {

    }
        break;

    case MotionEvent.ACTION_MOVE: {
        x = (int) event.getX();
        y = (int) event.getY();

        invalidate();
    }

        break;
    case MotionEvent.ACTION_UP:

        x = (int) event.getX();
        y = (int) event.getY();
        Log.d(APPConstant.LOG_TAG, ".................." + x + "......" + y);
        Toast.makeText(getContext(), ".................." + x + "......" + y,Toast.LENGTH_SHORT).show();
        invalidate();
        break;
    }
    return true;
}



}
1个回答

2
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);
}

}

这样可以一次选择多个位图文件。在本例中,我仅使用矩形,您可以将这些矩形填充为位图。


首先感谢上帝。这个程序运行成功了,你的代码没有问题。非常感谢你,Rajesh CP。最后也要感谢stackoverflow。 - Ahesanali Suthar
非常感谢。这对我来说真是一件痛苦的事情@Rajesh - Vikrant_Dev
@Rajesh CP:实际上我必须使用位图,所以我把位图放在矩形内部,但问题是矩形的背景红色出现在位图后面。我们能解决这个问题吗? - Ahesanali Suthar
@Triode 看起来太长了,而且我已经离开了这个项目,所以现在无法对此发表任何意见。 - Ahesanali Suthar
@Triode,是否可以使用这个逻辑来解决这个问题:https://stackoverflow.com/questions/52429340/select-multiple-bitmaps-using-ontouchevent - Chetan Mehra
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接