Android chrisbanes-PhotoView 使用案例

今天用了下chrisbanes-PhotoView 第三方library,地址:https://github.com/chrisbanes/PhotoView/tree/master

Android studio注意导入library Modules的时候,要在最外层的build.gradle里面加上classpath ‘com.novoda:bintray-release:0.3.4’
比如这样:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.novoda:bintray-release:0.3.4'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

可以去看上面的例子,也可以看下面的三个例子:

1、SimpleSample 基础案例

 .co.senab.photoview.PhotoView
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="1"
     android:id="@+id/pv"/>
public class SimpleActivity extends Activity {

    private ImageView imageView;
    private PhotoViewAttacher attacher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        imageView = (ImageView) findViewById(R.id.pv);
        imageView.setImageResource(R.drawable.wallpaper);
        attacher = new PhotoViewAttacher(imageView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem item = menu.findItem(R.id.menu_zoom_toggle);
        if (item != null) {
            item.setTitle(attacher.canZoom() ? "Disable Zoom" : "Enable Zoom");
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_zoom_toggle:
                // 根据手势缩放
                attacher.setZoomable(!attacher.canZoom());
                break;
            case R.id.menu_random:
                // 缩放到随机比例
                Random random = new Random();
                float minScale = attacher.getMinimumScale();
                float maxScale = attacher.getMaximumScale();
                float randomScale = minScale + (random.nextFloat() * (maxScale - minScale));
                attacher.setScale(randomScale, true);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        attacher.cleanup();
    }
}

2、ViewPagerSample 滑动案例

public class ViewPagerActivity extends Activity {

    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(new SamplePagerAdapter());
    }

    static class SamplePagerAdapter extends PagerAdapter {

        private static final int[] sDrawables = {R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper};

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            photoView.setImageResource(sDrawables[position]);
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getCount() {
            return sDrawables.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}

3、RotationSample 图片旋转案例

public class RotationSampleActivity extends Activity {

    private PhotoView photoView;
    private final android.os.Handler handler = new android.os.Handler();
    private boolean rotating = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rotation);
        photoView = (PhotoView) findViewById(R.id.pv);
        photoView.setImageResource(R.drawable.wallpaper);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 0, Menu.NONE, "Rotate 10° Right");
        menu.add(Menu.NONE, 1, Menu.NONE, "Rotate 10° Left");
        menu.add(Menu.NONE, 2, Menu.NONE, "Toggle automatic rotation");
        menu.add(Menu.NONE, 3, Menu.NONE, "Reset to 0");
        menu.add(Menu.NONE, 4, Menu.NONE, "Reset to 90");
        menu.add(Menu.NONE, 5, Menu.NONE, "Reset to 180");
        menu.add(Menu.NONE, 6, Menu.NONE, "Reset to 270");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                photoView.setRotationBy(10);
                return true;
            case 1:
                photoView.setRotationBy(-10);
                return true;
            case 2:
                toggleRotation();
                return true;
            case 3:
                photoView.setRotationTo(0);
                return true;
            case 4:
                photoView.setRotationTo(90);
                return true;
            case 5:
                photoView.setRotationTo(180);
                return true;
            case 6:
                photoView.setRotationTo(270);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void toggleRotation() {
        if (rotating) {
            handler.removeCallbacksAndMessages(null);
        } else {
            rotateLoop();
        }
        rotating = !rotating;
    }

    private void rotateLoop() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                photoView.setRotationBy(1);
                rotateLoop();
            }
        }, 15);
    }
}

你可能感兴趣的:(Android)