(安卓自學筆記五)photoview的使用

首先添加依賴

compile 'com.github.chrisbanes:PhotoView:1.2.6'

如果依賴文件加載失敗就修改gradle

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
        jcenter()
    }
}

如果從出現以下報錯

Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.

就用exclude去除依賴中多餘的部分

compile ('com.github.chrisbanes:PhotoView:1.2.6'){
        exclude module:
            'support-v4'
        exclude group:
                'com.android.support'}

添加成功之後,像Imageview一樣去建一個佈局

    <uk.co.senab.photoview.PhotoView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg"
        />

佈局怎麼樣就看自己的喜好了

然後去Activity中註解

private PhotoView iv;
private PhotoViewAttacher ivAttacher;
iv=(PhotoView)findViewById(R.id.iv);
ivAttacher= new PhotoViewAttacher(iv);

和Imageview不一樣的地方是,多了一個attacher來管理photoview

可以通過它來添加點擊監聽器

ivAttacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener(){
            @Override
            public void onPhotoTap(View view, float v, float v1) {
//                if(pictureUrl.size()>currentPicture){
 //                   Glide.with(ShowActivity.this).load(pictureUrl.get(currentPicture++)).asBitmap().into(iv);
  //                  Log.w("phototap","加載完畢");
                }
            }
            @Override
            public void onOutsidePhotoTap() {
  //              Log.w("phototap","ouside");
            }
        });

或者是一個長按的監聽器

ivAttacher.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View view) {
            Log.w("longclick","長按事件");
          //  Dialog dialog = new AlertDialog.Builder(ShowActivity.this).setTitle("保存?").setMessage("是否確認保存?").
          //          setPositiveButton("確認", new DialogInterface.OnClickListener()
          //          {
           //             public void onClick(DialogInterface dialog, int whichButton)
           //             {
           //                 savePicture(view);
           //                 dialog.cancel();
            //            }
            //        }).create();
            //    dialog.show();
                return true;
            }
        });

可以設置是否允許放大

ivAttacher.setZoomable(true);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章