使用PhotoView实现图片查看

项目需要实现图片查看的功能,使用ImageView不能缩放、使用系统的图片查看器又不能符合自己的定制化需求,所以我就使用Viewpager + PhotoView 来实现了。


引入PhotoView

PhotoView 的Github链接,PhotoView旨在帮助开发者轻松实现Android ImageView的缩放。

1、配置仓库地址

在项目的build.gradle文件中加入以下代码

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

如果有多个url的话,可以这样写

allprojects {
    repositories {
        jcenter()
        maven { url 'https://esri.bintray.com/arcgis' }
        maven { url "https://jitpack.io" }
    }
}

2、引入第三方库

在你项目的APP Module的build.gradle文件中加上

compile 'com.github.chrisbanes:PhotoView:2.1.3'

3、控件使用

引入三方库之后就可以使用PhotoView控件了

<com.github.chrisbanes.photoview.PhotoView
    android:id="@+id/photo_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

图片查看器的实现

1、定义查看图片的TaskBigImgActivity.java

public class TaskBigImgActivity extends BaseActivity {

    @BindView(R.id.header_title)
    TextView headerTitle;
    @BindView(R.id.header_left_img)
    ImageView headerLeftImg;
    @BindView(R.id.big_img_vp)
    ViewPager bigImgVp;
    @BindView(R.id.header_right_tv)
    TextView headerRightTv;
    private int position;
    private ArrayList<String> paths;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_big_img);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        position = intent.getIntExtra("position", 0);
        paths = intent.getStringArrayListExtra("paths");
        String title = intent.getStringExtra("title");
        headerTitle.setText(title);
        headerLeftImg.setVisibility(View.VISIBLE);
        headerRightTv.setVisibility(View.VISIBLE);
        headerRightTv.setText(position + 1 + "/" + paths.size());
        initView();
    }

    private void initView() {
        bigImgVp.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return paths == null ? 0 : paths.size();
            }

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

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View adView = LayoutInflater.from(TaskBigImgActivity.this).inflate(R.layout.item_big_img, null);
                PhotoView icon = (PhotoView) adView.findViewById(R.id.flaw_img);
                icon.setBackgroundColor(getResources().getColor(R.color.colorBlack));
                Glide.with(TaskBigImgActivity.this)
                        .load(paths.get(position))
                        .into(icon);
                container.addView(adView);
                return adView;
            }

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

        bigImgVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                headerRightTv.setText(position + 1 + "/" + paths.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }

        });

        bigImgVp.setCurrentItem(position, true);
    }

    @OnClick(R.id.header_left_img)
    public void onClick() {
        finish();
    }
}

对应的布局文件activity_task_big_img.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.cnbs.cableinspection.user.activity.TaskBigImgActivity">

    <include layout="@layout/header" />

    <android.support.v4.view.ViewPager
        android:id="@+id/big_img_vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Adapter中使用的布局item_big_img.xml

<?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="wrap_content"
    android:background="@color/colorWhite"
    android:gravity="center">

    <com.github.chrisbanes.photoview.PhotoView
        android:id="@+id/flaw_img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

2、查看图片的调用

private ArrayList<String> recordPaths = new ArrayList<>(); //缺陷记录的图片集合
...
Intent imgIntent = new Intent(TaskRecordActivity.this, TaskBigImgActivity.class);
imgIntent.putStringArrayListExtra("paths",recordPaths);
imgIntent.putExtra("title","缺陷记录图片");
imgIntent.putExtra("position",msg.arg2);
startActivity(imgIntent);

position表示查看的第几张图片


到这里就可以自由的查看图片了,可以实现缩放、旋转等等变换。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 PhotoView实现涂鸦功能,你可以按照以下步骤进行操作: 1. 添加 PhotoView 依赖: 在你的项目的 `build.gradle` 文件中添加 PhotoView 依赖项: ```groovy dependencies { implementation 'com.github.chrisbanes:PhotoView:2.3.0' } ``` 确保使用的是最新版本的 PhotoView。 2. 在布局文件中添加 PhotoView: 在你的布局文件中添加 PhotoView 控件,并设置好相关属性,例如图片资源、缩放类型等: ```xml <com.github.chrisbanes.photoview.PhotoView android:id="@+id/photo_view" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@drawable/your_image" /> ``` 3. 添加涂鸦功能: 在代码中获取 PhotoView 对象,并设置触摸事件监听器,以实现涂鸦功能。你可以使用 Canvas 和 Paint 类来绘制路径。 ```java PhotoView photoView = findViewById(R.id.photo_view); photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() { @Override public void onPhotoTap(ImageView view, float x, float y) { // 获取触摸点坐标,并将其转换为图片上的坐标 Drawable drawable = photoView.getDrawable(); if (drawable != null) { int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight(); int viewWidth = photoView.getWidth(); int viewHeight = photoView.getHeight(); float imageX = (x * drawableWidth) / viewWidth; float imageY = (y * drawableHeight) / viewHeight; // 在图片上绘制涂鸦 Bitmap bitmap = Bitmap.createBitmap(drawableWidth, drawableHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth(5); canvas.drawPoint(imageX, imageY, paint); // 将修改后的图片显示在 PhotoViewphotoView.setImageBitmap(bitmap); } } }); ``` 在以上代码中,通过监听 `onPhotoTap` 方法获取触摸点的坐标,并将其转换为图片上的坐标。然后,使用 Canvas 和 Paint 绘制涂鸦,最后将修改后的图片显示在 PhotoView 上。 你可以根据需要修改涂鸦的样式、颜色和大小。 希望这些信息对你有所帮助!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值