如何将我的自定义视图添加到布局以便匹配父级?

时间:2013-11-28 16:20:28

标签: android

我有以下自定义视图(基本上是包含图像的视图,在该图像上可以绘制矩形)。

 public class MyView extends View {

public Bitmap mBitmap;
public Canvas mCanvas;
public Rect mPath;
public Paint mBitmapPaint;
public Paint mPaint;

public MyView(Context c) {
    super(c);
    Log.e("","call first constructor");
    mPath = new Rect();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(0xFFFF0000);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(3);
}

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

public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.b6p1).copy(Bitmap.Config.ARGB_8888, true);
    mCanvas.drawBitmap(bitmap, 0, 0, mBitmapPaint);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(0xFFAAAAAA);

    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

    canvas.drawRect(mPath, mPaint);
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {

    mX = x;
    mY = y;
}

private void touch_move(float x, float y) {

    float left;
    float right;
    float bottom;
    float top;
    if (x < mX) {
        left = x;
        right = mX;
    } else {
        left = mX;
        right = x;
    }

    if (y < mY) {
        top = y;
        bottom = mY;
    } else {
        top = mY;
        bottom = y;
    }

    mPath = new Rect((int) left, (int) top, (int) right, (int) bottom);

}

private void touch_up() {
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        touch_start(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        touch_move(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_UP:
        touch_up();
        invalidate();
        break;
    }
    return true;
}
 }

现在我想将它添加到我的布局中, 布局如下, 我想用我的MyView视图替换ImageView。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.8"
        android:orientation="vertical"
        android:weightSum="1" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.9" >

            <HorizontalScrollView
                android:id="@+id/horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <ScrollView
                    android:id="@+id/vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <ImageView
                        android:id="@+id/image"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
                    </ImageView>
                </ScrollView>
            </HorizontalScrollView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.1"
            android:background="@android:color/darker_gray" >

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

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

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.2"
        android:background="@android:color/darker_gray"
        android:orientation="vertical" >

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

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

    </LinearLayout>
</LinearLayout>

</LinearLayout>

我试过了:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.8"
        android:orientation="vertical"
        android:weightSum="1" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.9" >

            <HorizontalScrollView
                android:id="@+id/horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <ScrollView
                    android:id="@+id/vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <com.example.test.MyView
                        android:id="@+id/image"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                      </ScrollView>
            </HorizontalScrollView>
        </LinearLayout>

但没有成功,

可以绘制的图像不显示......

显示空白字段......

1 个答案:

答案 0 :(得分:3)

您应该在自定义视图中覆盖onMeasure()。请执行以下操作:

@Override
public void onMeasure(int widthSpecs, int heightSpecs) {
    super.onMeasure(widthSpecs, heightSpecs);
    setMeasuredDimension(mDisplayUtils.getScreenWidth(), mDisplayUtils.getScreenHeight());
}

或您想要的任何值。

我能想到的另一件事是你在主构造函数上初始化你的变量。这样做:

public MyView(Context c) {
    super(c);
    init();
}

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

public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init(){
    Log.e("","call first constructor");
    mPath = new Rect();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(0xFFFF0000);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(3);
}