Android中的GalleryView实例演示-周末福利有美女图
16lz
2021-01-24
今天周末,祝周末愉快!
先看实例效果:
滑动小图点击后上面的大图就和小图一致了
activity代码:
package com.tmacsky;import android.app.Activity;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.ImageSwitcher;import android.widget.ImageView;import android.widget.Toast;import android.widget.ViewSwitcher.ViewFactory;public class GalleryActivity extends Activity implements ViewFactory {int index = 0;ImageSwitcher imageSwitcher;int [] image = {R.drawable.ha,R.drawable.hb,R.drawable.hc,R.drawable.hd,R.drawable.he,R.drawable.hf,R.drawable.hg,R.drawable.hh};@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.gallery);//设置imageswitchimageSwitcher = (ImageSwitcher)findViewById(R.id.imageswitcher);//设置一个容器imageSwitcher.setFactory(this);//给一个初始的值imageSwitcher.setImageResource(image[index]);//定义galleryGallery gallery = (Gallery)findViewById(R.id.gallery);//设置adaptergallery.setAdapter(new ImageAdapter());gallery.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stub//设置滑动后弹出切换图片的数字Toast.makeText(GalleryActivity.this, " " + arg2, 3000).show();imageSwitcher.setImageResource(image[arg2]);}});}class ImageAdapter extends BaseAdapter{public int getCount() {// TODO Auto-generated method stubreturn image.length;}public Object getItem(int position) {// TODO Auto-generated method stubreturn image[position];}public long getItemId(int position) {// TODO Auto-generated method stubreturn image[position];}public View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubImageView imageView = new ImageView(GalleryActivity.this);imageView.setPadding(10, 50, 10, 5);imageView.setImageResource(image[position]);return imageView;}}//设置工厂的时候,系统需要返回一个新的imageview,此处直接是默认的就可以了public View makeView() {// TODO Auto-generated method stubreturn new ImageView(this);}}gallery.xml的文件代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><ImageSwitcher android:id="@+id/imageswitcher" android:background="@drawable/hc" android:inAnimation="@android:anim/fade_in" android:outAnimation="@android:anim/fade_out" android:layout_gravity="center_vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" ></ImageSwitcher><Gallery android:layout_width="200px" android:layout_height="wrap_content" android:id="@+id/gallery" android:layout_gravity="center_horizontal" ></Gallery></LinearLayout>
那啥图片资源什么的自己可以找自己喜欢的图,OK~
更多相关文章
- ViewPager用法(一)图片+原点+循环播放
- Android(安卓)设置TextView滑动滚动条和滑动效果
- RelativeLayout布局属性详解
- 《Android第一行代码》first reading 十一
- Android(安卓)7.0 系统解决拍照的问题 exposed beyond app throu
- Android两种 旋转Bitmap方法
- 【工具类】如何通过代码安装一个apk文件
- Android多点触摸缩放图片-android学习之旅(四)
- android中比较两张图片的相似度