android 加载类似长微博的超长图或超大图subsampling-scale-image-view

本文介绍了一种使用BitmapRegionDecoder处理超长图像的方法,并提供了一个具体的实例代码,展示了如何利用SubsamplingScaleImageView控件有效地加载和显示大尺寸图片。

超长图直接加载的时候有可能会出现超出Bitmap最大高度,直接使用BitmapFactory.Options来缩放图片可能会把图片缩的太小,所以加载长图最好还是用BitmapRegionDecoder来进行分段显示。

subsampling-scale-image-view显示这种超长图或超大图效果很好,而且对手势判断也做的非常好,不过它只是一个控件,不能加载网络资源,只能通过其他下载后再用它进行加载,上效果

贴代码

 

public class MainActivity extends AppCompatActivity {


    private SubsamplingScaleImageView scaleImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scaleImageView = findViewById(R.id.scale_image_view);
//        设置最小缩放比,默认是1
//        scaleImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
//        scaleImageView.setMinScale(1.5f);

//        scaleImageView.setImage(ImageSource.uri("本地路径"));
        scaleImageView.setImage(ImageSource.asset("scale_image_1.jpg"));
//      设置默认缩放比和初始显示位置
        scaleImageView.setScaleAndCenter(1.5f, new PointF(0, 0));


    }

    public void change(View view) {
        scaleImageView.recycle();
        scaleImageView.setImage(ImageSource.asset("scale_image_3.jpg"));
        scaleImageView.setScaleAndCenter(1.5f, new PointF(0, 0));
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.example.scaleimage.MainActivity">

    <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
        android:id="@+id/scale_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="换图"
        android:onClick="change"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

长图是随便网上找的,大图是其他人那里拿的,地址是

http://upload.wikimedia.org/wikipedia/commons/3/33/Physical_Political_World_Map.jpg

分辨率6480x3888

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值