美文网首页实用Android
AndroidAPP实现查看大图

AndroidAPP实现查看大图

作者: 不略 | 来源:发表于2022-08-28 14:55 被阅读0次

    1.先看效果图

    c5a25bcf192c76497000c4151693d11.jpg

    2.点击事件传值

    //查看大图
    Intent intent = new Intent(ShopQualiFauthActivity.this, ImagePreviewActivity.class);
    //imageList图片的集合
    intent.putExtra("images", imageList);
    intent.putExtra("position", finalI);
    startActivity(intent);
    

    3.ImagePreviewActivity

    public class ImagePreviewActivity extends BaseActivity {
        private ActivityImagePreviewBinding view;
        private PhotoViewPager vp;
        private ArrayList<String> images;
    
        @Override
        protected View getLayout() {
            view = ActivityImagePreviewBinding.inflate(getLayoutInflater());
            return view.getRoot();
        }
    
        @Override
        protected void initView() {
            View statusbar = findViewById(R.id.statusbar);
            setStatusBarColorAndTextColor(statusbar, getResources().getColor(R.color.black), true);
            vp = view.vp;
            view.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
    
            view.ibtnClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
    
        }
    
        @Override
        protected void initData() {
            int position = getIntent().getIntExtra("position", 0);
            images = getIntent().getStringArrayListExtra("images");
            view.tvNum.setText(position+1+"/"+ images.size());
            LogUtils.d("TAG","images--------------------->"+images);
            vp.setAdapter(new ImagePagerAdapter(this, images));
            vp.setCurrentItem(position,false);
            vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                }
    
                @Override
                public void onPageSelected(int position) {
                    view.tvNum.setText(position+1+"/"+ images.size());
                }
    
                @Override
                public void onPageScrollStateChanged(int state) {
    
                }
            });
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        android:orientation="vertical">
    
        <com.fengyongle.app.view.PhotoViewPager
            android:id="@+id/vp"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <com.fengyongle.app.view.StatusBarView
            android:id="@+id/statusbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_below="@+id/statusbar"
            android:layout_marginLeft="18dp"
            android:layout_marginTop="15dp"
            android:layout_marginRight="18dp"
            android:orientation="horizontal">
    
            <ImageButton
                android:id="@+id/ibtn_close"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="@null"
                android:src="@drawable/icon_close2" />
    
            <TextView
                android:id="@+id/tv_num"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_centerInParent="true"
                android:background="@drawable/shape_text"
                android:gravity="center"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:text="1/5"
                android:textColor="@color/white"
                android:textSize="17dp" />
        </RelativeLayout>
    </RelativeLayout>
    

    4.自定义PhotoViewPager

    public final class PhotoViewPager extends ViewPager {
    
        public PhotoViewPager(Context context) {
            super(context);
        }
    
        public PhotoViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            // 当 PhotoView 和 ViewPager 组合时 ,用双指进行放大时 是没有问题的,但是用双指进行缩小的时候,程序就会崩掉
            // 并且抛出java.lang.IllegalArgumentException: pointerIndex out of range
            try {
                return super.onInterceptTouchEvent(ev);
            } catch (IllegalArgumentException | ArrayIndexOutOfBoundsException ignored) {
                return false;
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:AndroidAPP实现查看大图

        本文链接:https://www.haomeiwen.com/subject/letcnrtx.html