第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

獲取不到 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了????

求助大神

正在回答

1 回答

兩個(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

????}


0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
打造個(gè)性的圖片預(yù)覽與多點(diǎn)觸控
  • 參與學(xué)習(xí)       23116    人
  • 解答問題       74    個(gè)

實(shí)現(xiàn)圖片自由縮放與多點(diǎn)觸控效果,對(duì)Matrix以及手勢(shì)檢測(cè)API深入學(xué)習(xí)

進(jìn)入課程

獲取不到 Drawable

我要回答 關(guān)注問題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)