demo1233


public class DepthPageTransformer implements ViewPager.PageTransformer {
    private static final float MIN_SCALE = 0.75f;

    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();
        if (position < -1)  //[-无穷 , -1)
        {
            // this page is way off_screen to the left
            view.setAlpha(0);
        } else if (position <= 0) //[-1 , 0]
        {
            //Use the default slide transition when moving to the left page
            view.setAlpha(1);
            view.setTranslationX(0);
            view.setScaleX(1);
            view.setScaleY(1);
        } else if (position <= 1) //(0 , 1]
        {
            //Fade the page out
            view.setAlpha(1 - position);

            //Counteract the default slide transition
            view.setTranslationX(pageWidth * -position);

            //scale the page down(between MIN_SCALE and 1)
            float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);
        } else {
            // this page is way off_screen to the right
            view.setAlpha(0);
        }
    }
}
public class FixedSpeedScroller extends Scroller {
    private int mDuration = 1000;

    public FixedSpeedScroller(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public FixedSpeedScroller(Context context, Interpolator interpolator) {
        super(context, interpolator);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        // Ignore received duration, use fixed one instead
        super.startScroll(startX, startY, dx, dy, mDuration);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy) {
        // Ignore received duration, use fixed one instead
        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) {
            //对ViewPager页号求模取出View列表中要显示的项
            position %= mListImgView.size();
            if (position < 0) {
                position = mListImgView.size() + position;
            }
            final ImageView view = mListImgView.get(position);
            //如果View已经在之前添加到了一个父组件,则必须先remove,否则会抛出IllegalStateException。
            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 = 0f;//文本长度
    private float viewWidth = 0f;
    private float step = 0f;//文字的横坐标
    private float y = 0f;//文字的纵坐标
    private float temp_view_plus_text_length = 0.0f;//用于计算的临时变量
    private float temp_view_plus_two_text_length = 0.0f;//用于计算的临时变量
    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.0f;
        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;//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.v7.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.v7.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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值