仿微信朋友圈图片预览自定义View

来源:互联网 发布:java参数公式 编辑:程序博客网 时间:2024/06/15 21:15

1、先来看看效果




2、使用方法

在xml中添加如下代码:


<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.imagepreviewdemo.MainActivity">            <!--作为背景-->    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@android:color/black"        android:id="@+id/bg"></FrameLayout>            <com.imagepreviewdemo.view.ImagesPreView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/preView"        ></com.imagepreviewdemo.view.ImagesPreView></FrameLayout>


在activity的onCreate()中初始化


final ImagesPreView preView=(ImagesPreView)findViewById(R.id.preView);final FrameLayout backGround=(FrameLayout)findViewById(R.id.bg);//添加图片资源List<Integer>resources=new ArrayList<Integer>();resources.add(R.drawable.timg_1);resources.add(R.drawable.timg_2);resources.add(R.drawable.timg_3);//初始化preView.setResources(resources,getSupportFragmentManager());//设置当前显示的图片preView.setCurrentItem(0);//设置背景View,会随着图片向下滑动渐隐preView.setBackGroundView(backGround);//设置图片向下滑动事件监听preView.setOnScrollListener(new ScaleImagView.OnScrollListener() {    @Override    public void onScroll(float scroll) {        //scroll值为向下滑动的距离和屏幕总高度百分比x100,值0~100之间        if (scroll>50){            //当图片向下滑动的距离超过50%的屏幕总高度时,设置打开松开手指时的事件监听            preView.setTouchUpEnable(true);        }    }});//松开手指时的事件监听,如果没有设置preView.setTouchUpEnable(true),以下方法将不会被执行preView.setOnTouchUpListener(new ScaleImagView.OnTouchUpListener() {    @Override    public void onTouchUp() {    }});//设置图片滑动监听preView.setOnImageChangeListener(new ImagesPreView.OnImageChangeListener() {    @Override    public void OnImageChange(int position) {    }});

除了传drawable资源以外还可以传url资源
List<String>urlStrings=new ArrayList<>();urlStrings.add("http://192.168.1.39:90/Photos/a.png");urlStrings.add("http://192.168.1.39:90/Photos/b.png");preView.setFilesOrUrl(urlStrings,getSupportFragmentManager());


本地file资源

List<String>filePaths=new ArrayList<>();filePaths.add("/storage/sdcard0/a.png");filePaths.add("/storage/sdcard0/b.png");preView.setFilesOrUrl(filePaths,getSupportFragmentManager());


3、用到的开源框架

Glide


4、源码地址

欢迎勘误

点击打开链接








阅读全文
0 0