1、 在Activity中直接new HelloView 時View的大小。 java
public HelloView(Context context){ android super(context); canvas Log.v("HelloView(Context context)","" + this.getHeight()+ " "+ this.getWidth()); ide } 函數 /** 測試 * 這個是咱們要在XML中初始化用的 this * */ spa public HelloView(Context context,AttributeSet attrs){ orm super(context, attrs); xml Log.v("HelloView(Context context,AttributeSet attrs)","" +this.getHeight()+ " " + this.getWidth()); }
/** * 繪製View * */ protected void onDraw(Canvas canvas){ Log.v("onDraw(Canvas canvas)","" + this.getHeight()+ " " +this.getWidth()); canvas.drawColor(Color.WHITE); myUseBitmapFactory(canvas); myUseBitmapDrawable(canvas); myUseInputStreamandBitmapDrawable(canvas);
}
|
protected void onMeasure(int widthMeasureSpec, intheightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); Log.v("onMeasure","" + this.getHeight()+ " " +this.getWidth()); } |
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <view class="com.fxhy.stady.HelloView" android:layout_width="50dip" android:layout_height="120dip" /> </LinearLayout> |
/** * 使用自定義的View * */ public class MainActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);// 使用自定義的View } } |