变身小甜甜 2015-07-19 14:41 采纳率: 0%
浏览 1825

android 自定义控件 onMeasure

android 自定义控件SurfaceView

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int originalWidth = MeasureSpec.getSize(widthMeasureSpec);//800
int calculatedHeight = originalWidth * aspectHeight / aspectWidth;//1095
super.onMeasure(
MeasureSpec.makeMeasureSpec(originalWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(calculatedHeight, MeasureSpec.EXACTLY));
}

    对onMeasure方法表示不理解,
    上述代码中的originalWidth,calculatedHeight,是指控件本身还是真个屏幕?通过debug,发现打印出来的值都对不上啊?



     <view
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    class="com.sensetime.facesign.util.DefinedSurfaceView"
    android:id="@+id/surfaceView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />



    package com.sensetime.facesign.util;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.SurfaceView;

import com.sensetime.facesign.R;

/**

  • Created by SenseTime on 2015/1/10.
    */
    public class DefinedSurfaceView extends SurfaceView {

    private int aspectWidth = 480;
    private int aspectHeight = 640;
    private Context mContext;

    public DefinedSurfaceView(Context context) {
    super(context);
    mContext = context;
    }

    public DefinedSurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    setCustomAttributes(attrs);
    }

    public DefinedSurfaceView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mContext = context;
    setCustomAttributes(attrs);
    }

    private void setCustomAttributes(AttributeSet attrs) {
    TypedArray a = mContext.obtainStyledAttributes(attrs,
    R.styleable.definedlayout);
    aspectWidth = a.getDimensionPixelSize(
    R.styleable.definedlayout_target_width, 480);//aspectWidth == 480 //??
    aspectHeight = a.getDimensionPixelSize(
    R.styleable.definedlayout_target_height, 640);//aspectHeight == 640 //??

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int originalWidth = MeasureSpec.getSize(widthMeasureSpec);//800
    int calculatedHeight = originalWidth * aspectHeight / aspectWidth;//1095
    super.onMeasure(
    MeasureSpec.makeMeasureSpec(originalWidth, MeasureSpec.EXACTLY),
    MeasureSpec.makeMeasureSpec(calculatedHeight, MeasureSpec.EXACTLY));
    }
    }

  • 写回答

1条回答 默认 最新

  • danielinbiti 2015-07-19 14:52
    关注
     如果是SurfaceView,则不能根据 onMeasure获取高宽
    需要用
    SurfaceHolder.Callback的surfaceCreated才能获取实际的高度和宽度
    public void surfaceCreated(SurfaceHolder holder) {
      // TODO Auto-generated method stub
    Log.d("TEST","Width="+ this.getWidth()+"|Height="+this.getHeight());
    
     }
    
     view可以用onMeasure
    
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号