私はスタートメニューで、ボタンをクリックするとストーリーイベントが起こるようなゲームを作ろうとしています。プレイヤーが使用している文字に応じてボタンをクリックすると、異なるストーリーが表示されるため、ストーリーのために10億の異なるアクティビティや断片を作りたくありません。Android:ビューをプリロードするには?
同じアクティビティ内で切り替えるレイアウトが2つあります。 レイアウト1からレイアウト2に切り替えるには、レイアウト1をレイアウト2に追加しようとしましたが、ViewFlipper
も試しましたが、レイアウト1からレイアウト2に切り替えると、レイアウト2のTextView
は、私が言いたいことに変わります。一番下にあると思われるテキストボックスが最初に表示され、次に一番下にジャンプします
スレッドを使用してレイアウト2のプリロードが試されましたが、レイアウト2が壊れてしまいます。私がやります?ここで
は、各文字の
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chara"
android:scaleType="fitStart"
android:adjustViewBounds="true"
/>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textbox"
android:layout_gravity="bottom">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/vntextbox"
android:adjustViewBounds="true"
android:scaleType="fitEnd">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/story" />
</RelativeLayout>
</merge>
物語のための物語
public class Story extends FrameLayout {
String[][] story;
int H;
int W;
int L;
int index =0;
TextView text;
ImageView character;
View v;
Story(Context context,String[][]story,int length){
super(context);
this.story=story;
this.L = length;
init(context);
}
public void init(Context context) {
LayoutInflater l = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = l.inflate(R.layout.story, this, true);
final RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final FrameLayout.LayoutParams imageParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
text = (TextView) v.findViewById(R.id.story);
character = (ImageView) v.findViewById(R.id.chara);
final RelativeLayout textBox = (RelativeLayout) v.findViewById(R.id.textbox);
textBox.post(new Runnable() {
@Override
public void run() {
H = textBox.getHeight();
W = textBox.getWidth();
textParams.setMargins(W/12, H/8, W/10, H/8);
text.setLayoutParams(textParams);
text.setText(story[index][0]);
}
});
v.post(new Runnable() {
@Override
public void run() {
int h = v.getHeight();
imageParams.setMargins(W/7 * 2, h/10, 0, H/3);
character.setLayoutParams(imageParams);
character.setImageResource(Integer.parseInt(story[index][1]));
}
});
}
}
XMLのための私のクラスで、彼らは彼らの物語を返すメソッドを持って、特定の文字の例:
public class Player extends ImageView {
Context context;
Player (Context context){
super(context);
this.context=context;
}
public Story getIntro(){
int normal = R.drawable.normal;
int serious = R.drawable.serious;
int happy = R.drawable.happy;
final int length = 8;
String[][] story = // a story
final Story s = new Story(context, story, l);
s.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do some stuff
}
});
return s;
}
}
ここでは物語が使われている場所です
public class StartMenu extends AppCompatActivity {
@Override
protected void onCreate(final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.startmenu);
final RelativeLayout r = (RelativeLayout)findViewById(R.id.startscreen);
final Button b =new Button(this);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//here I want to do something like this: (doesn't work due to problems described above)
Player p = new Player(StartMenu.this);
Story s = p.getIntro();
r.removeAllViews();
r.addView(s);
}
});
私は断片に慣れていません。ストーリークラス拡張フラグメントを作成して、それを作成するたびにアクティビティを切り替えることはできますか? – Pear
> "フラグメントは、独自のライフサイクルを持ち、独自の入力イベントを受け取り、アクティビティの実行中に追加または削除できるアクティビティのモジュラセクションと考えることができます(「サブアクティビティ」のようなもの)あなたはさまざまな活動で再利用できます)。 Fragmentを作成し、Storyクラスのロジックをその中に入れることができます。 :) –
私は断片を使ってみました。主なアクティビティをストーリーフラグメントに置き換えると、ストーリーフラグメントのレイアウトが乱れてしまいます(つまり、イメージビューは、私が設定した位置ではなく、左上隅に表示されます)。それを解決する方法はありますか? – Pear