2011-03-25 88 views
3

我遇到了畫布功能的問題,使用畫布繪製1位圖時沒有問題,但只要添加第二個位圖,它只繪製最後一個位圖,不顯示都。 請幫忙。這是我的代碼。 我的意圖是,然後在屏幕上獨立地動畫這兩個不同的位圖。如何在畫布上繪製多個位圖

@Override 
     protected void onDraw(Canvas canvas) { 
      football = BitmapFactory.decodeResource(getResources(), 
        R.drawable.ballicon); 

     receiver = BitmapFactory.decodeResource(getResources(), 
       R.drawable.rec); 


     canvas.drawBitmap(football, translate, null); 
     canvas.drawBitmap(receiver, translate, null); 


     Matrix m = canvas.getMatrix(); 
     Log.d(DEBUG_TAG, "Matrix: " + translate.toShortString()); 
     Log.d(DEBUG_TAG, "Canvas: " + m.toShortString()); 
    } 

感謝

+1

,如果你在不同的位置作爲測試得出的位圖,會發生什麼畫canvasview? canvas.drawBitmap(football,0,0,null); canvas.drawBitmap(receiver,100,100,null); – slund 2011-03-25 03:32:45

+0

我同意你的觀點,你在代碼的哪個位置繪製它們?他們都使用相同的'翻譯'矩陣。通過使用不同的Matricies在不同的位置繪製它們,並且如果要在同一位置繪製它們,請在Paint對象中給出一個較低的Alpha值,以便它可以讓另一個「流血」穿過它。 – 2011-03-25 05:17:16

+0

好吧我明白你的意思了,我會試試看。謝謝! – huskyd97 2011-03-25 14:52:22

回答

1

這裏是與多個位圖

public class AdvanceCanvasView extends View { 
private Bitmap bitmap; 
private Canvas bitmapCanvas; 
private Bitmap mBitmapBrush; 
private ArrayList<Bitmap> bitmapArrayList; 
private Vector2 mBitmapBrushDimensions; 
private Paint paintLine; 
private List<Vector2> mPositions = new ArrayList<Vector2>(100); 
private HashMap<Integer, Path> pathMap; // current Paths being drawn 
private HashMap<Integer, Point> previousPointMap; // current Points 
private int i = 0; 

private static final class Vector2 { 
    public Vector2(float x, float y) { 
     this.x = x; 
     this.y = y; 
    } 

    public final float x; 
    public final float y; 
} 

@SuppressLint("UseSparseArrays") 
public AdvanceCanvasView(Context context, AttributeSet attrs) { 
    super(context, attrs); // pass context to View's constructor 
} 

public AdvanceCanvasView(Context c) { 
    super(c); 
    pathMap = new HashMap<>(); 
    previousPointMap = new HashMap<>(); 
    bitmapArrayList = new ArrayList<>(); 
    paintLine = new Paint(); 
} 

@Override 
public void onSizeChanged(int w, int h, int oldW, int oldH) { 
    bitmap = Bitmap.createBitmap(getWidth(), getHeight(), 
      Bitmap.Config.ARGB_8888); 
    bitmapCanvas = new Canvas(bitmap); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawBitmap(bitmap, 0, 0, null); 
    for (int i = 0; i < mPositions.size(); i++) { 
     canvas.drawBitmap(bitmapArrayList.get(i), mPositions.get(i).x, mPositions.get(i).y, null); 
    } 

} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    int action = event.getActionMasked(); 
    int actionIndex = event.getActionIndex(); 

    if (action == MotionEvent.ACTION_DOWN 
      || action == MotionEvent.ACTION_POINTER_DOWN) { 
     touchStarted(event.getX(actionIndex), event.getY(actionIndex), 
       event.getPointerId(actionIndex)); 
    } 
    else if (action == MotionEvent.ACTION_UP 
      || action == MotionEvent.ACTION_POINTER_UP) { 
     touchEnded(event.getPointerId(actionIndex)); 
    } 
    else { 
     touchMoved(event); 
    } 

    invalidate(); 

    return true; 
} 

private void touchStarted(float x, float y, int lineID) { 
    Path path; 
    Point point; 
    path = new Path(); // create a new Path 
    pathMap.put(lineID, path); // add the Path to Map 
    point = new Point(); // create a new Point 
    previousPointMap.put(lineID, point); // add the Point to the Map 
    path = new Path(); // create a new Path 
    point = new Point(); // create a new Point 
    path.moveTo(x, y); 
    point.x = (int) x; 
    point.y = (int) y; 

} // end method touchStarted 

private void touchMoved(MotionEvent event) { 
    // for each of the pointers in the given MotionEvent 
    for (int i = 0; i < event.getPointerCount(); i++) { 
     final float posX = event.getX(); 
     final float posY = event.getY(); 
     mPositions.add(new Vector2(posX - mBitmapBrushDimensions.x/2, posY - mBitmapBrushDimensions.y/2)); 
     bitmapArrayList.add(mBitmapBrush); 
    } 
    invalidate(); 

} 

private void touchEnded(int lineID) { 
    Path path = pathMap.get(lineID); 
    path.reset(); 
} 

public void init(Bitmap bitmap) { 
    mBitmapBrush = bitmap; 
    BitmapShader shader = new BitmapShader(mBitmapBrush, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR); 
    paintLine.setShader(shader); 
    mBitmapBrushDimensions = new Vector2(mBitmapBrush.getWidth(), mBitmapBrush.getHeight()); 
} 

}