zoukankan      html  css  js  c++  java
  • Android开源库--PhotoView图片查看

    如果说我比别人看得更远些,那是因为我站在了巨人的肩上。

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

    介绍

    在一般的应用中,总会遇到查看图片的功能,点击图片之后,详细查看,PhotoView这个开源控件就帮我们很好的完成了这个功能。

    它继承自ImageView,支持缩放,以及各种手势的监听。

    特性

    • 通过多点触摸和双击来进行放大。
    • 通过轻弹划实现平滑滚动。
    • 在带有滚动功能的父类工作的很好(比如ViewPager)
    • 允许当显示的矩阵发生发生改变时通知应用,当你需要基于当前缩放/滑动位置来更新你的ui时非常有用。
    • 允许当你点击图片时通知应用

    使用

    1、通过代码附加ImageView使用

    ImageView mImageView;
    PhotoViewAttacher mAttacher;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Any implementation of ImageView can be used!
        mImageView = (ImageView) findViewById(R.id.iv_photo);
    
        // Set the Drawable displayed
        Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper);
        mImageView.setImageDrawable(bitmap);
    
        // Attach a PhotoViewAttacher, which takes care of all of the zooming functionality.
        mAttacher = new PhotoViewAttacher(mImageView);
    }
    
    
    // If you later call mImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call
    attacher.update();

    2、通过layout使用。

    <uk.co.senab.photoview.PhotoView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/iv_photo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    PS:如果需要旋转图片可以通过setRotationTo,setRotationBy实现,其他相关操作间示例代码:

    /*******************************************************************************
     * Copyright 2011, 2012 Chris Banes.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     *******************************************************************************/
    package uk.co.senab.photoview.sample;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.Matrix;
    import android.graphics.RectF;
    import android.graphics.drawable.Drawable;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.ImageView.ScaleType;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.Random;
    
    import uk.co.senab.photoview.PhotoViewAttacher;
    import uk.co.senab.photoview.PhotoViewAttacher.OnMatrixChangedListener;
    import uk.co.senab.photoview.PhotoViewAttacher.OnPhotoTapListener;
    
    public class SimpleSampleActivity extends Activity {
    
        static final String PHOTO_TAP_TOAST_STRING = "Photo Tap! X: %.2f %% Y:%.2f %% ID: %d";
        static final String SCALE_TOAST_STRING = "Scaled to: %.2ff";
    
        private TextView mCurrMatrixTv;
    
        private PhotoViewAttacher mAttacher;
    
        private Toast mCurrentToast;
    
        private Matrix mCurrentDisplayMatrix = null;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ImageView mImageView = (ImageView) findViewById(R.id.iv_photo);
            mCurrMatrixTv = (TextView) findViewById(R.id.tv_current_matrix);
    
            Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper);
            mImageView.setImageDrawable(bitmap);
    
            // The MAGIC happens here!
            mAttacher = new PhotoViewAttacher(mImageView);
    
            // Lets attach some listeners, not required though!
            mAttacher.setOnMatrixChangeListener(new MatrixChangeListener());
            mAttacher.setOnPhotoTapListener(new PhotoTapListener());
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main_menu, menu);
            return super.onCreateOptionsMenu(menu);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
    
            // Need to call clean-up
            mAttacher.cleanup();
        }
    
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            MenuItem zoomToggle = menu.findItem(R.id.menu_zoom_toggle);
            assert null != zoomToggle;
            zoomToggle.setTitle(mAttacher.canZoom() ? R.string.menu_zoom_disable : R.string.menu_zoom_enable);
    
            return super.onPrepareOptionsMenu(menu);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_zoom_toggle:
                    mAttacher.setZoomable(!mAttacher.canZoom());
                    return true;
    
                case R.id.menu_scale_fit_center:
                    mAttacher.setScaleType(ScaleType.FIT_CENTER);
                    return true;
    
                case R.id.menu_scale_fit_start:
                    mAttacher.setScaleType(ScaleType.FIT_START);
                    return true;
    
                case R.id.menu_scale_fit_end:
                    mAttacher.setScaleType(ScaleType.FIT_END);
                    return true;
    
                case R.id.menu_scale_fit_xy:
                    mAttacher.setScaleType(ScaleType.FIT_XY);
                    return true;
    
                case R.id.menu_scale_scale_center:
                    mAttacher.setScaleType(ScaleType.CENTER);
                    return true;
    
                case R.id.menu_scale_scale_center_crop:
                    mAttacher.setScaleType(ScaleType.CENTER_CROP);
                    return true;
    
                case R.id.menu_scale_scale_center_inside:
                    mAttacher.setScaleType(ScaleType.CENTER_INSIDE);
                    return true;
    
                case R.id.menu_scale_random_animate:
                case R.id.menu_scale_random:
                    Random r = new Random();
    
                    float minScale = mAttacher.getMinimumScale();
                    float maxScale = mAttacher.getMaximumScale();
                    float randomScale = minScale + (r.nextFloat() * (maxScale - minScale));
                    mAttacher.setScale(randomScale, item.getItemId() == R.id.menu_scale_random_animate);
    
                    showToast(String.format(SCALE_TOAST_STRING, randomScale));
    
                    return true;
                case R.id.menu_matrix_restore:
                    if (mCurrentDisplayMatrix == null)
                        showToast("You need to capture display matrix first");
                    else
                        mAttacher.setDisplayMatrix(mCurrentDisplayMatrix);
                    return true;
                case R.id.menu_matrix_capture:
                    mCurrentDisplayMatrix = mAttacher.getDisplayMatrix();
                    return true;
                case R.id.extract_visible_bitmap:
                    try {
                        Bitmap bmp = mAttacher.getVisibleRectangleBitmap();
                        File tmpFile = File.createTempFile("photoview", ".png",
                                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
                        FileOutputStream out = new FileOutputStream(tmpFile);
                        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
                        out.close();
                        Intent share = new Intent(Intent.ACTION_SEND);
                        share.setType("image/png");
                        share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tmpFile));
                        startActivity(share);
                        Toast.makeText(this, String.format("Extracted into: %s", tmpFile.getAbsolutePath()), Toast.LENGTH_SHORT).show();
                    } catch (Throwable t) {
                        t.printStackTrace();
                        Toast.makeText(this, "Error occured while extracting bitmap", Toast.LENGTH_SHORT).show();
                    }
                    return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        private class PhotoTapListener implements OnPhotoTapListener {
    
            @Override
            public void onPhotoTap(View view, float x, float y) {
                float xPercentage = x * 100f;
                float yPercentage = y * 100f;
    
                showToast(String.format(PHOTO_TAP_TOAST_STRING, xPercentage, yPercentage, view == null ? 0 : view.getId()));
            }
        }
    
        private void showToast(CharSequence text) {
            if (null != mCurrentToast) {
                mCurrentToast.cancel();
            }
    
            mCurrentToast = Toast.makeText(SimpleSampleActivity.this, text, Toast.LENGTH_SHORT);
            mCurrentToast.show();
        }
    
        private class MatrixChangeListener implements OnMatrixChangedListener {
    
            @Override
            public void onMatrixChanged(RectF rect) {
                mCurrMatrixTv.setText(rect.toString());
            }
        }
    
    }
  • 相关阅读:
    20130329 基于校历的授课计划与进度管理系统 文档展示(417更新)
    【记录】搭建本地wordpress全过程
    (转)如何修改WAMP中mysql默认空密码
    SQL SERVER 与ACCESS、EXCEL的数据转换
    用C#得到真正的随机数
    (转)SQL Server 2008 R2 中英文 开发版企业版标准版 下载
    [安装升级] Discuz!X2.5 全新安装图文教程
    Arduino语言
    2010全面兼容IE6/IE7/IE8/FF的CSS HACK写法
    ASP.NET2.0利用httphandler实现URL重写(伪URL及伪静态)
  • 原文地址:https://www.cnblogs.com/leestar54/p/4220068.html
Copyright © 2011-2022 走看看