安卓图片(ImageView):展示大图、双击放大、触摸放大缩小、滑动切换、长按保存等

我只是一个虾纸丫 提交于 2019-12-06 03:25:07

安卓图片(ImageView):展示大图、双击放大、触摸放大缩小、滑动切换、长按保存等

背景:由于项目中需要做一个图片展示功能,大概就是在一个页面中有一个listview或者recyclerview的布局,然后item中有图文混排,点击图片后能看大图,并且能触摸放大,双击放大,触摸缩小,多张图片的话还可以滑动切换,长按图片还可以保存到本地等操作,然后就开始按着需求设计功能,由于网上也有很多类似demo,简直满坑满谷,所以就不在介绍各种原理及实现方法,功能很简单,代码也很easy,主要就是依赖photoView开源代码完成,各位看官就将就一下吧:
- 1.怎样展示大图
- 2.自定义imageview
- 3.多张图片滑动
- 4.长按保存,取消


1.首先点击图片后要展示当前所点击的图片大图,用什么承载这张图片呢,这里我用的是dialog,下面po出代码,注释很详细,放心阅读:

/**
  * 点击图片后,调用该展示大图方法
  * @param mPosition
  */
public void showPictureDialog(final int mPosition) {
    //创建dialog
    mDialog = new Dialog(this, R.style.PictureDialog);
    final Window window1 = mDialog.getWindow() ;
    WindowManager m = getWindowManager();
    Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
    WindowManager.LayoutParams p = window1.getAttributes(); // 获取对话框当前的参数值
    p.height = (int) (d.getHeight() * 1.0); // 改变的是dialog框在屏幕中的位置而不是大小
    p.width = (int) (d.getWidth() * 1.0); // 宽度设置为屏幕
    window1.setAttributes(p);
    View inflate = View.inflate(QuestionDetailsActivity.this, R.layout.picture_dialog, null);//该layout在后面po出
    int screenWidth = getResources().getDisplayMetrics().widthPixels;
    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(screenWidth, ViewGroup.LayoutParams.MATCH_PARENT);
    mDialog.setContentView(inflate, layoutParams);
    pager = (ViewPagerFixed) inflate.findViewById(R.id.gallery01);
    pager.setOnPageChangeListener(pageChangeListener);
    PicturePageAdapter adapter = new PicturePageAdapter((ArrayList<String>) mListPicPath, this);
    pager.setAdapter(adapter);
    pager.setPageMargin(0);
    pager.setCurrentItem(mPosition);        window1.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    mDialog.setOnKeyListener(new DialogOnKeyListener());
    mDialog.show();
    adapter.setOnPictureClickListener(new PicturePageAdapter.OnPictureClickListener() {
        @Override
        public void OnClick() {                window1.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            mDialog.dismiss();
        }
    });
    //长按图片保存
    adapter.setOnPictureLongClickListener(new PicturePageAdapter.OnPictureLongClickListener() {
        @Override
        public void OnLongClick() {
            //展示保存取消dialog
            showPicDialog();
        }
    });
}
//展示保存取消dialog
    private void showPicDialog() {
        homeWorkDialog = new SavePicDialog(QuestionDetailsActivity.this);
        homeWorkDialog.setOnClickListener(new SavePicDialog.onClickListener() {
            @Override
            public void onConfirm() {
                //启动图片下载线程
                if (pager != null){
                    onDownLoad(mListPicPath.get(pager.getCurrentItem()));
                }
              Toast.makeText(QuestionDetailsActivity.this,"保存成功!",Toast.LENGTH_SHORT).show();
                homeWorkDialog.dismiss();
            }
            @Override
            public void onCancle() {
                homeWorkDialog.dismiss();
            }
        });
        Window dialogWindow = homeWorkDialog.getWindow();
        dialogWindow.setGravity( Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 0;
        dialogWindow.setAttributes(lp);
        homeWorkDialog.show();
    }
//上面style
<style name="PictureDialog" parent="android:style/Theme.Translucent.NoTitleBar.Fullscreen">
        <item name="android:background">#00000000</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
    </style>

//上面picture_dialog布局
<?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">
    <com.XXX.ViewPagerFixed
      android:id="@+id/gallery01" 
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    </com.XXX.ViewPagerFixed>
</RelativeLayout>
/**
 * Created by ml on 17/6/30.
 */

public class PicturePageAdapter extends PagerAdapter {
    private ArrayList<String> mListUrls;
    private SparseArray<View> mViews = new SparseArray<>();
    private Context mContext;
    private int size;
    private OnPictureClickListener mOnPictureClickListener;
    private OnPictureLongClickListener mOnPictureLongClickListener;

    public PicturePageAdapter(ArrayList<String> listUrls, Context context) {
        mContext = context;
        mListUrls = listUrls;
        size = mListUrls == null ? 0 : mListUrls.size();
    }

    public SparseArray<View> getData(){
        return this.mViews;
    }

    public int getCount() {
        return size;
    }

    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
        arg0.removeView(mViews.get(arg1));
    }

    public void finishUpdate(View arg0) {
    }

    public Object instantiateItem(ViewGroup arg0, int arg1) {
        View view = mViews.get(arg1);
        if(view == null){
            //引入photoView
            PhotoView img = new PhotoView(mContext);
            try {
                img.setBackgroundColor(0xff000000);
                //Glide需要自己去引入了
                Glide
                        .with(mContext)
                        .load(mListUrls.get(arg1))
                        .into(img);

                img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                img.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                    @Override
                    public void onPhotoTap(View view, float x, float y) {
                        if (mOnPictureClickListener != null) {
                            mOnPictureClickListener.OnClick();
                        }
                    }
                });
                //设置长按图片监听
                img.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        if (mOnPictureLongClickListener != null) {
                            mOnPictureLongClickListener.OnLongClick();
                        }
                        return false;
                    }
                });
                mViews.put(arg1,img);
                view = img;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        ((ViewPagerFixed) arg0).addView(view);

        return view;
    }

    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    /**
     * 图片监听接口
     */
    public interface OnPictureClickListener{
        void OnClick();
    }

    /**
     * 设置图片监听
     * @param onPictureClickListener
     */
    public void setOnPictureClickListener(OnPictureClickListener onPictureClickListener){
        mOnPictureClickListener = onPictureClickListener;
    }

    /**
     * 长按图片监听接口
     */
    public interface OnPictureLongClickListener{
        void OnLongClick();
    }

    /**
     * 设置长按图片监听
     * @param onPictureLongClickListener
     */
    public void setOnPictureLongClickListener(OnPictureLongClickListener onPictureLongClickListener){
        mOnPictureLongClickListener = onPictureLongClickListener;
    }
}
//自定义viewpager类
public class ViewPagerFixed extends android.support.v4.view.ViewPager {
public ViewPagerFixed(Context context) {super(context);}
public ViewPagerFixed(Context context, AttributeSet attrs) {super(context, attrs);}
@Override
    public boolean onTouchEvent(MotionEvent ev) {
        try {
            return super.onTouchEvent(ev);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        }
        return false;
    }
@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        try {
            return super.onInterceptTouchEvent(ev);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        }
        return false;
    }
}
希望大家静下心来整理,也给自己增加理解,或者发现不好的地方,指出来,咱们一起进步,完善

好了,这样就结束了,谢谢大家浏览使用,也欢迎提出问题,一起进步!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!