SubsamplingScaleImageView加载大长图

1、SubsamplingScaleImageView是一个非常优秀长图加载控件,使用起来也非常简单,主要适用于当原图的长度和宽度都超过手机屏幕的长度和宽度,当用普通的ImageView控件的时候,图片会严重的变形的,而SubsamplingScaleImageView非常完美的解决了这一特性。

2、在安卓studio中使用

2.1在build.gradle中引入,我使用的是Glide下载的

compile 'com.github.bumptech.glide:glide:3.7.0' compile ‘

com.davemorrissey.labs:subsampling-scale-image-view:3.5.0'

2.2引入布局xml

2.3代码展示

public class MainActivity extends AppCompatActivity {

    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String imagePath="";
        final SubsamplingScaleImageView imageView =  (SubsamplingScaleImageView)findViewById(R.id.imageView);
        //手势回调
        gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                if (imageView.isReady()) {
                    PointF sCoord = imageView.viewToSourceCoord(e.getX(), e.getY());
                    Toast.makeText(getApplicationContext(), "单击: " + ((int) sCoord.x) + ", " + ((int) sCoord.y),
                            Toast.LENGTH_SHORT).show();
                } else {
                    // Toast.makeText(getApplicationContext(), fail_tips, Toast.LENGTH_SHORT).show();
                }

                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                if (imageView.isReady()) {
                    PointF sCoord = imageView.viewToSourceCoord(e.getX(), e.getY());
                    Toast.makeText(getApplicationContext(), "长按: " + ((int) sCoord.x) + ", " + ((int) sCoord.y),
                            Toast.LENGTH_SHORT).show();
                } else {
                    //Toast.makeText(getApplicationContext(), fail_tips, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                if (imageView.isReady()) {
                    PointF sCoord = imageView.viewToSourceCoord(e.getX(), e.getY());
                    Toast.makeText(getApplicationContext(), "双击: " + ((int) sCoord.x) + ", " + ((int) sCoord.y),
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
                }

                return false;
            }
        });

        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return gestureDetector.onTouchEvent(motionEvent);
            }
        });

        imageView.setMaxScale(15);
        imageView.setZoomEnabled(true);
        // spinner.setVisibility(View.GONE);
        imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
        Glide.with(this)
                .load(imagePath).downloadOnly(new SimpleTarget() {
            @Override
            public void onResourceReady(File resource, GlideAnimation glideAnimation) {
                int  sWidth = BitmapFactory.decodeFile(resource.getAbsolutePath()).getWidth();
                int sHeight = BitmapFactory.decodeFile(resource.getAbsolutePath()).getHeight();
                WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
                int width = wm.getDefaultDisplay().getWidth();
                int height = wm.getDefaultDisplay().getHeight();
                if (sHeight >= height
                        && sHeight / sWidth >=3) {
                    imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
                    imageView.setImage(ImageSource.uri(Uri.fromFile(resource)), new ImageViewState(2.0F, new PointF(0, 0), 0));
                }else {
                    imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
                    imageView.setImage(ImageSource.uri(Uri.fromFile(resource)));
                    imageView.setDoubleTapZoomStyle(ZOOM_FOCUS_CENTER_IMMEDIATE);
                }


            }

            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                super.onLoadFailed(e, errorDrawable);
            }
        });
    }
}

3、注意事项

     使用Glide下载大长图时,如果在项目中同时使用了使用Glide进行图片格式的转换时就会出现下载图片失败的问题。比如使用了GlideModule改变图片的加载格式。详情读者可以自行去研究源码的实现原理

 

 

你可能感兴趣的:(SubsamplingScaleImageView加载大长图)