当前位置: 主页 > 开源代码 >

ViewPage滑动加载大图和点击关注效果

分享

介绍:

ViewPage滑动加载大图和点击关注效果,viewpager滑动页面加载多张图片效果,
和点击关注按钮时候的状态变化效果。
本项目来源:https://github.com/SerhatSurguvec/Double-Tap-To-Like
大体实现代码:ViewPagerAdapter里面主要实现。

//ViewPager
public class ViewPagerAdapter extends PagerAdapter {
    // Declare Variables
    Context context;
    LayoutInflater inflater;
    int[] images;

    public ViewPagerAdapter(Context context, int[] images) {
        this.context = context;
        this.images = images;
    }

    @Override
    public int getCount() {
        return 5;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        ImageView imageview;
        final ImageView heartAnim;
        final ImageView likeImg;
        final TextView likeCount;
        TextView whichOfThem;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.viewpager_item, container,
                false);


        // Locate the ImageView in viewpager_item.xml
        imageview = (ImageView) itemView.findViewById(R.id.image);
        heartAnim = (ImageView) itemView.findViewById(R.id.heart_anim);
        likeImg = (ImageView) itemView.findViewById(R.id.item_comment_like_img);
        likeCount = (TextView) itemView.findViewById(R.id.item_comment_like_count);
        whichOfThem = (TextView) itemView.findViewById(R.id.which);

        whichOfThem.setText((position + 1) + "/" + 5);
        imageview.setImageResource(images[position]);

        final GestureDetector gd = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {

                Animation pulse_fade = AnimationUtils.loadAnimation(context, R.anim.pulse_fade_in);
                pulse_fade.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        heartAnim.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        heartAnim.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                heartAnim.startAnimation(pulse_fade);
                likeImg.setImageDrawable(context.getResources().getDrawable(R.drawable.like_active));
                likeCount.setText("3 Likes");
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                super.onLongPress(e);

            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
                return true;
            }
        });

        imageview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return gd.onTouchEvent(event);
            }
        });


        // Add viewpager_item.xml to ViewPager
        (container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);

    }
}


 

运行效果: