我正在使用图库视图制作记忆游戏。我希望所有卡片都显示在底部,当用户从图库中选择一张卡片时,特定卡片会显示在其上方(在图片视图中)。当用户触摸该卡时,卡应该转动(使用翻转动画)并且应该显示卡的正面(这应该是新的活动)。在这个翻转过程中,我一直想在底部显示画廊。我怎样才能做到这一点?我有以下代码没有错误,但应用程序崩溃后才开始。在查看log cat时,它会在启动新酸度时出现错误。
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class GalleryActivity extends Activity {
//---the images to display---
Integer[] imageIDs = {
R.drawable.you,
R.drawable.date,
R.drawable.competition,
R.drawable.contacts,
R.drawable.memory,
R.drawable.mission,
R.drawable.recognition,
R.drawable.report,
R.drawable.stallion,
R.drawable.strategy,
R.drawable.team
};
ImageView youview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
Toast.makeText(getBaseContext(),
"card" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
//---display the images selected---
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
youview=(ImageView)findViewById(R.drawable.you);
youview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(GalleryActivity.this, You.class);
startActivity(intent);
}
});
}
public class ImageAdapter extends BaseAdapter
{
Context context;
int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the item---
public Object getItem(int position) {
return position;
}
//---returns the ID of an item---
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
这是我正在使用的布局: -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#006600">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose the card"
android:textSize="20dp"
android:textStyle="bold"/>
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
<ImageView
android:id="@+id/image1"
android:layout_width="320dp"
android:layout_height="400dp"
android:layout_above="@+id/gallery1"
android:scaleType="fitXY" />
</RelativeLayout>
答案 0 :(得分:1)
你应该使用一个片段:
private void addCardFragment() {
String fragmentTag = getFragmentTag();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.card_flip_in, R.animator.card_flip_out);
CardFragment fragment = new CardFragment();
//set bundle with argument for image id to show if fragment
fragmentTransaction.replace(R.id.your_relative_or_frame_layout, CardFragment, fragmentTag);
fragmentTransaction.commit();
fragmentTransaction = null;
}