获取不到 Drawable
ZoomImageView.java
package com.example.view;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
public class ZoomImageView extends ImageView implements OnGlobalLayoutListener{
private boolean mOnce=false;
private float mInitScale; //初始化缩放值
private float mMidScale; //双击缩放值
private float mMaxScale; //最大值
private Matrix mScaleMatrix;
public ZoomImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
// init
mScaleMatrix=new Matrix();
setScaleType(ScaleType.MATRIX);
}
public ZoomImageView(Context context, AttributeSet attrs) {
this(context, null,0);
// TODO Auto-generated constructor stub
}
public ZoomImageView(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
/*
* view 从attach窗口上时执行
* @see android.widget.ImageView#onAttachedToWindow()
*/
@Override
protected void onAttachedToWindow() {
// TODO Auto-generated method stub
super.onAttachedToWindow();
/*
* 注册 onGloalLayout
*/
getViewTreeObserver().addOnGlobalLayoutListener(this);//???
}
/*
* view 从窗口上移除时执行
* @see android.widget.ImageView#onDetachedFromWindow()
*/
@SuppressWarnings("deprecation")
@Override
protected void onDetachedFromWindow() {
// TODO Auto-generated method stub
super.onDetachedFromWindow();
getViewTreeObserver().removeGlobalOnLayoutListener(this);
/*
* 好像应该使用这个
*/
// getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
/**
* 获取 ImageView 加载完成的图片
*/
@Override
public void onGlobalLayout() {
Log.i("MYAPP", "onGlobalLayout");
if (!mOnce) {
Log.i("MYAPP", "monce");
//得到空间的宽和高
int width=getWidth();
int height=getHeight();
Log.i("MYAPP", "width="+width+",height="+height);
//得到图片,图片的宽和高
Drawable d=getDrawable();
Log.i("MYAPP", "d="+d);
if (d==null) {
return;
}
int dw=d.getIntrinsicWidth();
int dh=d.getIntrinsicHeight();
float scale=1.0f;
/*
* 如果图片宽度大于控件宽度,但高度小于空间高度,缩小
*/
if (dw>width&&dh<height) {
scale=width*1.0f/dw;
}
/*
* 如果图片宽度大于控件宽度,但高度小于空间高度,缩小
*/
if (dh>height&&dw<width) {
scale=height*1.0f/dh;
}
if ((dh>height&&dw>width)||dw<width&&dh<height) {
scale=Math.min(width*1.0f/dw, height*1.0f/dh);
}
/*
* 得到缩放值
*/
mInitScale=scale;
mMaxScale=mInitScale*4;
mMidScale=mInitScale*2;
//将图片移动至控件中心
int dx=getWidth()/2-dw/2;
int dy=getHeight()/2-dh/2;
mScaleMatrix.postTranslate(dx, dy);
mScaleMatrix.postScale(mInitScale, mInitScale,width/2,height/2);
//设置图片缩放
setImageMatrix(mScaleMatrix);
Log.i("MYAPP", "dx="+dx+",dy="+dy+"mInitScale="+mInitScale);
mOnce=true;
}
}
}main_activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.view.ZoomImageView android:id="@+id/zoomImageView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="matrix" android:src="@drawable/img" /> </LinearLayout>
logcat 显示出 d==null ,也就是说获取不到 Drawable ,我明明有加src了啊??
求助大神