android - android setContentView 中的 2 个不同 View

标签 android animation imageview

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

我需要的是在 View 顶部和动画 View 下方的 ImageView

    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);
        }
    }
}

主.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>

最佳答案

使用RelativeLayoutRelativeLayout 的较晚子级堆叠在较早的子级“之上”(Z 轴)。

关于android - android setContentView 中的 2 个不同 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3185967/

相关文章:

android - 我们如何在android中为imageview设置高度和宽度dp?

java - 如何将 way2sms 集成到我的 Android 应用程序

Android 11 无障碍服务权限

android - 当我的 AAR 已经包含依赖项时,向我的项目添加依赖项

javascript - React Native 中的循环运动动画

Android:将 <img> 标签替换为 ImageViews

android - Android 中的 MaterialDialog autoDismiss() 与 canceledOnTouchOutside()

Python Matplotlib FuncAnimation.save() 只保存 100 帧

Android-setDuration 不起作用?对象动画师

Android通过intentservice下载图片OK但无法在ImageVIew中设置