android - 如何将我的自定义 View 添加到布局以匹配父级?

标签 android

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

 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 View 替换 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>

但是没有成功,

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

它显示一个空白字段...

最佳答案

您应该在您的自定义 View 中覆盖 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);
}

关于android - 如何将我的自定义 View 添加到布局以匹配父级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20270727/

相关文章:

java - Android OrientationEventListener 进行多次调用

android - 用自身替换 fragment 不会显示任何内容

android - CPU 处理相机图像

android - 如何在没有第 3 方解析器的情况下在 Kotlin 中解析 JSON?

android - MonkeyTalk Android Detect String Containing\n for Button Tap

android - 使用 IMAGE_CAPTURE Intent 时如何提高相机分辨率

Android 视频 WebView 全屏不工作 - onShowCustomView() 未调用

android - FlashDevelop - Haxe 尝试使用 Android 和 Flash 进行调试

android - 在 Dart/Flutter 中识别一个 2D 列表是否包含一个列表

android - React Native Android 在启用 Debug模式时崩溃