gallery使用的一个demo

首先是原始代码:

布局文件:Layout/main.xml,里面声明了一个gallery的空间和一个图片显示的空间:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="fill_parent"

   android:orientation="vertical"

  android:layout_height="wrap_content">

    <Gallery android:id="@+id/gallery1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:gravity="center_vertical"

        android:spacing="16dp"

    />

    <ImageView

    android:id="@+id/iv"

    android:layout_gravity="center_vertical"

    android:layout_marginTop="20px"

    android:layout_width="320px"

    android:layout_height="320px"

    ></ImageView>

</LinearLayout>

其次资源文件/drawable下:

Acitivity文件GalleryDemo.java

package com.example.gallarydemo;

 

import android.R.layout;

import android.app.Activity;

import android.content.Context;

import android.content.res.TypedArray;

import android.database.Cursor;

import android.graphics.Color;

import android.os.Bundle;

import android.provider.Contacts.People;

import android.view.ContextMenu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.view.ContextMenu.ContextMenuInfo;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.BaseAdapter;

import android.widget.Gallery;

import android.widget.ImageView;

import android.widget.SimpleCursorAdapter;

import android.widget.SpinnerAdapter;

import android.widget.Toast;

import android.widget.AdapterView.AdapterContextMenuInfo;

 

/**

 * @author square_l

 *

 */

public class GalleryDemo extends Activity {

         private Gallery gallery = null;

         private ImageView imageView = null;

    /* 使用res/drawable图片作为图片来源 */

    private int[] myImageIds = { R.drawable.wifi1,R.drawable.wifi2,R.drawable.wifi3,R.drawable.wifi4,

                       R.drawable.wifi5,    R.drawable.wifi6};

         /** Called when the activity is first created. */

         @Override

         public void onCreate(Bundle savedInstanceState) {

             super.onCreate(savedInstanceState);

             setContentView(R.layout.main);

 

 

             imageView = (ImageView) findViewById(R.id.iv);

             gallery = ((Gallery) findViewById(R.id.gallery1));

             ImageAdapter adapter = new ImageAdapter(this,myImageIds);

            

           

             gallery.setOnItemClickListener(new OnItemClickListener() {

            //用户点击图片时,将该图片的ResourceID设到下面的ImageView中去,

            @Override

            public void onItemClick(AdapterView<?> arg0, View view, int position,

                    long arg3) {

               

                imageView.setImageResource(myImageIds[position]);

               

            }

        });

        gallery.setAdapter(adapter);

 

         }

 

         public class ImageAdapter extends BaseAdapter {

             /* 类成员 myContextContext父类 */

             private Context myContext;

 

             /* 使用res/drawable图片作为图片来源 */

             private int[] myImageIds = null;

 

             /* 构造器只有一个参数,即要存储的Context */

             public ImageAdapter(Context c,int[] myImageIds) {

              this.myContext = c;

              this.myImageIds = myImageIds;

             }

 

             /* 返回所有已定义的图片总数量 */

             public int getCount() {

              return this.myImageIds.length;

             }

 

             /* 利用getItem方法,取得目前容器中图像的数组ID */

             public Object getItem(int position) {

              return position;

             }

 

             public long getItemId(int position) {

              return position;

             }

 

             /* 取得目前欲显示的图像View,传入数组ID值使之读取与成像 */

             public View getView(int position, View convertView, ViewGroup parent) {

              /* 创建一个ImageView对象 */

              ImageView i = new ImageView(this.myContext);

              i.setImageResource(this.myImageIds[position]);

              i.setScaleType(ImageView.ScaleType.FIT_XY);

 

              /* 设置这个ImageView对象的宽高,单位为dip */

              i.setLayoutParams(new Gallery.LayoutParams(120, 120));

              return i;

             }

 

             /* 依据距离中央的位移量利用getScale返回views的大小(0.0f to 1.0f) */

             public float getScale(boolean focused, int offset) {

              /* Formula: 1 / (2 ^ offset) */

              return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));

             }

            

         }

}

以上是最简单的例子,效果如图所示:

如果想要实现以下效果:

第一种,实现选中的图片比较明亮点,而没选中的不明亮

只需要,在上面的程序中设置两个地方,

activity中设置,没被选中时的透明度:

gallery.setUnselectedAlpha(0.5f);

选中,在ImageAdaptergetView(int position, View convertView, ViewGroup parent)中,设置imageview.setBackgroundColor(Color.alpha(1)); 背景色为1

第二种情况是让图片循环显示,也就是说图片显示到最后一张以后就显示第一张,效果如下图

修改的地方有三个:

在ImageAdapter中的getCount() 方法中,修改返回值为无穷大 return Integer.MAX_VALUE;

在ImageAdapter中的getView(int position, View convertView, ViewGroup parent)方法中,设置imageview.setImageResource(imgs[position % imgs.length]); 取余

Activity中增加选中时候取余的,要不然选中会出错imageView.setImageResource(myImageIds[position%myImageIds.length]);

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值