首页 > 代码库 > Gallery实现图片文字左右滑动

Gallery实现图片文字左右滑动

明天周六了,又可以睡懒觉了

毕业快五个月了,进入社会的赶脚就是不一样,事实证明,我很倔强,倔强到别人很难改变我,我只做我喜欢的事情,这段时间静下来学习,感觉挺好,大学都没这个认真过,因为有梦想,所以要前行,这样的生活状态,还是比较喜欢的,我从一个什么都不会的门外汉,到现在自己研究源码,一切都很惬意,也很开心,我愿意去做,我喜欢去做,只要能提升,不管提升的是什么,只要有价值就好,减少做没有意义的事情...

最近都是在维护之前写的代码,闲散的时间比较多,一般都是在学习,看到群里有求助的就去回答,让我自己能看到自己的价值,共同分享,同样这个项目也来自于群里一哥们问我的,给了一张图片问怎么实现就研究了一下,虽说Gallery已经不怎么用了,但是能实现这个功能就好,当然你也可以自定义

先上图:


开始的时候用的动画,但是由于不好控制图片的颜色,后来改成了现在的样子,这里的文字和图片可以私人订制


上代码:

1.主布局

<LinearLayout 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"
    android:gravity="center"
    android:orientation="vertical">

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:spacing="20dp"/>
    
</LinearLayout>

这里比较简单就只是一个Gallery

2.Gallery的适配器

package com.sdufe.thea.guo.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdufe.thea.guo.R;
import com.sdufe.thea.guo.model.GalleryModel;

public class PictureAdapter extends BaseAdapter {

	private Context context;
	private int selectItem;
	private List<GalleryModel> list;

	public PictureAdapter(Context context, List<GalleryModel> list) {
		super();
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		if (list != null) {
			return list.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (list != null) {
			return list.get(position);
		}
		return null;
	}

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

	public void setSelectItem(int selectItem) {

		if (this.selectItem != selectItem) {
			this.selectItem = selectItem;
			notifyDataSetChanged();
		}
	}

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

		ViewHold hold;

		if (convertView == null) {
			hold = new ViewHold();
			convertView = LayoutInflater.from(context).inflate(R.layout.gallery_item, null);
			convertView.setTag(hold);
		} else {
			hold = (ViewHold) convertView.getTag();
		}
		hold.mImageView = (ImageView) convertView.findViewById(R.id.imageview);
		hold.mTextView = (TextView) convertView.findViewById(R.id.text);

		hold.mImageView.setImageResource(list.get(position).getImageView());
		hold.mTextView.setText(list.get(position).getText());
		if (selectItem == position) {
			hold.mImageView.setLayoutParams(new LinearLayout.LayoutParams(150, 180));
			hold.mTextView.setTextSize(20);
			hold.mTextView.setFocusable(true);
		} else {
			hold.mImageView.setLayoutParams(new LinearLayout.LayoutParams(120, 150));
			hold.mTextView.setTextSize(20);
			hold.mTextView.setFocusable(false);
		} 
		return convertView;
	}

	static class ViewHold {
		public TextView mTextView;
		private ImageView mImageView;
	}

}

适配器的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center">
    
    <ImageView 
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src=http://www.mamicode.com/"@drawable/one_select">
Gallery在acitivity的使用

package com.sdufe.thea.guo;

import java.util.ArrayList;
import java.util.List;

import com.sdufe.thea.guo.adapter.PictureAdapter;
import com.sdufe.thea.guo.model.GalleryModel;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;

public class MainActivity extends Activity {

	private Gallery gallery;
	private PictureAdapter adapter;
	private List<GalleryModel> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initData();
		initView();
	}

	private void initView() {
		gallery = (Gallery) findViewById(R.id.gallery);
		adapter = new PictureAdapter(this, list);

		gallery.setAdapter(adapter);


		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				adapter.setSelectItem(position);
				// ScaleAnimation animation = new ScaleAnimation(1, 1.5f, 1,
				// 1.5f,
				// Animation.RELATIVE_TO_SELF, 0.5f,
				// Animation.RELATIVE_TO_SELF, 0.5f);
				// animation.setDuration(2000);
				// view.startAnimation(animation);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
	}

	private void initData() {
		list = new ArrayList<GalleryModel>();

		list.add(new GalleryModel(R.drawable.one_select, "房产交易"));
		list.add(new GalleryModel(R.drawable.two_select, "附近洗车店"));
		list.add(new GalleryModel(R.drawable.three_select, "社区商城"));
	}

}

这里来个总结:第一次setOnItemSelectedListener()源码中的大致意思就是你使用选中功能的时候使用这个方法,我们这里实现的功能就是左右滑动图片,当滑动到哪一个时哪一个就放大对应的图片和文字


ok,结束


源码下载地址:http://download.csdn.net/detail/elinavampire/8184077

github下载地址:https://github.com/zimoguo/GalleryPicture

Gallery实现图片文字左右滑动