android setContentView中的2个不同视图

时间:2010-07-06 12:00:05

标签: android animation imageview

我需要在一个视图中有2个不同的图像。 GraphicsView(this) 应该是一个动画(实际上是一个在视图中心自动旋转的图像)和 R.layout .main 应该是视图的背景(视图顶部的静态图像)

我需要的是视图顶部的图像视图,以及我所拥有的动画视图

    public class spinball extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(new GraphicsView(this));
        setContentView(R.layout.main);
}
    private static class GraphicsView extends View {
        private Bitmap ball;
            private int XOffset;
            private int YOffset;

        private Animation anim;

        public GraphicsView(Context context) {
            super(context);
            ball = BitmapFactory
                    .decodeResource(getResources(), R.drawable.ball);
            XOffset = ball.getWidth() / 2;
            YOffset = ball.getHeight() / 2;
        }
        private void createAnim(Canvas canvas) {
            anim = new RotateAnimation(0, 360, canvas.getWidth() / 2, canvas
                    .getHeight() / 2);
            anim.setRepeatMode(Animation.RESTART);
            anim.setRepeatCount(Animation.INFINITE);
            anim.setDuration(10000L);
            anim.setInterpolator(new AccelerateDecelerateInterpolator());

            startAnimation(anim);
        }
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            // creates the animation the first time
            if (anim == null) {
                createAnim(canvas);
            }

            Path circle = new Path();

            int centerX = canvas.getWidth() / 2;
            int centerY = canvas.getHeight() / 2;
            int r = Math.min(centerX, centerY);

            circle.addCircle(centerX, centerY, r, Direction.CW);
            Paint paint = new Paint();
            canvas.drawBitmap(ball, centerX - jobsXOffset,
                    centerY - jobsYOffset, null);
        }
    }
}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <ImageView android:src="@drawable/static_image"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:background="#FFFFFF"
       />
</LinearLayout>

2 个答案:

答案 0 :(得分:4)

使用RelativeLayout。后来RelativeLayout堆叠的孩子“在”(Z轴)早期孩子的上面。

答案 1 :(得分:0)

活动只能有一个内容视图。如果您只想拥有背景图像,可以设置顶级布局的背景可绘制,并使该布局包含ImageView。这就是你想要的吗?