Android開源庫--PhotoView圖片查看

來源:互聯網
上載者:User

標籤:

如果說我比別人看得更遠些,那是因為我站在了巨人的肩上。

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

介紹

在一般的應用中,總會遇到查看圖片的功能,點擊圖片之後,詳細查看,PhotoView這個開原始檔控制就幫我們很好的完成了這個功能。

它繼承自ImageView,支援縮放,以及各種手勢的監聽。

特性

  • 通過多點觸摸和雙擊來進行放大。
  • 通過輕彈劃實現平滑滾動。
  • 在帶有滾動功能的父類工作的很好(比如ViewPager)
  • 允許當顯示的矩陣發生髮生改變時通知應用,當你需要基於當前縮放/滑動位置來更新你的ui時非常有用。
  • 允許當你點擊圖片時通知應用

使用

1、通過代碼附加ImageView使用

 

ImageView mImageView;PhotoViewAttacher mAttacher;@Overridepublic 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 callattacher.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());        }    }}

 

Android開源庫--PhotoView圖片查看

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.