Gallery实现图片文字左右滑动

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

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

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

先上图:

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

 

上代码:

1.主布局

 

[html] view plain copy
 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical">  
  7.   
  8.     <Gallery  
  9.         android:id="@+id/gallery"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:spacing="20dp"/>  
  13.       
  14. </LinearLayout>  


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

 

2.Gallery的适配器

 

[java] view plain copy
 
  1. package com.sdufe.thea.guo.adapter;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.ImageView;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.TextView;  
  13.   
  14. import com.sdufe.thea.guo.R;  
  15. import com.sdufe.thea.guo.model.GalleryModel;  
  16.   
  17. public class PictureAdapter extends BaseAdapter {  
  18.   
  19.     private Context context;  
  20.     private int selectItem;  
  21.     private List<GalleryModel> list;  
  22.   
  23.     public PictureAdapter(Context context, List<GalleryModel> list) {  
  24.         super();  
  25.         this.context = context;  
  26.         this.list = list;  
  27.     }  
  28.   
  29.     @Override  
  30.     public int getCount() {  
  31.         if (list != null) {  
  32.             return list.size();  
  33.         }  
  34.         return 0;  
  35.     }  
  36.   
  37.     @Override  
  38.     public Object getItem(int position) {  
  39.         if (list != null) {  
  40.             return list.get(position);  
  41.         }  
  42.         return null;  
  43.     }  
  44.   
  45.     @Override  
  46.     public long getItemId(int position) {  
  47.         return position;  
  48.     }  
  49.   
  50.     public void setSelectItem(int selectItem) {  
  51.   
  52.         if (this.selectItem != selectItem) {  
  53.             this.selectItem = selectItem;  
  54.             notifyDataSetChanged();  
  55.         }  
  56.     }  
  57.   
  58.     @Override  
  59.     public View getView(int position, View convertView, ViewGroup parent) {  
  60.   
  61.         ViewHold hold;  
  62.   
  63.         if (convertView == null) {  
  64.             hold = new ViewHold();  
  65.             convertView = LayoutInflater.from(context).inflate(R.layout.gallery_item, null);  
  66.             convertView.setTag(hold);  
  67.         } else {  
  68.             hold = (ViewHold) convertView.getTag();  
  69.         }  
  70.         hold.mImageView = (ImageView) convertView.findViewById(R.id.imageview);  
  71.         hold.mTextView = (TextView) convertView.findViewById(R.id.text);  
  72.   
  73.         hold.mImageView.setImageResource(list.get(position).getImageView());  
  74.         hold.mTextView.setText(list.get(position).getText());  
  75.         if (selectItem == position) {  
  76.             hold.mImageView.setLayoutParams(new LinearLayout.LayoutParams(150, 180));  
  77.             hold.mTextView.setTextSize(20);  
  78.             hold.mTextView.setFocusable(true);  
  79.         } else {  
  80.             hold.mImageView.setLayoutParams(new LinearLayout.LayoutParams(120, 150));  
  81.             hold.mTextView.setTextSize(20);  
  82.             hold.mTextView.setFocusable(false);  
  83.         }   
  84.         return convertView;  
  85.     }  
  86.   
  87.     static class ViewHold {  
  88.         public TextView mTextView;  
  89.         private ImageView mImageView;  
  90.     }  
  91.   
  92. }  


适配器的布局:

 

 

[html] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"   
  6.     android:gravity="center">  
  7.       
  8.     <ImageView   
  9.         android:id="@+id/imageview"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:src="@drawable/one_select"  
  13.         android:focusableInTouchMode="true"  
  14.         android:focusable="true"/>  
  15.       
  16.     <TextView   
  17.         android:id="@+id/text"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:textColor="@drawable/bg_text_color"  
  21.         android:text="aa"  
  22.         android:textSize="16dp"/>  
  23.   
  24. </LinearLayout>  


Gallery在acitivity的使用

 

 

[java] view plain copy
 
  1. package com.sdufe.thea.guo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.sdufe.thea.guo.adapter.PictureAdapter;  
  7. import com.sdufe.thea.guo.model.GalleryModel;  
  8.   
  9. import android.os.Bundle;  
  10. import android.annotation.SuppressLint;  
  11. import android.app.Activity;  
  12. import android.graphics.Color;  
  13. import android.view.Menu;  
  14. import android.view.View;  
  15. import android.view.animation.Animation;  
  16. import android.view.animation.ScaleAnimation;  
  17. import android.widget.AdapterView;  
  18. import android.widget.AdapterView.OnItemClickListener;  
  19. import android.widget.AdapterView.OnItemSelectedListener;  
  20. import android.widget.Gallery;  
  21.   
  22. public class MainActivity extends Activity {  
  23.   
  24.     private Gallery gallery;  
  25.     private PictureAdapter adapter;  
  26.     private List<GalleryModel> list;  
  27.   
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.activity_main);  
  32.   
  33.         initData();  
  34.         initView();  
  35.     }  
  36.   
  37.     private void initView() {  
  38.         gallery = (Gallery) findViewById(R.id.gallery);  
  39.         adapter = new PictureAdapter(this, list);  
  40.   
  41.         gallery.setAdapter(adapter);  
  42.   
  43.   
  44.         gallery.setOnItemSelectedListener(new OnItemSelectedListener() {  
  45.   
  46.             @Override  
  47.             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
  48.                 adapter.setSelectItem(position);  
  49.                 // ScaleAnimation animation = new ScaleAnimation(1, 1.5f, 1,  
  50.                 // 1.5f,  
  51.                 // Animation.RELATIVE_TO_SELF, 0.5f,  
  52.                 // Animation.RELATIVE_TO_SELF, 0.5f);  
  53.                 // animation.setDuration(2000);  
  54.                 // view.startAnimation(animation);  
  55.             }  
  56.   
  57.             @Override  
  58.             public void onNothingSelected(AdapterView<?> parent) {  
  59.             }  
  60.         });  
  61.     }  
  62.   
  63.     private void initData() {  
  64.         list = new ArrayList<GalleryModel>();  
  65.   
  66.         list.add(new GalleryModel(R.drawable.one_select, "房产交易"));  
  67.         list.add(new GalleryModel(R.drawable.two_select, "附近洗车店"));  
  68.         list.add(new GalleryModel(R.drawable.three_select, "社区商城"));  
  69.     }  
  70.   
  71. }  


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

 

 

ok,结束

 

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

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

posted @ 2016-11-24 19:43  天涯海角路  阅读(94)  评论(0)    收藏  举报