PhotoView 图片缩放功能

Android 图片浏览功能  图片放大缩小  ,使用 photoview 双击或双指缩放的ImageView使用多点触控和双击。滚动,以平滑滚动甩。

首先去github网站上去下载  网址:https://github.com/chrisbanes/PhotoView


我用的Studio写的  用eclipse的可以下载库文件,然后依赖库文件即可



把这个放到根目录 build.gradle 

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


把这个放到你项目app下面的  build.gradle


dependencies {
    compile 'com.github.chrisbanes:PhotoView:1.3.0'
}

具体代码如下:


package ti.potoviewdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    public  void click(View v){//跳转页面  缩放图片

        startActivity(new Intent(MainActivity.this,Photo.class));
        
    }
}




package ti.potoviewdemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import butterknife.ButterKnife;
import butterknife.InjectView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by ti on 2016/9/15.
 */
public class Photo extends Activity {
    private static final String TAG = "Photo";
    @InjectView(R.id.iv)
    ImageView iv;
    private BitmapFactory.Options options;
    private Bitmap bitmap;
    private WindowManager windowManager;
    private Display display;
    private PhotoViewAttacher attacher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);

        ButterKnife.inject(this);


        attacher = new PhotoViewAttacher(iv);


    }

    private void compress() {//压缩图片
        //拿到图片
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.yang);
        //测量
        options = new BitmapFactory.Options();
        options.inJustDecodeBounds=true;//只测量
        float height=bitmap.getHeight();
        float width=bitmap.getWidth();

        windowManager = getWindowManager();
        display = windowManager.getDefaultDisplay();

        float screenWidth=display.getWidth();

        float scale=screenWidth/width;

        if(scale==0){
            scale=1;
        }

        int dispHeiggt= (int) (height*scale);
        Log.e(TAG, "compress: "+dispHeiggt );
        RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams((int) screenWidth,dispHeiggt);

        iv.setLayoutParams(layoutParams);



    }
}






把这个放到你项目的根目录中去  build.gradle

你可能感兴趣的:(PhotoView 图片缩放功能)