gpt4 book ai didi

Android 显示全屏 slider 图像

转载 作者:太空宇宙 更新时间:2023-11-03 13:26:22 24 4
gpt4 key购买 nike

我在 Android 屏幕上显示了一个 gridviewImages。我的需求是滑动全屏图片

这里是我的代码:

图像适配器:

public class ImageAdapter extends BaseAdapter {
private Context mContext;

// Constructor
public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return mThumbIds.length;
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}

imageView.setImageResource(mThumbIds[position]);
return imageView;
}

// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.cn1, R.drawable.cn2,
R.drawable.cn3, R.drawable.cn4,
R.drawable.cn5, R.drawable.cn6,
R.drawable.cn7, R.drawable.cn8,
R.drawable.cn9, R.drawable.cn10,
R.drawable.cn11, R.drawable.cn12

};
}

主要 Activity :

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {

// Send intent to SingleViewActivity
Intent i =
new Intent(getApplicationContext(), SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
Log.d("onCreateMain>>","OnCreateMain");

startActivity(i);
}
});
}

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

}

单 View Activity :

public class SingleViewActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
Log.d("onCreate>>","OnCreate");

// Get intent data
Intent i = getIntent();

// Selected image id
int position = i.getExtras().getInt("id");

Log.d("Position>>>","Position"+position);
ImageAdapter imageAdapter = new ImageAdapter(this);

ImageView imageView = (ImageView) findViewById(R.id.SingleView);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}

}

布局 XMl 文件:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>

single_view.xml:

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

<ImageView android:id="@+id/SingleView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

</LinearLayout>

我的要求是滑动全屏图片,有人可以帮忙吗?

最佳答案

ViewPager 将让您在 fragment 之间“滑动”,这正是您想要达到的效果。只需将您当前的 SingleViewActivity 变成一个 fragment :

public class SingleViewFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.single_view, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

int resource = getArguments().getInt("image");

ImageView imageView = (ImageView) findViewById(R.id.SingleView);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}

您将需要一个 PagerAdapter 来根据需要生成此 fragment 。

public class ImagesPagerAdapter extends FragmentPagerAdapter{

public Integer[] mThumbIds = {
R.drawable.cn1, R.drawable.cn2,
R.drawable.cn3, R.drawable.cn4,
R.drawable.cn5, R.drawable.cn6,
R.drawable.cn7, R.drawable.cn8,
R.drawable.cn9, R.drawable.cn10,
R.drawable.cn11, R.drawable.cn12
};

@Override
public Fragment getItem(int i) {
Bundle args = new Bundle();
args.putInt("image", mThumbIds[i]);

SingleViewFragment fragment = new SingleViewFragment();
fragment.setArguments(args);

return fragment;
}

@Override
public int getCount() {
return mThumbIds.length;
}
}

然后您将需要一个包含单个 ViewPager 的 Activity ,将 ImagesPagerAdapter 的实例设置为其适配器,并为其指定一个开始位置。

public class SingleViewActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pager);

// Selected image id
int position = getIntent().getIntExtra("id", 0);

ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ImagesPagerAdapter(this));
pager.setCurrentItem(position);
}
}

其中 activiy_pager 只是一个包含您需要的 ViewPager 的布局。

<ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

关于Android 显示全屏 slider 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19057996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com