Android Gallery效果

本文介绍了一个使用Android Gallery组件实现图片轮播的应用实例。通过两个XML布局文件定义界面,并在Java代码中设置图片资源及监听事件,实现了图片轮播功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.两个布局文件

第一个布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <Gallery
       android:id="@+id/gallery"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:spacing="10dp"/>
   <ImageView
       android:id="@+id/imageview"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:scaleType="fitXY"/>
</LinearLayout>

第二个布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ImageView android:id="@+id/cell_imageview"
        android:layout_width="50dp"
        android:layout_height="80dp"
        android:scaleType="fitXY"/> 
</LinearLayout>

java部分

public class MainActivity extends Activity {

 private Gallery gallery;
 private ImageView imageview;
 private int[] images = {R.drawable.p1,
   R.drawable.p2, R.drawable.p3,
   R.drawable.p4, R.drawable.p5,
   R.drawable.p6,R.drawable.p7};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  gallery = (Gallery) findViewById(R.id.gallery);
  imageview = (ImageView)findViewById(R.id.imageview);
  
  MyAdapter adapter = new MyAdapter();
  gallery.setAdapter(adapter);
  gallery.setSelection(Integer.MAX_VALUE/2);
  imageview.setImageResource(images[(Integer.MAX_VALUE/2)%images.length]);

  gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView<?> parent, View view,
     int position, long id) {
    // TODO Auto-generated method stub
    System.out.println("position="+position);
    imageview.setImageResource(images[position%images.length]);
   }

   @Override
   public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub
    
   }
  });
 }
 
 class MyAdapter extends BaseAdapter{

  @Override
  public int getCount() {
   // TODO Auto-generated method stub
   return Integer.MAX_VALUE;
  }

  @Override
  public Object getItem(int position) {
   // TODO Auto-generated method stub
   return null;
  }

  @Override
  public long getItemId(int position) {
   // TODO Auto-generated method stub
   return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   // TODO Auto-generated method stub
   LayoutInflater l = LayoutInflater.from(MainActivity.this);
   View view = l.inflate(R.layout.cell, null);
   ImageView imageview = (ImageView)view.findViewById(R.id.cell_imageview);
   imageview.setImageResource(images[position%images.length]);
    
   return view; 
  }
  
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值