2011-09-06 17 views
1

次のクラスを使用して回転可能なダイアログを作成しても問題ありません。アニメーションのあるダイアログを回転させる際の問題

import android.content.Context; 

import android.graphics.Canvas; 
import android.graphics.Matrix; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.widget.LinearLayout; 

public class RotateLinearLayout extends LinearLayout { 

private Matrix mForward = new Matrix(); 
private Matrix mReverse = new Matrix(); 
private float[] mTemp = new float[2]; 
private float degree = 0; 

public RotateLinearLayout(Context context) { 
    super(context); 
} 

public RotateLinearLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
protected void dispatchDraw(Canvas canvas) { 
    if (degree == 0) { 
     super.dispatchDraw(canvas); 
     return; 
    } 
    canvas.rotate(degree, getWidth()/2, getHeight()/2); 
    mForward = canvas.getMatrix(); 
    mForward.invert(mReverse); 
    canvas.save(); 
    canvas.setMatrix(mForward); // This is the matrix we need to use for 
           // proper positioning of touch events 
    super.dispatchDraw(canvas); 
    canvas.restore(); 
} 

@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 
    if (degree == 0) { 
     return super.dispatchTouchEvent(event); 
    } 
    final float[] temp = mTemp; 
    temp[0] = event.getX(); 
    temp[1] = event.getY(); 

    mReverse.mapPoints(temp); 

    event.setLocation(temp[0], temp[1]); 
    return super.dispatchTouchEvent(event); 
} 

public void rotate() { 
    if (degree == 0) { 
     degree = 180; 
    } else { 
     degree = 0; 
    } 
} 
} 

このダイアログの左側には、アニメーションが搭載されています。ダイアログが回転しない場合、ImageViewは正しくアニメーション化されます。ダイアログを回転させると、ImageViewはダイアログの右側でアニメートする必要がありますが、以前に配置されたImageViewの画面ピクセルは醜い状態に変わります。回転後、ImageViewの位置は正しく変更されますが、アニメーションの位置は変更されません。

ImageViewのアニメーションの新しい位置を設定するにはどうすればよいですか?

答えて

0

私はちょうどラインinvalidate();を追加し、すべてが修正されました:

@Override 
protected void dispatchDraw(Canvas canvas) { 
    if (degree == 0) { 
     super.dispatchDraw(canvas); 
     return; 
    } 
    canvas.rotate(degree, getWidth()/2, getHeight()/2); 
    mForward = canvas.getMatrix(); 
    mForward.invert(mReverse); 
    canvas.save(); 
    canvas.setMatrix(mForward); // This is the matrix we need to use for 
           // proper positioning of touch events 
    super.dispatchDraw(canvas); 
    canvas.restore(); 
    // is needed 
    invalidate(); 
    } 
関連する問題