1

我一直在试图弄清楚如何在顶部有 2 个图层的背景布局。当一个 imageView(人)被点击时,我希望它成为船的一部分,绘制在船的顶部。这样,当船 imageView 被点击时,人会随着船过河。我让船在被点击时来回移动,但是想要在点击时将 person imageView 设置为像船 imageView 的孩子。现在我正在努力让这个人成为船 imageView 的孩子,但我一直在尝试的一切只是编译、运行,然后在正确的位置显示所有内容,除了人 imageView 在左上角而不是船内。如果有人可以向我指出我做错了什么以及最好的方法是拥有这样的多层,我将不胜感激。

没有错误,人对象只是没有出现在船上。

以下是主要活动:

package com.cannibal_photographer;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

    FrameLayout main = (FrameLayout)findViewById(R.layout.activity_main);
    LayoutInflater li = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup boatview = (ViewGroup) li.inflate(R.layout.boatlayout,main,false);
    View personview = li.inflate(R.layout.personlayout,boatview,false);

    ((ViewGroup) main).addView(boatview);
    ((ViewGroup) boatview).addView(personview);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

这是船类:

package com.cannibal_photographer;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class Boat extends ImageView {

boolean state = true;

public Boat(Context context) {
    super(context);
    init();
}

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

 public Boat(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

private void init()
{
    this.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick (View v) {
            if (state) {
                moveBoat(-215);
            } else {
                moveBoat(215);
            }
        }
    });
}

//TranslateAnimation animation;
//TranslateAnimation animation2;

public void moveBoat(int amount){
    /*
    animation = new TranslateAnimation(0, 0, 0, amount);
    animation.setDuration(250);
    animation.setFillAfter(true);
    this.startAnimation(animation);
    */
    this.offsetTopAndBottom(amount);
    state = !state;
}
}

这是人员类:

package com.cannibal_photographer;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class Person extends ImageView {

boolean state = true;

public Person(Context context) {
    super(context);
    init();
}

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

 public Person(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

private void init()
{
    this.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick (View v) {
            if (state) {
                //movePerson(325,-230);
            } else {
                //movePerson(-325,230);
            }
        }
    });


}

public void movePerson(int x, int y)
{
    this.offsetLeftAndRight(x);
    this.offsetTopAndBottom(y);

    state = !state;
}

}

这是背景的主要布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@layout/activity_main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
</FrameLayout>

这是船的布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/framelayoutView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.cannibal_photographer.Boat
    android:id="@+id/boatimageView"
    android:layout_width="78dp"
    android:layout_height="130dp"
    android:layout_marginLeft="105dp"
    android:layout_marginTop="225dp"
    android:src="@drawable/boat" />

</FrameLayout>

最后,这是人物布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.cannibal_photographer.Person
            android:id="@+id/personView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/greensquare"
            android:gravity="center_vertical" />

</FrameLayout>
4

0 回答 0