Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android從零單排之獲取ImageView的寬高為0?

Android從零單排之獲取ImageView的寬高為0?

編輯:關於Android編程

問題描述

說起來我也夠菜的!⊙﹏⊙b汗

今天搞了一個關於圖片的demo,想動態的改變一張圖片的大小和margin值。但是在activity中獲取imageview的寬度和高度的時候,總是0。哎,我tm就是搞不懂了,怎麼能是0呢?

	imageView1 = (ImageView) findViewById(R.id.id_img1);
	int width = imageView1.getWidth();
	int height = imageView1.getHeight();

分析原因

起初懷疑是我代碼寫的有問題,可能寫錯了,可是反復檢查和實驗了幾次,還TM是0。蛋疼!後來上網一查,發現了新大陸了。

在onCreate方法中,控件其實還沒有畫好,也就是說當onCreate方法執行完了之後,才開始繪制控件,所以在onCreate方法中獲取width和height返回的值是0。但是我又想onResume方法是不是可以呢?後來發現還是不行!我自定義了ImageView,然後打印量log日志,發現果然是這樣,onCreate和onResume方法執行完了之後,才開始執行控件的測量(onMeasure)和繪制(onDraw)方法。

MyImageView.java---自定義ImageView類:

public class MyImageView extends ImageView {

	public MyImageView(Context context) {
		super(context);
	}

	public MyImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		Log.i("imageview", "onMeasure...");
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Log.i("imageview", "onDraw...");
	}
}
activity類:

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_demo3);
		Log.i("imageview", "onCreate.....");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.i("imageview", "onResume.....");
	}
打印出來的LOG日志如下:

\

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPGgyPr3ivvaw7LeoPC9oMj4KPHA+ICAgILzIyLtvbkNyZWF0Zbe9t6jW0LK70NCjrNOmuMPT0Mbky/u1xLe9t6jBy6Oh09rKx7TTzfjJz9XStb3By8jnz8K8uNbWt723qKOhPC9wPgo8cD4gICAgtdrSu9bWt723qKO6yrnTw0JpdG1hcEZhY3RvcnmhozwvcD4KPHA+PHByZSBjbGFzcz0="brush:java;">Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.songjiang); Log.i("imageview", "width: " + bitmap.getWidth()); Log.i("imageview", "height: " + bitmap.getHeight());
第二種方法: 使用MeasureSpec.makeMeasureSpec方法。

int w = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		img.measure(w, h);
		Log.i("imageview2", "width: " + img.getMeasuredWidth());
		Log.i("imageview2", "height: " + img.getMeasuredHeight());
第三種方法:使用ViewTreeObserver.addOnPreDrawListener方法。

ViewTreeObserver vto = img.getViewTreeObserver();
		vto.addOnPreDrawListener(new OnPreDrawListener() {
			@Override
			public boolean onPreDraw() {
				int height = img.getMeasuredHeight();
				int width = img.getMeasuredWidth();
				Log.i("imageview3", "width: " + width);
				Log.i("imageview3", "height: " + height);
				return true;
			}
		});

第四種方法:使用ViewTreeObserver.addOnGlobalLayoutListener方法。

ViewTreeObserver vto = img.getViewTreeObserver();
		vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			@Override
			public void onGlobalLayout() {
				img.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				Log.i("imageview4", "width: " + img.getWidth());
				Log.i("imageview4", "height: " + img.getHeight());
			}
		});

注意:方法三會調用3次。

\

在代碼中動態改變ImageView的寬高就很簡單了!

DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int desityDpi = metrics.densityDpi;
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hi);
		int width = bitmap.getWidth() * 160 / desityDpi;
		int height = bitmap.getHeight() * 160 / desityDpi;
		LayoutParams params = imageView1.getLayoutParams();
		params.width = width;
		params.height = height;
		imageView1.setLayoutParams(params);

下載

demo代碼此處此處下載!(隨後上傳!)



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved