您的位置:首页 > 移动开发 > Android开发

android 控件中layout_width,layout_height与onMeasure(int widthMeasureSpec, int heightMeasureSpec)

2015-08-16 14:31 701 查看
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<com.example.propertyanimsecdemo.BezierCurveView
android:id="@+id/bezierView"
android:background="#cccccc"
android:layout_width="440dp"
android:layout_height="320dp"/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

</FrameLayout>
package com.example.propertyanimsecdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class BezierCurveView extends View{

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

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

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.v("ee", "width = " + MeasureSpec.getSize(widthMeasureSpec) + "| height = " + MeasureSpec.getSize(heightMeasu               reSpec));
}

}




这样的结果是因为dm.densityDpi=160,dm.density=1.0

DisplayMetrics dm = getResources().getDisplayMetrics();

w_screen = dm.widthPixels;

h_screen = dm.heightPixels;

Log.d("ee", "w_screen:"+w_screen+" h_screen:"+h_screen+" "+dm.densityDpi);

看下面:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<com.example.propertyanimsecdemo.BezierCurveView
android:id="@+id/bezierView"
android:background="#cccccc"
android:layout_width="320dp"
android:layout_height="320dp"/>
</FrameLayout>


DisplayMetrics dm = getResources().getDisplayMetrics();
w_screen = dm.widthPixels;
h_screen = dm.heightPixels;
Log.d("ee", "w_screen:"+w_screen+"    h_screen:"+h_screen+"    "+dm.densityDpi);
int w=Utils.dip2px(this, 320);
int h=Utils.dip2px(this, 320);
Log.d("ee", "w:"+w+"    h:"+h);


public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.v("ee", "width = " + MeasureSpec.getSize(widthMeasureSpec) + "| height = " + MeasureSpec.getSize(heightMeasureSpec));
}

public void onDraw(Canvas canvas){
canvas.drawColor(Color.WHITE);
path.reset();
path.moveTo(0,0);
path.cubicTo(getMeasuredWidth(),0, 0,getMeasuredHeight(),getMeasuredWidth(),getMeasuredHeight());
//		path.moveTo(mPoints[0].x,mPoints[0].y);
//		path.cubicTo(mPoints[1].x, mPoints[1].y, mPoints[2].x, mPoints[2].y,mPoints[3].x, mPoints[3].y);
Log.d("ee", getHeight()+"    "+getWidth()+"      getMeasuredWidth:"+getMeasuredWidth()+"      getMeasuredHeight:"+getMeasuredHeight());
canvas.drawPath(path, paint);
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: