package wst.webview;
/*
* Copyright 2012 Laurence Dawson
*
* 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.
*/
/*
* This class is based upon the file ImageViewTouchBase.java which can be found at:
* https://dl-ssl.google.com/dl/googlesource/git-repo/repo
*
* Copyright (C) 2009 The Android Open Source Project
*/
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Build;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
@SuppressLint("NewApi")
public class ZoomableImageView extends View {
// Statics
static final float sPanRate = 7;
static final float sScaleRate = 1.25F;
static final int sPaintDelay = 250;
static final int sAnimationDelay = 500;
// This is the base transformation which is used to show the image
// initially. The current computation for this shows the image in
// it's entirety, letterboxing as needed. One could chose to
// show the image as cropped instead.
//
// This matrix is recomputed when we go from the thumbnail image to
// the full size image.
private Matrix mBaseMatrix = new Matrix();
// This is the supplementary transformation which reflects what
// the user has done in terms of zooming and panning.
//
// This matrix remains the same when we go from the thumbnail image
// to the full size image.
private Matrix mSuppMatrix = new Matrix();
// This is the final matrix which is computed as the concatentation
// of the base matrix and the supplementary matrix.
private Matrix mDisplayMatrix = new Matrix();
// A replacement ImageView matrix
private Matrix mMatrix = new Matrix();
// Used to filter the bitmaps when hardware acceleration is not enabled
private Paint mPaint;
// Temporary buffer used for getting the values out of a matrix.
private float[] mMatrixValues = new float[9];
// The current bitmap being displayed.
private Bitmap mBitmap;
// Dimensions for the view
private int mThisWidth = -1, mThisHeight = -1;
// The max zoom for the view, determined programatically
private float mMaxZoom;
// If not null, calls setImageBitmap when onLayout is triggered
private Runnable mOnLayoutRunnable = null;
// Stacked to the internal queue to invalidate the view
private Runnable mRefresh = null;
// Stacked to the internal queue to scroll the view
private Runnable mFling = null;
// The time of the last draw operation
private double mLastDraw = 0;
// Scale and gesture listeners for the view
private ScaleGestureDetector mScaleDetector;
private GestureDetector mGestureDetector;
// Single tap listener
private OnImageTouchedListener mImageTouchedListener;
// Programatic entry point
public ZoomableImageView(Context context) {
super(context);
init( context );
}
// Set the single tap listener
public void setOnImageTouchedListener( OnImageTouchedListener listener ){
this.mImageTouchedListener = listener;
}
// XML entry point
public ZoomableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init( context );
}
// Setup the view
private void init( Context context) {
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setFilterBitmap(true);
mPaint.setAntiAlias(true);
// Setup the refresh runnable
mRefresh = new Runnable() {
@Override
public void run() {
postInvalidate();
}
};
// Setup the gesture and scale listeners
mScaleDetector = new ScaleGestureDetector( context, new ScaleListener() );
mGestureDetector = new GestureDetector(context, new MyGestureListener());
// Force hardware acceleration
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
// Get the bitmap for the view
public Bitmap getImageBitmap(){
return mBitmap;
}
// Free the bitmaps and matrices
public void clear(){
if(mBitmap!=null)
mBitmap = null;
}
// When the layout is calculated, set the
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
mThisWidth = right - left;
mThisHeight = bottom - top;
Runnable r = mOnLayoutRunnable;
if (r != null) {
mOnLayoutRunnable = null;
r.run();
}
if (mBitmap != null) {
setBaseMatrix(mBitmap, mBaseMatrix);
setImageMatrix(getImageViewMatrix());
}
}
// Translate a given point through a given matrix.
static private void translatePoint(Matrix matrix, float [] xy) {
matrix.mapPoints(xy);
}
// Identical to the setImageMatrix method in ImageView
public void setImageMatrix(Matrix m){
if (m != null && m.isIdentity()) {
m = null;
}
// don't invalidate unless we're actually changing our matrix
if (m == null && !this.mMatrix.isIdentity() || m != null && !this.mMatrix.equals(m)) {
this.mMatrix.set(m);
invalidate();
}
}
// Sets the bitmap for the image and resets the base
public void setImageBitmap(final Bitmap bitmap) {
final int viewWidth = getWidth();
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && bitmap!=null && bitmap.getHeight()>1800 )
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
if (viewWidth <= 0) {
mOnLayoutRunnable = new Runnable() {
public void run() {
setImageBitmap(bitmap);
}
};
return;
}
if (bitmap != null) {
setBaseMatrix(bitmap, mBaseMatrix);
this.mBitmap = bitmap;
} else {
mBaseMatrix.reset();
this.mBitmap = bitmap;
}
mSuppMatrix.reset();
setImageMatrix(getImageViewMatrix());
mMaxZoom = maxZoom();
// Set the image to fit the screen
zoomTo(zoomDefault());
}
// Unchanged from ImageViewTouchBase
// Center as much as possible in one or both axis. Centering is
// defined as follows: if the image is scaled down below the
// view's dimensions then center it (literally). If the image
// is scaled larger than the view and is translated out of view
// then translate it back into view (i.e. eliminate black bars).
protected void center(boolean vertical, boolean horizontal, boolean animate) {
if (mBitmap == null)
return;
Matrix m = getImageViewMatrix();
float [] topLeft = new float[] { 0, 0 };
float [] botRight = new float[] { mBitmap.getWidth(), mBitmap.getHeight() };
translatePoint(m, topLeft);
translatePoint(m, botRight);
float height = botRight[1] - topLeft[1];
float width = botRight[0] - topLeft[0];
float deltaX = 0, deltaY = 0;
if (vertical) {
int viewHeight = getHeight();
if (height < viewHeight) {
deltaY = (viewHeight - height)/2 - topLeft[1];
} else if (topLeft[1] > 0) {
deltaY = -topLeft[1];
} else if (botRight[1] < viewHeight) {
deltaY = getHeight() - botRight[1];
}
}
if (horizontal) {
int viewWidth = getWidth();
if (width < viewWidth) {
deltaX = (viewWidth - width)/2 - topLeft[0];
} else if (topLeft[0] > 0) {
deltaX = -topLeft[0];
} else if (botRight[0] < viewWidth) {
deltaX = viewWidth - botRight[0];
}
}
postTranslate(deltaX, deltaY);
if (animate) {
Animation a = new TranslateAnimation(-deltaX, 0, -deltaY, 0);
a.setStartTime(SystemClock.elapsedRealtime());
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
AndroidJavaScriptDemo.zip (50个子文件)
AndroidJavaScriptDemo
bin
classes.dex 19KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 2KB
AndroidJavaScriptDemo.apk 24KB
jarlist.cache 119B
dexedLibs
annotations-a4e82f0af5bc34208072482ae1a8d410.jar 943B
classes
wst
webview
ShowWebImageActivity.class 2KB
MainActivity.class 2KB
MainActivity$MyWebViewClient.class 2KB
BuildConfig.class 331B
ZoomableImageView$1.class 675B
R$layout.class 410B
ZoomableImageView$MyGestureListener.class 3KB
MainActivity$JavascriptInterface.class 1KB
R$id.class 467B
R$drawable.class 379B
R$attr.class 319B
R.class 564B
OnImageTouchedListener.class 168B
ZoomableImageView$3.class 1KB
ZoomableImageView$ScaleListener.class 1KB
ZoomableImageView.class 11KB
ZoomableImageView$4.class 1KB
R$string.class 406B
ZoomableImageView$2.class 756B
R$color.class 388B
R$style.class 377B
resources.ap_ 12KB
AndroidManifest.xml 1011B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
values
strings.xml 179B
styles.xml 472B
color.xml 129B
drawable-mdpi
icon.png 3KB
layout
show_webimage.xml 785B
main.xml 416B
assets
wst.html 616B
gen
wst
webview
BuildConfig.java 153B
R.java 1KB
proguard.cfg 1KB
src
wst
webview
MainActivity.java 3KB
OnImageTouchedListener.java 694B
ShowWebImageActivity.java 1KB
ZoomableImageView.java 17KB
.project 857B
.classpath 380B
project.properties 563B
AndroidManifest.xml 1011B
共 50 条
- 1
王挺帅
- 粉丝: 117
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页