【Android】Gallery实现选中图片变大,两侧没选中图片变小

先上图

当滑动时,选中的图片会变大,两侧的图片会变小。接下来上代码



MainActivity.Java

public class MainActivity extends AppCompatActivity {
    private Gallery gallery;
    private ImageAdapter imageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gallery = (Gallery) findViewById(R.id.gallery);
        imageAdapter = new ImageAdapter(this);
        gallery.setSpacing(10);//图与图之间的横向距离
        gallery.setAdapter(imageAdapter);
        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                // 选中Gallery中某个图像时,放大显示该图像
                imageAdapter.setSelectItem(position);
                imageAdapter.notifyDataSetChanged();//当滑动时,事件响应,通知适配器更新数据
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                //没选中
            }
        });
    }
}

-------------是不是很简单呢?--------------------

<pre style="font-family: 宋体; font-size: 15pt; background-color: rgb(255, 255, 255);"><span style="background-color:#e4e4ff;">activity_main:</span>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.abc.gallerydemo.MainActivity">

    <Gallery
        android:id="@+id/gallery"
        android:layout_marginTop="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </Gallery>
</RelativeLayout>

-----接下来是ImageAdapter--------

public class ImageAdapter extends BaseAdapter{
    private Context mContext;
    private int mGalleryItemBackground;
    private int[] myImageIds = {R.drawable.icon_bmfw, R.drawable.icon_bszn, R.drawable.icon_sqgg, R.drawable.icon_sqxw, R.drawable.icon_zcbx,};
    private int selectItem;

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = mContext.obtainStyledAttributes(R.styleable.Gallery); /* 使用在res/values/attrs.xml中的定义 的Gallery属性. */
//        mGalleryItemBackground = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0); ///*取得Gallery属性的Index
        a.recycle();/* 让对象的styleable属性能够反复使用 */
    }
    @Override
    public int getCount() {
        return Integer.MAX_VALUE;//最大值能使图片无限滑动
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setSelectItem(int selectItem) {
        this.selectItem = selectItem;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(myImageIds[position%myImageIds.length]);//实现循环滑动
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        if(selectItem==position){
            //选中时的动画
//            Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.my_scale_action);    //实现动画效果
//            imageView.startAnimation(animation);  //选中时,这时设置的比较大
            imageView.setLayoutParams(new Gallery.LayoutParams(320,240));
        }
        else{
            //未选中时的动画
//            Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.my_scale_action);    //实现动画效果
//            imageView.startAnimation(animation);
            imageView.setLayoutParams(new Gallery.LayoutParams(160,120));//未选中
        }
        return imageView;
    }
}

代码里都有注释,很简单而且一读就懂。我也是一名刚入门的新手,希望与大家共同进步。

 

源码:http://download.csdn.net/detail/qq_24531461/9649849



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
是的,Gallery小部件可以自定义图片布局显示。你可以使用Gallery小部件的模板来自定义图片的布局和样式。 在使用Gallery小部件时,你可以通过设置`template`属性来指定自定义的布局模板。这样你就可以根据自己的需求来设计图片的显示方式。 以下是一个示例,展示了如何使用Gallery小部件的自定义模板来自定义图片布局显示: ```php use dosamigos\gallery\Gallery; // ... echo Gallery::widget([ 'items' => $items, 'options' => ['class' => 'gallery-widget'], 'template' => '<div class="custom-gallery">{items}</div>', 'itemTemplate' => '<div class="custom-gallery-item">{image}</div>', 'clientOptions' => [ 'container' => '.custom-gallery', ], ]); ``` 在上述示例中,我们通过设置`template`属性来指定最外层容器的布局模板,即`<div class="custom-gallery">{items}</div>`。你可以根据需要修改这个模板,添加自己的CSS类名和HTML结构。 同时,我们还通过设置`itemTemplate`属性来指定每个图片项的布局模板,即`<div class="custom-gallery-item">{image}</div>`。你可以在这个模板中使用`{image}`占位符来插入图片元素。 最后,我们通过`clientOptions`属性将容器选择器`.custom-gallery`传递给客户端脚本,以确保正确地应用自定义样式和行为。 通过这种方式,你可以根据自己的需求自定义Gallery小部件的图片布局显示。记得根据你的实际情况修改模板和样式,以实现你期望的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值