带有Viewpager并缩放的Gridview画廊

时间:2018-11-21 09:42:23

标签: android gridview android-viewpager zooming

我正在开发一个简单的Gallery应用程序。 我有2个问题:

第一个问题

当我触摸GridView中的特定图像以全屏活动(viewpager活动)显示时,它总是显示GridView画廊中的第一张图像!不是我碰过的那个!!!

但是ViewPager正常工作。

第二个问题

我想放大ViewPager活动

我尝试了一个库(chrisbanes:phtoview),但是ViewPager遇到了一些问题

摘要

我希望我的图库应用正常运行。 GridView图片库(您可以滑动和缩放图像)

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ViewPager slider;
    private SliderAdapter sliderAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        slider=findViewById(R.id.slider);
        sliderAdapter=new SliderAdapter(this);
        slider.setAdapter(sliderAdapter);
    }

    private class SliderAdapter extends PagerAdapter{

        private Context context;
        private LayoutInflater inflater;

        public SliderAdapter(Context context) {
            this.context = context;
        }

        int[] list_img={R.drawable.ferari,R.drawable.ferrari,R.drawable.ferrarilogoo,R.drawable.yellowferrariiii,R.drawable.models,R.drawable.applogo,R.drawable.carfront,R.drawable.california,R.drawable.laferrari

        };

        @Override
        public int getCount() {
            return list_img.length;
        }

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

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, final int position) {
            inflater=(LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
            View view=inflater.inflate(R.layout.item_slider,container,false);
            ImageView imageView=view.findViewById(R.id.img_item);
            imageView.setImageResource(list_img[position]);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(context,Fullscreen.class);
                    intent.putExtra("Ferrari",position);
                    startActivity(intent);
                }
            });
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((ImageView)object);
        }
    }


}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(230, 250));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(3, 3, 3, 3);

        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    public Integer[] mThumbIds = {
            R.drawable.ferari, R.drawable.models,
            R.drawable.ferrarihd,
            R.drawable.ferrari,
            R.drawable.yellowferrariiii,
            R.drawable.models,
            R.drawable.home,
            R.drawable.carfront,
            R.drawable.villa,
            R.drawable.derrarii,
            R.drawable.laferrari,
            R.drawable.realestate,
            R.drawable.carfront,R.drawable.california,R.drawable.laferrari

    };






}

FullscreenviewActivity.java

public class FullScreenViewActivity extends AppCompatActivity {

    private ViewPager slider;
    private SliderAdapter sliderAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_screen_view);

        slider=findViewById(R.id.slider);
        sliderAdapter=new SliderAdapter(this);
        slider.setAdapter(sliderAdapter);

        slider.setPageTransformer(false, new ViewPager.PageTransformer() {
            @Override
            public void transformPage(View page, float position) {
                // do transformation here
                final float normalizedposition = Math.abs(Math.abs(position) - 1);
                page.setScaleX(normalizedposition / 2 + 0.5f);
                page.setScaleY(normalizedposition / 2 + 0.5f);
                //or
                // page.setRotationY(position * -30);
            }
        });



        // get intent data
        Intent i = getIntent();



        // Selected image id
        int position = i.getExtras().getInt("id");
        ImageAdapter gridViewAdapter = new ImageAdapter(this);
        gridViewAdapter.getItemId(position);






    }

    private class SliderAdapter extends PagerAdapter {

        private Context context;
        private LayoutInflater inflater;

        public SliderAdapter(Context context) {
            this.context = context;
        }



        int[] mThumbIds={   R.drawable.ferari, R.drawable.models,
                R.drawable.ferrarihd,
                R.drawable.ferrari,
                R.drawable.yellowferrariiii,
                R.drawable.models,
                R.drawable.home,
                R.drawable.carfront,
                R.drawable.villa,
                R.drawable.derrarii,
                R.drawable.laferrari,
                R.drawable.realestate,
                R.drawable.carfront,R.drawable.california,R.drawable.laferrari

        };





        @Override
        public CharSequence getPageTitle (int position) {
            return "Ferrari Cars";
        }

        @Override
        public int getCount() {
            return mThumbIds.length;
        }

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

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, final int position) {
            inflater=(LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
            View view=inflater.inflate(R.layout.item_slider,container,false);
            ImageView imageView=view.findViewById(R.id.img_item);
            imageView.setImageResource(mThumbIds[position]);


            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((ImageView)object);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您在FullScreenActivity中拥有图片的位置,因此只需做一件事即可滚动该图片

slider.setCurrentItem(pos);

之后

int position = i.getExtras().getInt("id");

并使用TouchImageView for zoom