public class DepthPageTransformer implements ViewPager .PageTransformer {
private static final float MIN_SCALE = 0.75 f;
public void transformPage (View view, float position) {
int pageWidth = view.getWidth();
if (position < -1 )
{
view.setAlpha(0 );
} else if (position <= 0 )
{
view.setAlpha(1 );
view.setTranslationX(0 );
view.setScaleX(1 );
view.setScaleY(1 );
} else if (position <= 1 )
{
view.setAlpha(1 - position);
view.setTranslationX(pageWidth * -position);
float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else {
view.setAlpha(0 );
}
}
}
public class FixedSpeedScroller extends Scroller {
private int mDuration = 1000 ;
public FixedSpeedScroller (Context context) {
super (context);
}
public FixedSpeedScroller (Context context, Interpolator interpolator) {
super (context, interpolator);
}
@Override
public void startScroll (int startX, int startY, int dx, int dy, int duration) {
super .startScroll(startX, startY, dx, dy, mDuration);
}
@Override
public void startScroll (int startX, int startY, int dx, int dy) {
super .startScroll(startX, startY, dx, dy, mDuration);
}
public void setmDuration (int time) {
mDuration = time;
}
public int getmDuration () {
return mDuration;
}
}
public class SliderImageLayout extends RelativeLayout implements ViewPager .OnPageChangeListener {
private LayoutInflater mInflater;
private Context mContext;
private ViewPager mViewPager;
private LinearLayout mDotLayout;
private TextView mTitleTV;
private FixedSpeedScroller mScroller;
private OnSliderItemClickListener mOnSliderItemClickListener;
private int imgSize = 0 ;
private List<ImageView> mListImgView;
private List<String> titleTexts;
private int previousPosition = 0 ;
private Handler handler = new Handler() {
@Override
public void handleMessage (Message msg) {
if (msg.what == 0x112 ) {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1 );
} else if (msg.what == 0x123 ) {
Bitmap bitmap = (Bitmap) msg.obj;
if (bitmap != null ) {
ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(bitmap);
mListImgView.add(imageView);
if (mListImgView.size() == imgSize) {
setViewPagerAdapter();
}
}
}
}
};
public SliderImageLayout (Context context) {
this (context, null );
}
public SliderImageLayout (Context context, AttributeSet attrs) {
this (context, attrs, 0 );
}
public SliderImageLayout (Context context, AttributeSet attrs, int defStyleAttr) {
super (context, attrs, defStyleAttr);
mContext = context;
initView();
}
private void initView () {
mListImgView = new ArrayList<>();
titleTexts = new ArrayList<>();
mInflater = LayoutInflater.from(mContext);
View view = mInflater.inflate(R.layout.view_slider_layout, this );
mDotLayout = (LinearLayout) view.findViewById(R.id.ll_dot_group);
mTitleTV = (TextView) view.findViewById(R.id.tv_img_desc);
mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
mViewPager.setPageTransformer(true , new DepthPageTransformer());
mViewPager.setOnPageChangeListener(this );
try {
Field mField = ViewPager.class.getDeclaredField("mScroller" );
mField.setAccessible(true );
mScroller = new FixedSpeedScroller(mContext, new AccelerateInterpolator());
mField.set(mViewPager, mScroller);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setSliderImg (final String imgUrl) {
imgSize += 1 ;
new Thread() {
@Override
public void run () {
InputStream in = null ;
try {
URL url = new URL(imgUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000 );
conn.setRequestMethod("GET" );
in = new BufferedInputStream(conn.getInputStream());
Bitmap bitmap = BitmapFactory.decodeStream(in);
Message msg = new Message();
msg.what = 0x123 ;
msg.obj = bitmap;
handler.sendMessage(msg);
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null ) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}.start();
}
public void setSliderImg (int imgId) {
imgSize += 1 ;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgId);
Message msg = new Message();
msg.what = 0x123 ;
msg.obj = bitmap;
handler.sendMessage(msg);
}
public void setTitleText (String titleText) {
if (!TextUtils.isEmpty(titleText)) {
titleTexts.add(titleText);
}
}
private void setViewPagerAdapter () {
initDotViews();
mViewPager.setAdapter(new ViewPagerAdapter());
mScroller.setmDuration(700 );
mViewPager.setCurrentItem(Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2 % mListImgView.size()));
new Timer().schedule(new TimerTask() {
@Override
public void run () {
handler.sendEmptyMessage(0x112 );
}
}, 0 , 4500 );
}
private void initDotViews () {
View dotView;
for (int i = 0 ; i < mListImgView.size(); i++) {
dotView = new View(mContext);
dotView.setBackgroundResource(R.drawable.selector_dot);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(10 , 10 );
if (i != 0 ) {
lp.leftMargin = 15 ;
}
dotView.setLayoutParams(lp);
dotView.setEnabled(true );
mDotLayout.addView(dotView);
}
}
@Override
public void onPageSelected (int position) {
int newPosition = position % mListImgView.size();
mDotLayout.getChildAt(newPosition).setEnabled(false );
if (titleTexts != null && titleTexts.size() > 0 ) {
mTitleTV.setVisibility(VISIBLE);
mTitleTV.setText(titleTexts.get(newPosition));
}
mDotLayout.getChildAt(previousPosition).setEnabled(true );
previousPosition = newPosition;
}
@Override
public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged (int state) {
}
private class ViewPagerAdapter extends PagerAdapter {
@Override
public int getCount () {
return Integer.MAX_VALUE;
}
@Override
public Object instantiateItem (ViewGroup container, int position) {
position %= mListImgView.size();
if (position < 0 ) {
position = mListImgView.size() + position;
}
final ImageView view = mListImgView.get(position);
ViewGroup vp = (ViewGroup) view.getParent();
if (vp != null ) {
vp.removeView(view);
}
container.addView(view);
final int newPosition = position;
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick (View v) {
if (mOnSliderItemClickListener != null ) {
mOnSliderItemClickListener.onSliderItemClick(newPosition);
}
}
});
return view;
}
@Override
public void destroyItem (ViewGroup container, int position, Object object) {
}
@Override
public boolean isViewFromObject (View view, Object object) {
return view == object;
}
}
public void setOnSliderItemClickListener (OnSliderItemClickListener onSliderItemClickListener) {
mOnSliderItemClickListener = onSliderItemClickListener;
}
public interface OnSliderItemClickListener {
void onSliderItemClick(int position);
}
}
public class AutoScrollTextView extends TextView implements View .OnClickListener
{
public final static String TAG = AutoScrollTextView.class.getSimpleName();
private float textLength = 0 f;
private float viewWidth = 0 f;
private float step = 0 f;
private float y = 0 f;
private float temp_view_plus_text_length = 0.0 f;
private float temp_view_plus_two_text_length = 0.0 f;
public boolean isStarting = false ;
private Paint paint = null ;
private String text = "" ;
public AutoScrollTextView (Context context) {
super (context);
initView();
}
public AutoScrollTextView (Context context, AttributeSet attrs) {
super (context, attrs);
initView();
}
public AutoScrollTextView (Context context, AttributeSet attrs, int defStyle) {
super (context, attrs, defStyle);
initView();
}
private void initView ()
{
setOnClickListener(this );
}
public void init (WindowManager windowManager)
{
paint = getPaint();
text = getText().toString();
textLength = paint.measureText(text);
viewWidth = getWidth();
if (viewWidth == 0 )
{
if (windowManager != null )
{
Display display = windowManager.getDefaultDisplay();
viewWidth = display.getWidth();
}
}
step = textLength;
temp_view_plus_text_length = viewWidth + textLength;
temp_view_plus_two_text_length = viewWidth + textLength * 2 ;
y = getTextSize() + getPaddingTop();
}
@Override
public Parcelable onSaveInstanceState ()
{
Parcelable superState = super .onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.step = step;
ss.isStarting = isStarting;
return ss;
}
@Override
public void onRestoreInstanceState (Parcelable state)
{
if (!(state instanceof SavedState)) {
super .onRestoreInstanceState(state);
return ;
}
SavedState ss = (SavedState)state;
super .onRestoreInstanceState(ss.getSuperState());
step = ss.step;
isStarting = ss.isStarting;
}
public static class SavedState extends BaseSavedState {
public boolean isStarting = false ;
public float step = 0.0 f;
SavedState(Parcelable superState) {
super (superState);
}
@Override
public void writeToParcel (Parcel out, int flags) {
super .writeToParcel(out, flags);
out.writeBooleanArray(new boolean []{isStarting});
out.writeFloat(step);
}
public static final Parcelable.Creator<SavedState> CREATOR
= new Parcelable.Creator<SavedState>() {
public SavedState[] newArray (int size) {
return new SavedState[size];
}
@Override
public SavedState createFromParcel (Parcel in) {
return new SavedState(in);
}
};
private SavedState (Parcel in) {
super (in);
boolean [] b = null ;
in.readBooleanArray(b);
if (b != null && b.length > 0 )
isStarting = b[0 ];
step = in.readFloat();
}
}
public void startScroll ()
{
isStarting = true ;
invalidate();
}
public void stopScroll ()
{
isStarting = false ;
invalidate();
}
@Override
protected void onDraw (Canvas canvas) {
super .onDraw(canvas);
canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
if (!isStarting)
{
return ;
}
step += 0.5 ;
if (step > temp_view_plus_two_text_length)
step = textLength;
invalidate();
}
@Override
public void onClick (View v) {
if (isStarting)
stopScroll();
else
startScroll();
}
}
public class CateAdapter extends RecyclerView.Adapter<CateAdapter.CateViewHolder>
{
private LayoutInflater mLayoutInflater;
private List<CategoryEntity> mCateDatas;
private Context mContext;
private OnItemClickListener mOnItemClickListener;
public interface OnItemClickListener
{
void onItemClick(View view , int position);
}
public void setOnItemClickListener (OnItemClickListener onItemClickListener)
{
this .mOnItemClickListener = onItemClickListener;
}
public CateAdapter (Context context , List<CategoryEntity> data)
{
this .mContext = context;
this .mCateDatas = data;
mLayoutInflater = LayoutInflater.from (context);
}
@Override
public CateViewHolder onCreateViewHolder (ViewGroup parent, int viewType)
{
View view = mLayoutInflater.inflate(R.layout.item_cate_layout , parent , false );
CateViewHolder cateViewHolder = new CateViewHolder(view);
return cateViewHolder;
}
@Override
public void onBindViewHolder (CateViewHolder holder, int position) {
holder.mCateName.setText(mCateDatas.get (position).getCateName());
holder.mCateImg.setImageResource(mCateDatas.get (position).getPicId());
setOnClickListener(holder);
}
private void setOnClickListener (final CateViewHolder holder)
{
if (mOnItemClickListener != null )
{
holder.itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View v)
{
int layoutPosition = holder.getLayoutPosition();
mOnItemClickListener.onItemClick(holder.itemView , layoutPosition);
}
});
}
}
@Override
public int getItemCount () {
return mCateDatas.size();
}
class CateViewHolder extends RecyclerView.ViewHolder
{
TextView mCateName;
ImageView mCateImg;
public CateViewHolder (View itemView)
{
super(itemView);
mCateName = (TextView) itemView.findViewById(R.id.cate_name);
mCateImg = (ImageView) itemView.findViewById(R.id.cate_img);
}
}
}
<shape xmlns:android ="http://schemas.android.com/apk/res/android"
android:shape ="rectangle" >
<corners android:radius ="5dp" />
<solid android:color ="@android:color/darker_gray" />
</shape >
<RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:padding ="10dp" >
<LinearLayout
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_centerInParent ="true"
android:orientation ="vertical" >
<ImageView
android:id ="@+id/cate_img"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_gravity ="center_horizontal"
android:src ="@drawable/icon_favorite" />
<TextView
android:id ="@+id/cate_name"
android:layout_marginTop ="5dp"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_below ="@+id/cate_img"
android:textColor ="@android:color/black"
android:layout_gravity ="center_horizontal"
android:textSize ="12sp"
android:text ="@string/test_test" />
</LinearLayout >
</RelativeLayout >
<RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="match_parent"
android:layout_height ="wrap_content" >
<android.support.v4.view.ViewPager
android:id ="@+id/viewPager"
android:layout_width ="match_parent"
android:layout_height ="190dp" />
<LinearLayout
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:layout_alignBottom ="@+id/viewPager"
android:background ="#55000000"
android:gravity ="center_horizontal"
android:orientation ="vertical"
android:padding ="3dp" >
<TextView
android:id ="@+id/tv_img_desc"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="这里是广告标题"
android:textColor ="#ffffff"
android:textSize ="13sp"
android:visibility ="gone" />
<LinearLayout
android:id ="@+id/ll_dot_group"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:orientation ="horizontal" />
</LinearLayout >
</RelativeLayout >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<wcb.com .mytestdemo .SliderImageLayout
android:id="@+id/slider_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/icon_search"
android:drawablePadding="6dp"
android:background="@drawable/search_item_style"
android:padding="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:hint="@string/search_text" />
<android.support .v 7.widget .RecyclerView
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:paddingLeft="10dp"
android:layout_height="wrap_content"
android:id="@+id/cate_list_view"
android:layout_below="@+id/slider_layout" >
</android.support .v 7.widget .RecyclerView >
<wcb.com .mytestdemo .AutoScrollTextView
android:id="@+id/auto_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:inputType="text"
android:background="#EEE"
android:textSize="16sp"
android:layout_below="@+id/cate_list_view" />
</RelativeLayout>
</ScrollView>
</LinearLayout>