ZoomImageVIEw.java:
复制代码 代码如下:
/**
* @author gongchaobin
*
* 自定义可伸缩的ImageVIEw
*/
public class ZoomImageVIEw extends VIEw{
/** 画笔类 **/
private Paint mPaint;
private Runnable mRefresh = null;
/** 缩放手势监听类 **/
private ScaleGestureDetector mScaleDetector;
/** 手势识别类 **/
private GestureDetector mGestureDetector;
/** 当前被渲染的Bitmap **/
private Bitmap mBitmap;
private int mThisWIDth = -1,mThisHeight = -1;
private Runnable mOnLayoutRunnable = null;
private Matrix mBaseMatrix = new Matrix();
private Matrix mdisplayMatrix = new Matrix();
private Matrix mSuppMatrix = new Matrix();
private Matrix mMatrix = new Matrix();
/** 最大的拉伸比例 **/
private float mMaxZoom;
private float[] mMatrixValues = new float[9];
private Runnable mFling = null;
private double mLastDraw = 0;
static final int sPaintDelay = 250;
public ZoomImageVIEw(Context context,AttributeSet attrs,int defStyle) {
super(context,attrs,defStyle);
// Todo auto-generated constructor stub
init();
}
public ZoomImageVIEw(Context context,AttributeSet attrs) {
super(context,attrs);
// Todo auto-generated constructor stub
init();
}
public ZoomImageVIEw(Context context) {
super(context);
// Todo auto-generated constructor stub
init();
}
private voID init() {
mPaint = new Paint();
// 图像抖动处理
mPaint.setDither(true);
// 过滤优化 *** 作 加快显示
mPaint.setFilterBitmap(true);
// 去掉锯齿
mPaint.setAntiAlias(true);
/** 刷新线程 **/
mRefresh = new Runnable() {
@OverrIDe
public voID run() {
postInvalIDate();
}
};
mScaleDetector = new ScaleGestureDetector(getContext(),new ScaleListener());
mGestureDetector = new GestureDetector(getContext(),new MyGestureListener());
// 判断是否是新的API 开启硬件加速
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(VIEw.LAYER_TYPE_HARDWARE,null);
}
}
public Bitmap getimageBitmap() {
return mBitmap;
}
/** 回收Bitmap **/
public voID clear() {
if(mBitmap != null && !mBitmap.isRecycled()) {
mBitmap.recycle();
mBitmap = null;
}
}
@OverrIDe
protected voID onLayout(boolean changed,int left,int top,int right,
int bottom) {
// Todo auto-generated method stub
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());
}
}
private voID setBaseMatrix(Bitmap bitmap,Matrix matrix) {
float vIEwWIDth = getWIDth();
float vIEwHeight = getHeight();
matrix.reset();
float wIDthScale = Math.min(vIEwWIDth / (float)bitmap.getWIDth(),1.0f);
float heightScale = Math.min(vIEwHeight / (float)bitmap.getHeight(),1.0f);
float scale;
if (wIDthScale > heightScale) {
scale = heightScale;
} else {
scale = wIDthScale;
}
/** 算取比例 进行平移 **/
matrix.setScale(scale,scale);
matrix.postTranslate(
(vIEwWIDth - ((float)bitmap.getWIDth() * scale))/2F,
(vIEwHeight - ((float)bitmap.getHeight() * scale))/2F);
}
protected Matrix getimageVIEwMatrix() {
mdisplayMatrix.set(mBaseMatrix);
mdisplayMatrix.postConcat(mSuppMatrix);
return mdisplayMatrix;
}
public voID setimageMatrix(Matrix m){
/** Matrix是否为空并是否定义 **/
if (m != null && m.isIDentity()) {
m = null;
}
if (m == null && !this.mMatrix.isIDentity() || m != null && !this.mMatrix.equals(m)) {
this.mMatrix.set(m);
invalIDate();
}
}
static private voID translatePoint(Matrix matrix,float [] xy) {
matrix.mapPoints(xy);
}
/**
* 设置Bitmap
*
* @param bitmap
*/
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();
zoomTo(zoomDefault());
}
public voID zoomTo(float scale) {
float wIDth = getWIDth();
float height = getHeight();
zoomTo(scale,wIDth/2F,height/2F);
}
protected voID zoomTo(float scale,float centerX,float centerY) {
if (scale > mMaxZoom) {
scale = mMaxZoom;
}
float oldScale = getScale();
float deltaScale = scale / oldScale;
/** 根据某个中心点按照比例缩放 **/
mSuppMatrix.postscale(deltaScale,deltaScale,centerX,centerY);
setimageMatrix(getimageVIEwMatrix());
center(true,true,false);
}
/**
* 计算中心位置
*
* @param vertical
* @param horizontal
* @param animate
*/
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,-deltaY,0);
a.setStartTime(SystemClock.elapsedRealtime());
a.setDuration(250);
setAnimation(a);
}
setimageMatrix(getimageVIEwMatrix());
}
protected voID postTranslate(float dx,float dy) {
mSuppMatrix.postTranslate(dx,dy);
}
public float getScale() {
return getScale(mSuppMatrix);
}
protected float getScale(Matrix matrix) {
if(mBitmap!=null)
return getValue(matrix,Matrix.MSCALE_X);
else
return 1f;
}
protected float getValue(Matrix matrix,int whichValue) {
matrix.getValues(mMatrixValues);
return mMatrixValues[whichValue];
}
/**
* 计算最大的拉伸比例
*
* @return
*/
protected float maxZoom() {
if (mBitmap == null)
return 1F;
float fw = (float) mBitmap.getWIDth() / (float)mThisWIDth;
float fh = (float) mBitmap.getHeight() / (float)mThisHeight;
float max = Math.max(fw,fh) * 16;
return max;
}
/**
* 原始显示比例
*
* @return
*/
public float zoomDefault() {
if (mBitmap == null)
return 1F;
float fw = (float)mThisWIDth/(float)mBitmap.getWIDth();
float fh = (float)mThisHeight/(float)mBitmap.getHeight();
return Math.max(Math.min(fw,fh),1);
}
protected voID zoomTo(final float scale,final float centerX,final float centerY,final float durationMs) {
final float incrementPerMs = (scale - getScale()) / durationMs;
final float oldScale = getScale();
final long startTime = System.currentTimeMillis();
post(new Runnable() {
public voID run() {
long Now = System.currentTimeMillis();
float currentMs = Math.min(durationMs,(float)(Now - startTime));
float target = oldScale + (incrementPerMs * currentMs);
zoomTo(target,centerY);
if (currentMs < durationMs) {
post(this);
}
}
});
}
protected voID scrollBy( float distanceX,float distanceY,final float durationMs ){
final float dx = distanceX;
final float dy = distanceY;
final long startTime = System.currentTimeMillis();
mFling = new Runnable() {
float old_x = 0;
float old_y = 0;
public voID run()
{
long Now = System.currentTimeMillis();
float currentMs = Math.min( durationMs,Now - startTime );
float x = eaSEOut( currentMs,dx,durationMs );
float y = eaSEOut( currentMs,dy,durationMs );
postTranslate( ( x - old_x ),( y - old_y ) );
center(true,false);
old_x = x;
old_y = y;
if ( currentMs < durationMs ) {
post( this );
}
}
};
post( mFling );
}
private float eaSEOut( float time,float start,float end,float duration){
return end * ( ( time = time / duration - 1 ) * time * time + 1 ) + start;
}
@OverrIDe
protected voID onDraw(Canvas canvas) {
// Todo auto-generated method stub
if(mBitmap!=null && !mBitmap.isRecycled() ){
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && getLayerType() == VIEw.LAYER_TYPE_HARDWARE ){
canvas.drawBitmap(mBitmap,mMatrix,null);
}else{
if( (System.currentTimeMillis()-mLastDraw) > sPaintDelay ){
canvas.drawBitmap(mBitmap,mPaint);
mLastDraw = System.currentTimeMillis();
}
else{
canvas.drawBitmap(mBitmap,null);
removeCallbacks(mRefresh);
postDelayed(mRefresh,sPaintDelay);
}
}
}
}
/**
* @author administrator
*
* 手势缩放监听
*/
class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@OverrIDe
public boolean onScale(ScaleGestureDetector detector) {
// Todo auto-generated method stub
Log.i("ZoomImageVIEw","onScale");
if(detector!=null && detector.isInProgress()){
try{
float targetScale = getScale() * detector.getScaleFactor();
targetScale = Math.min(maxZoom(),Math.max(targetScale,1.0f) );
zoomTo(targetScale,detector.getFocusX(),detector.getFocusY() );
invalIDate();
return true;
}catch(IllegalArgumentException e){
e.printstacktrace();
}
}
return false;
}
};
/**
* @author administrator
*
* 手势识别监听
*/
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@OverrIDe
public boolean onScroll(MotionEvent e1,MotionEvent e2,
float distanceX,float distanceY) {
// Todo auto-generated method stub
Log.i("ZoomImageVIEw","onScroll");
if((e1 != null && e1.getPointerCount() > 1) || (e2 != null && e2.getPointerCount() > 1)
|| (mScaleDetector != null && mScaleDetector.isInProgress())){
return false;
}
if(getScale() > zoomDefault() ) {
removeCallbacks(mFling);
postTranslate(-distanceX,-distanceY);
center(true,false);
}
return true;
}
@OverrIDe
public boolean onFling(MotionEvent e1,float veLocityX,
float veLocityY) {
Log.i("ZoomImageVIEw","onFling");
// Todo auto-generated method stub
if ((e1!=null && e1.getPointerCount() > 1) || ( e2!=null && e2.getPointerCount() > 1))
return false;
if (mScaleDetector.isInProgress())
return false;
try{
float diffX = e2.getX() - e1.getX();
float diffY = e2.getY() - e1.getY();
if ( Math.abs( veLocityX ) > 800 || Math.abs( veLocityY ) > 800 ) {
scrollBy( diffX / 2,diffY / 2,300 );
invalIDate();
}
}catch(NullPointerException e){
e.printstacktrace();
}
return super.onFling( e1,e2,veLocityX,veLocityY );
}
@OverrIDe
public boolean onDoubleTap(MotionEvent e) {
// Todo auto-generated method stub
Log.i("ZoomImageVIEw","onDoubleTap");
if ( getScale() > zoomDefault() ){
zoomTo(zoomDefault());
}
else
zoomTo(zoomDefault()*3,e.getX(),e.getY(),200);
return true;
}
@OverrIDe
public boolean onSingleTapConfirmed(MotionEvent e) {
// Todo auto-generated method stub
Log.i("ZoomImageVIEw","onSingleTapConfirmed");
// 设置点击事件
if(mImagetouchedListener != null) {
mImagetouchedListener.onImagetouched();
return false;
}
return super.onSingleTapConfirmed(e);
}
}
@OverrIDe
public boolean ontouchEvent(MotionEvent event) {
// Todo auto-generated method stub
if(mBitmap != null) {
mScaleDetector.ontouchEvent(event);
if(!mScaleDetector.isInProgress()) {
mGestureDetector.ontouchEvent(event);
}
}
return true;
};
/**
*
* @author administrator
*
* 点击接口
*/
private onImagetouchedListener mImagetouchedListener;
public interface onImagetouchedListener {
voID onImagetouched();
}
public voID setonImagetouchedListener(onImagetouchedListener Listener ){
this.mImagetouchedListener = Listener;
}
xml布局:
复制代码 代码如下:
<com.example.pay.ZoomImageVIEw
androID:scaleType="matrix"
androID:layout_wIDth="fill_parent"
androID:layout_height="fill_parent"
androID:ID="@+ID/zommImageVIEw"
/>
activity调用方法:
复制代码 代码如下:
mZoomImageVIEw = (ZoomImageVIEw) findVIEwByID(R.ID.zommImageVIEw);
mZoomImageVIEw.setimageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));
mZoomImageVIEw.setonImagetouchedListener(new onImagetouchedListener() {
@OverrIDe
public voID onImagetouched() {
// Todo auto-generated method stub
Toast.makeText(MainActivity.this,"11111",Toast.LENGTH_LONG).show();
}
});
以上是内存溢出为你收集整理的Android进阶篇-自定义图片伸缩控件具体实例全部内容,希望文章能够帮你解决Android进阶篇-自定义图片伸缩控件具体实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)