android gallery当前view变大,【Android】Gallery实现选中图片变大,两侧没选中图片变小...

先上图

0818b9ca8b590ca3270a3433284dd417.png

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

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) {

//没选中

}

});

}

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

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

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">

android:id="@+id/gallery"

android:layout_marginTop="50dp"

android:layout_width="match_parent"

android:layout_height="wrap_content">

-----接下来是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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值