zoukankan      html  css  js  c++  java
  • Android图片的缩放效果

    一.概述

    Android 图片要实现:手势滑动,双击变大,多点触控的效果. 其实是有一定难度的,我们需要用Matrix ,GestureDetector 等等需要完成一个复杂的逻辑才能实现,然而今天我要说的并不是这种方法,而是一个第三方库Photoview,它使得完成图片缩放工作只需要3-5行代码就搞定了. 是不是很爽...

    二.使用方法

    github:https://github.com/chrisbanes/PhotoView

    如果用AS需在引入如下库文件(目前是最新的):

    dependencies {
      compile 'com.commit451:PhotoView:1.2.4'
    }
    配置就是这么简单

    三.代码演示

    public class MainActivity extends AppCompatActivity {
        private PhotoView photoView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initialize();
            showData();
        }
        private void showData() {
            //加载图片
            Glide.with(this)
                    .load(R.mipmap.temp)
                    .asBitmap()
                    .into(photoView);
        }
        private void initialize() {
            photoView = (PhotoView) findViewById(R.id.photoView);
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <uk.co.senab.photoview.PhotoView
            android:id="@+id/photoView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    运行效果如下,支持 上下左右拖动,直接 双击变大, 再次双击全屏显示, 再次双击还原 等效果, 支持多点触摸缩放,任意滑动

    上面代码用到了Glide显示图片,所以项目需引入Glide库: 

    compile 'com.github.bumptech.glide:glide:3.6.1'
    关于Glide的详情介绍参考:

  • 相关阅读:
    java中interface的完整表述
    DoTA与人生
    DotNet.Utilities工具类
    visual studio xcopy /exclude测试
    Assembly.Load()方法,Assembly.LoadFrom()方法,Assembly.LoadFile()方法的区别!
    DataGridView列自适应宽度
    [Head First设计模式]
    winform 取消datagridview第一行选中状态
    winform WebBrowser如何修改使用默认的IE浏览器版本
    C# 注释换行
  • 原文地址:https://www.cnblogs.com/android-zcq/p/5138330.html
Copyright © 2011-2022 走看看