在 Android Studio 中分层图像?
Layering images in Android Studio?
目前我有一个 UI 设计的图像视图,问题是它们不能重叠或分层。 (或者如果他们可以,我还没弄清楚怎么做)
我想做3层,最底层是板子,
下一层是碎片,
下一层是具有有效动作的选定棋子。
我的想法是只在顶层有监听器。
我只是在寻找为此使用的最佳数据结构,该应用程序是我正在开发的类似国际象棋的游戏。
如果有另一种方法可能更好,我也洗耳恭听。
谢谢!
根据要求的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game Activity"
android:id="@+id/textView5"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView22"
android:src="@drawable/ni_whitesquare"
android:layout_below="@+id/textView5"
android:layout_alignRight="@+id/textView5"
android:layout_alignEnd="@+id/textView5"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="76dp"
android:contentDescription="whitesquare"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView32"
android:src="@drawable/ni_blacksquare"
android:layout_alignTop="@+id/imageView22"
android:layout_alignLeft="@+id/imageView22"
android:layout_alignStart="@+id/imageView22"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:contentDescription="blacksquare"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView12"
android:src="@drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_alignTop="@+id/imageView22"
android:layout_toLeftOf="@+id/imageView22"
android:layout_toStartOf="@+id/imageView22" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView21"
android:src="@drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_below="@+id/imageView22"
android:layout_toRightOf="@+id/imageView12"
android:layout_toEndOf="@+id/imageView12" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView23"
android:src="@drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_above="@+id/imageView12"
android:layout_toLeftOf="@+id/imageView33"
android:layout_toStartOf="@+id/imageView33" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView31"
android:src="@drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignBottom="@+id/imageView21"
android:layout_alignLeft="@+id/imageView32"
android:layout_alignStart="@+id/imageView32" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView11"
android:src="@drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="@+id/imageView21"
android:layout_toLeftOf="@+id/imageView21"
android:layout_toStartOf="@+id/imageView21" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView13"
android:src="@drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="@+id/imageView23"
android:layout_alignLeft="@+id/imageView12"
android:layout_alignStart="@+id/imageView12" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView33"
android:src="@drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="@+id/imageView23"
android:layout_alignLeft="@+id/imageView32"
android:layout_alignStart="@+id/imageView32" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/directions"
android:id="@+id/directionsstring"
android:layout_marginTop="30dp"
android:layout_below="@+id/imageView21"
android:layout_alignLeft="@+id/textView5"
android:layout_alignStart="@+id/textView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Startingpoint"
android:src="@drawable/ni_pawn"
android:layout_above="@+id/imageView11"
android:layout_toLeftOf="@+id/imageView12"
android:layout_toStartOf="@+id/imageView12" />
</RelativeLayout>
Activity :
public class GameActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final ImageView mStarting = (ImageView) findViewById(R.id.Startingpoint);
final ImageView mCenterSq = (ImageView) findViewById(R.id.imageView22);
final ImageView m12 = (ImageView)findViewById(R.id.imageView12);
mCenterSq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
m12.setImageResource(R.drawable.ni_blacksquare);
mCenterSq.setImageResource(R.drawable.ni_portal);
//center square turns into a portal when clicked.
if (mStarting.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_whitesquare).getConstantState()) &&
mCenterSq.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_portal).getConstantState()) )
{
mCenterSq.setOnClickListener(null);
return;
}
mStarting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
//Pawn selected
mStarting.setImageResource(R.drawable.ni_whitesquare);
m12.setImageResource(R.drawable.ni_greensquare);
mCenterSq.setImageResource(R.drawable.ni_greensquare);
if (mStarting.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_whitesquare).getConstantState()))
{
mStarting.setOnClickListener(null);
}
m12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if (m12.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_greensquare).getConstantState()))
{
m12.setImageResource(R.drawable.ni_pawn);
mCenterSq.setImageResource(R.drawable.ni_portal);
}
}
});
}
});
};
});
}
}
对于分层视图,您可能想要使用 FrameLayout 参见此答案:Layout Layers? Z-Axis?
此外,如果您尝试设置一个布局,其中有几个连续的视图需要一个接一个地排列,最合适的元素通常是 LinearLayout。 LinearLayout 将根据您为它设置的 android:orientation 属性,按照您声明它们的顺序自动按连续顺序设置您的视图。
总的来说,我建议查看其中每一个的文档。 RelativeLayout 可能不是要走的路,因为它涉及大量的视图定位管理,这对于我认为您正在尝试做的事情来说似乎很无聊。希望对您有所帮助
目前我有一个 UI 设计的图像视图,问题是它们不能重叠或分层。 (或者如果他们可以,我还没弄清楚怎么做)
我想做3层,最底层是板子,
下一层是碎片,
下一层是具有有效动作的选定棋子。
我的想法是只在顶层有监听器。
我只是在寻找为此使用的最佳数据结构,该应用程序是我正在开发的类似国际象棋的游戏。
如果有另一种方法可能更好,我也洗耳恭听。
谢谢!
根据要求的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game Activity"
android:id="@+id/textView5"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView22"
android:src="@drawable/ni_whitesquare"
android:layout_below="@+id/textView5"
android:layout_alignRight="@+id/textView5"
android:layout_alignEnd="@+id/textView5"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="76dp"
android:contentDescription="whitesquare"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView32"
android:src="@drawable/ni_blacksquare"
android:layout_alignTop="@+id/imageView22"
android:layout_alignLeft="@+id/imageView22"
android:layout_alignStart="@+id/imageView22"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:contentDescription="blacksquare"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView12"
android:src="@drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_alignTop="@+id/imageView22"
android:layout_toLeftOf="@+id/imageView22"
android:layout_toStartOf="@+id/imageView22" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView21"
android:src="@drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_below="@+id/imageView22"
android:layout_toRightOf="@+id/imageView12"
android:layout_toEndOf="@+id/imageView12" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView23"
android:src="@drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_above="@+id/imageView12"
android:layout_toLeftOf="@+id/imageView33"
android:layout_toStartOf="@+id/imageView33" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView31"
android:src="@drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignBottom="@+id/imageView21"
android:layout_alignLeft="@+id/imageView32"
android:layout_alignStart="@+id/imageView32" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView11"
android:src="@drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="@+id/imageView21"
android:layout_toLeftOf="@+id/imageView21"
android:layout_toStartOf="@+id/imageView21" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView13"
android:src="@drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="@+id/imageView23"
android:layout_alignLeft="@+id/imageView12"
android:layout_alignStart="@+id/imageView12" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView33"
android:src="@drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="@+id/imageView23"
android:layout_alignLeft="@+id/imageView32"
android:layout_alignStart="@+id/imageView32" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/directions"
android:id="@+id/directionsstring"
android:layout_marginTop="30dp"
android:layout_below="@+id/imageView21"
android:layout_alignLeft="@+id/textView5"
android:layout_alignStart="@+id/textView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Startingpoint"
android:src="@drawable/ni_pawn"
android:layout_above="@+id/imageView11"
android:layout_toLeftOf="@+id/imageView12"
android:layout_toStartOf="@+id/imageView12" />
</RelativeLayout>
Activity :
public class GameActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final ImageView mStarting = (ImageView) findViewById(R.id.Startingpoint);
final ImageView mCenterSq = (ImageView) findViewById(R.id.imageView22);
final ImageView m12 = (ImageView)findViewById(R.id.imageView12);
mCenterSq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
m12.setImageResource(R.drawable.ni_blacksquare);
mCenterSq.setImageResource(R.drawable.ni_portal);
//center square turns into a portal when clicked.
if (mStarting.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_whitesquare).getConstantState()) &&
mCenterSq.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_portal).getConstantState()) )
{
mCenterSq.setOnClickListener(null);
return;
}
mStarting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
//Pawn selected
mStarting.setImageResource(R.drawable.ni_whitesquare);
m12.setImageResource(R.drawable.ni_greensquare);
mCenterSq.setImageResource(R.drawable.ni_greensquare);
if (mStarting.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_whitesquare).getConstantState()))
{
mStarting.setOnClickListener(null);
}
m12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if (m12.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_greensquare).getConstantState()))
{
m12.setImageResource(R.drawable.ni_pawn);
mCenterSq.setImageResource(R.drawable.ni_portal);
}
}
});
}
});
};
});
}
}
对于分层视图,您可能想要使用 FrameLayout 参见此答案:Layout Layers? Z-Axis?
此外,如果您尝试设置一个布局,其中有几个连续的视图需要一个接一个地排列,最合适的元素通常是 LinearLayout。 LinearLayout 将根据您为它设置的 android:orientation 属性,按照您声明它们的顺序自动按连续顺序设置您的视图。
总的来说,我建议查看其中每一个的文档。 RelativeLayout 可能不是要走的路,因为它涉及大量的视图定位管理,这对于我认为您正在尝试做的事情来说似乎很无聊。希望对您有所帮助