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); }
相关文章推荐
- 下拉刷新,在 ActionBar 上显示效果图
- Android的事件处理之监听与回调
- 布局动画 属性初始 android:animateLayoutChanges
- Android:EditText中切换密码的显示与隐藏
- android 5.1 源码编译adb
- Android中自定义RatingBar
- Android 使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚
- Android 基础:surfaceSurface、SurfaceHolder 及 SurfaceHolder.Callback , 范例说明
- Android的所有权限说明
- Android Studio系列教程四--Gradle基础
- Android Context作用
- Android源码分析之ListView
- Android - 电池状态
- Android的ListView分页功能
- qq登陆
- android 相机图库调用以及裁剪
- android学习笔记1
- Android SQLite 简单使用示例
- android-音乐播放器实现及源码下载(四)
- android-音乐播放器实现及源码下载(三)