2013-11-04 90 views
1

所以我正在做一些旋轉和翻譯的ImageView。據我瞭解,imageview並不是真的在新的位置之後,而是在動畫完成後實際移動它。所以我試圖移動的imageview被稱爲令牌,它被設置在其原始位置,並且token.getLocationOnScreen(tokenPos);有它的起始位置。移動或翻譯後,我致電resetToken來處理移動或旋轉。這似乎在一半的時間內完成了預期的結果...不知道我做錯了什麼,但它與我想要的方式不一致。設置Imageview位置和旋轉

public void resetToken() { 
    //Decode Image using Bitmap factory. 
    Bitmap bMap = Bitmap.createBitmap(token.getDrawingCache()); 

    Matrix matrix = new Matrix(); 
    matrix.postRotate(0); 

    //move the token to the spot it needs to be in 
    //after an animation the token is not really 
    //in the new spot 
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(token.getWidth(), token.getHeight()); 
    lp.setMargins(tokenPos[0], tokenPos[1], 0, 0); 

    //Create bitmap with new values. 
    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, token.getWidth(), token.getHeight(), matrix, true); 
    token.setImageBitmap(bMapRotate); 
    token.setLayoutParams(lp); 
    //put rotated image in ImageView. 


    moveCounter = moveCounter + 1; 

} 

回答

1

我能夠這樣做。

public void resetToken() { 

    Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.robo); 
    Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888); 
    Canvas tempCanvas = new Canvas(bmResult); 
    tempCanvas.rotate(currentMapDirection, token.getWidth(), token.getHeight()); 
    tempCanvas.drawBitmap(bmpOriginal, 0, 0, null); 

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(token.getWidth(), token.getHeight()); 
    lp.setMargins(tokenPos[0], tokenPos[1], 0, 0); 

    token.setLayoutParams(lp); 

    token.setImageBitmap(bmResult); 


}