獲取不到 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窗口上時(shí)執(zhí)行 ?*?@see?android.widget.ImageView#onAttachedToWindow() ?*/ @Override protected?void?onAttachedToWindow()?{ //?TODO?Auto-generated?method?stub super.onAttachedToWindow(); /* ?*?注冊(cè)?onGloalLayout ?*/ getViewTreeObserver().addOnGlobalLayoutListener(this);//??? } /* ?*?view?從窗口上移除時(shí)執(zhí)行 ?*?@see?android.widget.ImageView#onDetachedFromWindow() ?*/ @SuppressWarnings("deprecation") @Override protected?void?onDetachedFromWindow()?{ //?TODO?Auto-generated?method?stub super.onDetachedFromWindow(); getViewTreeObserver().removeGlobalOnLayoutListener(this); /* ?*?好像應(yīng)該使用這個(gè) ?*/ // 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; //將圖片移動(dòng)至控件中心 int?dx=getWidth()/2-dw/2; int?dy=getHeight()/2-dh/2; mScaleMatrix.postTranslate(dx,?dy); mScaleMatrix.postScale(mInitScale,?mInitScale,width/2,height/2); //設(shè)置圖片縮放 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 ,也就是說(shuō)獲取不到 Drawable ,我明明有加src了????
求助大神
2017-08-12
兩個(gè)參數(shù)的構(gòu)造方法調(diào)用三個(gè)構(gòu)造參數(shù)構(gòu)造方法時(shí)寫錯(cuò)了
應(yīng)該是
public?ZoomImageView(Context?context,?AttributeSet?attrs)?{
????????this(context, attrs,0);
????????//?TODO?Auto-generated?constructor?stub
????}