标签:
看下最后的效果图:
为了增加一定的趣味,做了一个类似上面的相册效果,支持拖动时自动变化,和点击变化~~是不是很赞~
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:background="@android:color/white"
-
android:orientation="vertical" >
-
-
<FrameLayout
-
android:layout_width="fill_parent"
-
android:layout_height="0dp"
-
android:layout_weight="1" >
-
-
<ImageView
-
android:id="@+id/id_content"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:layout_gravity="center"
-
android:layout_margin="10dp"
-
android:scaleType="centerCrop"
-
android:src="@drawable/ic_launcher" />
-
</FrameLayout>
-
-
<com.example.zhy_horizontalscrollview.MyHorizontalScrollView
-
android:id="@+id/id_horizontalScrollView"
-
android:layout_width="wrap_content"
-
android:layout_height="150dp"
-
android:layout_gravity="bottom"
-
android:background="@android:color/white"
-
android:scrollbars="none" >
-
-
<LinearLayout
-
android:id="@+id/id_gallery"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_gravity="center_vertical"
-
android:orientation="horizontal" >
-
</LinearLayout>
-
</com.example.zhy_horizontalscrollview.MyHorizontalScrollView>
-
-
</LinearLayout>
没任何变化,除了把类名改成了我们自定义的类~
2、为了和国际接轨,我们也搞个Adapter,类似BaseAdapter
-
package com.example.zhy_horizontalscrollview;
-
-
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.TextView;
-
-
public class HorizontalScrollViewAdapter
-
{
-
-
private Context mContext;
-
private LayoutInflater mInflater;
-
private List<Integer> mDatas;
-
-
public HorizontalScrollViewAdapter(Context context, List<Integer> mDatas)
-
{
-
this.mContext = context;
-
mInflater = LayoutInflater.from(context);
-
this.mDatas = mDatas;
-
}
-
-
public int getCount()
-
{
-
return mDatas.size();
-
}
-
-
public Object getItem(int position)
-
{
-
return mDatas.get(position);
-
}
-
-
public long getItemId(int position)
-
{
-
return position;
-
}
-
-
public View getView(int position, View convertView, ViewGroup parent)
-
{
-
ViewHolder viewHolder = null;
-
if (convertView == null)
-
{
-
viewHolder = new ViewHolder();
-
convertView = mInflater.inflate(
-
R.layout.activity_index_gallery_item, parent, false);
-
viewHolder.mImg = (ImageView) convertView
-
.findViewById(R.id.id_index_gallery_item_image);
-
viewHolder.mText = (TextView) convertView
-
.findViewById(R.id.id_index_gallery_item_text);
-
-
convertView.setTag(viewHolder);
-
} else
-
{
-
viewHolder = (ViewHolder) convertView.getTag();
-
}
-
viewHolder.mImg.setImageResource(mDatas.get(position));
-
viewHolder.mText.setText("some info ");
-
-
return convertView;
-
}
-
-
private class ViewHolder
-
{
-
ImageView mImg;
-
TextView mText;
-
}
-
-
}
3、下面先看用法:
-
package com.example.zhy_horizontalscrollview;
-
-
import java.util.ArrayList;
-
import java.util.Arrays;
-
import java.util.List;
-
-
import android.app.Activity;
-
import android.graphics.Color;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.Window;
-
import android.widget.ImageView;
-
-
import com.example.zhy_horizontalscrollview.MyHorizontalScrollView.CurrentImageChangeListener;
-
import com.example.zhy_horizontalscrollview.MyHorizontalScrollView.OnItemClickListener;
-
-
public class MainActivity extends Activity
-
{
-
-
private MyHorizontalScrollView mHorizontalScrollView;
-
private HorizontalScrollViewAdapter mAdapter;
-
private ImageView mImg;
-
private List<Integer> mDatas = new ArrayList<Integer>(Arrays.asList(
-
R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
-
R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h,
-
R.drawable.l));
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
requestWindowFeature(Window.FEATURE_NO_TITLE);
-
setContentView(R.layout.activity_main);
-
-
mImg = (ImageView) findViewById(R.id.id_content);
-
-
mHorizontalScrollView = (MyHorizontalScrollView) findViewById(R.id.id_horizontalScrollView);
-
mAdapter = new HorizontalScrollViewAdapter(this, mDatas);
-
-
mHorizontalScrollView
-
.setCurrentImageChangeListener(new CurrentImageChangeListener()
-
{
-
@Override
-
public void onCurrentImgChanged(int position,
-
View viewIndicator)
-
{
-
mImg.setImageResource(mDatas.get(position));
-
viewIndicator.setBackgroundColor(Color
-
.parseColor("#AA024DA4"));
-
}
-
});
-
-
mHorizontalScrollView.setOnItemClickListener(new OnItemClickListener()
-
{
-
-
@Override
-
public void onClick(View view, int position)
-
{
-
mImg.setImageResource(mDatas.get(position));
-
view.setBackgroundColor(Color.parseColor("#AA024DA4"));
-
}
-
});
-
-
mHorizontalScrollView.initDatas(mAdapter);
-
}
-
-
}
用起来是不是有点像ListView,初始化数据适配器,然后设置数据适配器,然后就是设置各种回调~~
如果仅仅是一堆图片展示,类似商品切换,更见简单,就不需要设置滚动监听和点击监听了~
4、最后看自定义的MyHorizontalScrollView类
-
package com.example.zhy_horizontalscrollview;
-
-
import java.util.HashMap;
-
import java.util.Map;
-
-
import android.content.Context;
-
import android.graphics.Color;
-
import android.util.AttributeSet;
-
import android.util.DisplayMetrics;
-
import android.util.Log;
-
import android.view.MotionEvent;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.view.WindowManager;
-
import android.widget.HorizontalScrollView;
-
import android.widget.LinearLayout;
-
-
public class MyHorizontalScrollView extends HorizontalScrollView implements
-
OnClickListener
-
{
-
-
-
-
-
-
-
-
public interface CurrentImageChangeListener
-
{
-
void onCurrentImgChanged(int position, View viewIndicator);
-
}
-
-
-
-
-
-
-
-
public interface OnItemClickListener
-
{
-
void onClick(View view, int pos);
-
}
-
-
private CurrentImageChangeListener mListener;
-
-
private OnItemClickListener mOnClickListener;
-
-
private static final String TAG = "MyHorizontalScrollView";
-
-
-
-
-
private LinearLayout mContainer;
-
-
-
-
-
private int mChildWidth;
-
-
-
-
private int mChildHeight;
-
-
-
-
private int mCurrentIndex;
-
-
-
-
private int mFristIndex;
-
-
-
-
private View mFirstView;
-
-
-
-
private HorizontalScrollViewAdapter mAdapter;
-
-
-
-
private int mCountOneScreen;
-
-
-
-
private int mScreenWitdh;
-
-
-
-
-
-
private Map<View, Integer> mViewPos = new HashMap<View, Integer>();
-
-
public MyHorizontalScrollView(Context context, AttributeSet attrs)
-
{
-
super(context, attrs);
-
-
WindowManager wm = (WindowManager) context
-
.getSystemService(Context.WINDOW_SERVICE);
-
DisplayMetrics outMetrics = new DisplayMetrics();
-
wm.getDefaultDisplay().getMetrics(outMetrics);
-
mScreenWitdh = outMetrics.widthPixels;
-
}
-
-
@Override
-
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
-
{
-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
-
mContainer = (LinearLayout) getChildAt(0);
-
}
-
-
-
-
-
protected void loadNextImg()
-
{
-
-
if (mCurrentIndex == mAdapter.getCount() - 1)
-
{
-
return;
-
}
-
-
scrollTo(0, 0);
-
mViewPos.remove(mContainer.getChildAt(0));
-
mContainer.removeViewAt(0);
-
-
-
View view = mAdapter.getView(++mCurrentIndex, null, mContainer);
-
view.setOnClickListener(this);
-
mContainer.addView(view);
-
mViewPos.put(view, mCurrentIndex);
-
-
-
mFristIndex++;
-
-
if (mListener != null)
-
{
-
notifyCurrentImgChanged();
-
}
-
-
}
-
-
-
-
protected void loadPreImg()
-
{
-
-
if (mFristIndex == 0)
-
return;
-
-
int index = mCurrentIndex - mCountOneScreen;
-
if (index >= 0)
-
{
-
-
-
int oldViewPos = mContainer.getChildCount() - 1;
-
mViewPos.remove(mContainer.getChildAt(oldViewPos));
-
mContainer.removeViewAt(oldViewPos);
-
-
-
View view = mAdapter.getView(index, null, mContainer);
-
mViewPos.put(view, index);
-
mContainer.addView(view, 0);
-
view.setOnClickListener(this);
-
-
scrollTo(mChildWidth, 0);
-
-
mCurrentIndex--;
-
mFristIndex--;
-
-
if (mListener != null)
-
{
-
notifyCurrentImgChanged();
-
-
}
-
}
-
}
-
-
-
-
-
public void notifyCurrentImgChanged()
-
{
-
-
for (int i = 0; i < mContainer.getChildCount(); i++)
-
{
-
mContainer.getChildAt(i).setBackgroundColor(Color.WHITE);
-
}
-
-
mListener.onCurrentImgChanged(mFristIndex, mContainer.getChildAt(0));
-
-
}
-
-
-
-
-
-
-
public void initDatas(HorizontalScrollViewAdapter mAdapter)
-
{
-
this.mAdapter = mAdapter;
-
mContainer = (LinearLayout) getChildAt(0);
-
-
final View view = mAdapter.getView(0, null, mContainer);
-
mContainer.addView(view);
-
-
-
if (mChildWidth == 0 && mChildHeight == 0)
-
{
-
int w = View.MeasureSpec.makeMeasureSpec(0,
-
View.MeasureSpec.UNSPECIFIED);
-
int h = View.MeasureSpec.makeMeasureSpec(0,
-
View.MeasureSpec.UNSPECIFIED);
-
view.measure(w, h);
-
mChildHeight = view.getMeasuredHeight();
-
mChildWidth = view.getMeasuredWidth();
-
Log.e(TAG, view.getMeasuredWidth() + "," + view.getMeasuredHeight());
-
mChildHeight = view.getMeasuredHeight();
-
-
mCountOneScreen = mScreenWitdh / mChildWidth+2;
-
-
Log.e(TAG, "mCountOneScreen = " + mCountOneScreen
-
+ " ,mChildWidth = " + mChildWidth);
-
-
-
}
-
-
initFirstScreenChildren(mCountOneScreen);
-
}
-
-
-
-
-
-
-
public void initFirstScreenChildren(int mCountOneScreen)
-
{
-
mContainer = (LinearLayout) getChildAt(0);
-
mContainer.removeAllViews();
-
mViewPos.clear();
-
-
for (int i = 0; i < mCountOneScreen; i++)
-
{
-
View view = mAdapter.getView(i, null, mContainer);
-
view.setOnClickListener(this);
-
mContainer.addView(view);
-
mViewPos.put(view, i);
-
mCurrentIndex = i;
-
}
-
-
if (mListener != null)
-
{
-
notifyCurrentImgChanged();
-
}
-
-
}
-
-
@Override
-
public boolean onTouchEvent(MotionEvent ev)
-
{
-
switch (ev.getAction())
-
{
-
case MotionEvent.ACTION_MOVE:
-
-
-
int scrollX = getScrollX();
-
-
if (scrollX >= mChildWidth)
-
{
-
loadNextImg();
-
}
-
-
if (scrollX == 0)
-
{
-
loadPreImg();
-
}
-
break;
-
}
-
return super.onTouchEvent(ev);
-
}
-
-
@Override
-
public void onClick(View v)
-
{
-
if (mOnClickListener != null)
-
{
-
for (int i = 0; i < mContainer.getChildCount(); i++)
-
{
-
mContainer.getChildAt(i).setBackgroundColor(Color.WHITE);
-
}
-
mOnClickListener.onClick(v, mViewPos.get(v));
-
}
-
}
-
-
public void setOnItemClickListener(OnItemClickListener mOnClickListener)
-
{
-
this.mOnClickListener = mOnClickListener;
-
}
-
-
public void setCurrentImageChangeListener(
-
CurrentImageChangeListener mListener)
-
{
-
this.mListener = mListener;
-
}
-
-
}
首先,加载第一个Item,根据item的宽计算当前屏幕可以加载多少张图片,然后初始化第一屏的图片,接下来就是从写onTouchEvent,在其中监听用户的ACTION_MOVE,然后根据移动的距离加载前一张或者后一张,同时动态移除不可见的View,回收内存~~~~
代码中有个Map专门存储View和posion的,主要是为了给点击回调提供当前的View的位置,有点类似:Android
自定义 ViewPager 打造千变万化的图片切换效果里面的Map的巧妙用法~~
是不是完全实现了ViewPager和HorizontalScrollView的合体~~~HorizontalScrollView的效果,ViewPager的特性~~~~
最后贴一下旋转屏幕后的效果图:
可以看出,不仅是做相册,还是图片轮播想过都是刚刚的!
Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
标签:
原文地址:http://blog.csdn.net/qq_32521313/article/details/50978366