Ho un FrameLayout che contiene un SurfaceView sottoclasse e un ImageView. Voglio fare un TranslateAnimation sul ImageView. L'unico modo per farlo funzionare è aggiungere una vista vuota al FrameLayout. In caso contrario, ImageView viene ritagliato (ai limiti della posizione di ImageView all'inizio dell'animazione) durante l'animazione.Perché le animazioni della vista a volte vengono troncate?
Sono curioso di sapere perché la visualizzazione di fratello vuoto consente a ImageView di animare correttamente. La linea che lo fa funzionare è contrassegnata da un commento nel codice qui sotto.
public class Test5 extends Activity {
private static final String TAG = "Test5";
private MySurfaceView mMSV;
private ImageView mRectView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMSV = new MySurfaceView(this);
mRectView = new ImageView(this);
ShapeDrawable sd = new ShapeDrawable(new RectShape());
sd.getPaint().setColor(Color.RED);
sd.setIntrinsicWidth(300);
sd.setIntrinsicHeight(100);
mRectView.setImageDrawable(sd);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(mMSV);
frameLayout.addView(mRectView, new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT));
// I don't know why the following line prevents clipping during
// animation. It simply adds a vacuous View.
frameLayout.addView(new View(this));
setContentView(frameLayout);
} // onCreate
public class MySurfaceView extends SurfaceView implements
SurfaceHolder.Callback {
public MySurfaceView(Context context) {
super(context);
getHolder().addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas can = mMSV.getHolder().lockCanvas();
can.drawColor(Color.GRAY);
mMSV.getHolder().unlockCanvasAndPost(can);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_UP) {
Log.v(TAG, String.format("ACTION_UP, w=%d, h=%d", mRectView
.getWidth(), mRectView.getHeight()));
Animation an = new TranslateAnimation(0f, 200f, 0f, 200f);
// Animation an = new RotateAnimation(0f, 90f);
an.setStartOffset(200);
an.setDuration(1000);
mRectView.startAnimation(an);
}
return true;
}
} // MySurfaceView
} // Test5
Aggiunta di LayoutParam per riempire il genitore nella chiamata 'setContentView' non cambia nulla. – Bill
BTW, cambiando SurfaceView in una vista il problema si risolverà. – Bill
È possibile disegnare forme disegnabili su Canvas o Superficie. Nella tua applicazione la stai aggiungendo come vista su SurfaceView invece di disegnarla, quindi non stai veramente utilizzando il tuo SurfaceView, ma semplicemente utilizzandolo per rilevare il tocco e avviare l'animazione. Controlla su Android developer come stanno usando shapeDrawable: http://developer.android.com/guide/topics/graphics/2d-graphics.html#shape-drawable – Lumis