Android 以编程方式将按钮添加到具有自定义视图的布局中会隐藏该按钮

Android 以编程方式将按钮添加到具有自定义视图的布局中会隐藏该按钮,android,android-layout,android-custom-view,Android,Android Layout,Android Custom View,我有一个自定义视图,必须用手指在其上绘制(如FingerPaint示例中所示) 我试图通过编程方式在该视图下添加一个按钮,方法是创建一个LinearLayout并向其添加所需的视图 但是,我看不到按钮本身 我知道这是一个问题,因为我实现了onMeasure,因为如果我恢复到默认值,按钮会显示,但视图不会显示 主要活动类 public class FingerPaintActivity extends Activity implements ColorPickerDialog.OnColorCha

我有一个自定义视图,必须用手指在其上绘制(如FingerPaint示例中所示)

我试图通过编程方式在该视图下添加一个按钮,方法是创建一个LinearLayout并向其添加所需的视图

但是,我看不到按钮本身

我知道这是一个问题,因为我实现了onMeasure,因为如果我恢复到默认值,按钮会显示,但视图不会显示

主要活动类

public class FingerPaintActivity extends Activity implements ColorPickerDialog.OnColorChangedListener
{

    MyView mv;
    AlertDialog dialog;
    ArrayList<Integer> colorList;
    private LinearLayout.LayoutParams params;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);

        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        layout.setLayoutParams(params);

        Button submitButton = new Button(this);

        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        submitButton.setLayoutParams(params);
        submitButton.setGravity(Gravity.CENTER);
        submitButton.setText("Done");

        mv = new MyView(this);

        params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);

        mv.setLayoutParams(params);
        mv.setDrawingCacheEnabled(true);
        mv.setAdjustViewBounds(true);

        layout.addView(mv);
        layout.addView(submitButton);

        setContentView(layout);

        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(6);

    }
    private Paint mPaint;

    @Override
    public void colorChanged(int color)
    {
        mPaint.setColor(color);
        Log.i("COLOR", color + "");
    }

    public class MyView extends ImageView
    {

        private Bitmap mBitmap, originalBitmap, originalNonResizedBitmap;
        private Canvas mCanvas;
        private Path mPath;
        private Paint mBitmapPaint;
        Context context;
        private BitmapFactory.Options options = new BitmapFactory.Options();

        public MyView(Context c)
        {
            super(c);
            context = c;
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
            options.inMutable = true;

            originalNonResizedBitmap = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.amsler_boundary, options));

            originalNonResizedBitmap = getResizedBitmap(originalNonResizedBitmap);

            originalBitmap = originalNonResizedBitmap.copy(Bitmap.Config.ARGB_8888, true);
            mBitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);

            mCanvas = new Canvas(mBitmap);

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

        @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);

            // mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);

        }
        @Override
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);

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

            canvas.drawPath(mPath, mPaint);

        }

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

        private void touch_start(float x, float y)
        {

            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;

        }
        private void touch_move(float x, float y)
        {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
            {
                mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
                mX = x;
                mY = y;
            }
        }
        private void touch_up()
        {
            mPath.lineTo(mX, mY);
            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            mPath.reset();
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
            // mPaint.setMaskFilter(null);
        }

        @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();
                    // Need to set null or clogs the Paint
                    mPaint.setXfermode(null);
                    invalidate();

                    break;
            }
            return true;
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {

            int newWidthMeasure = MeasureSpec.makeMeasureSpec(mBitmap.getWidth(), MeasureSpec.EXACTLY);
            int newHeightMeasure = MeasureSpec.makeMeasureSpec(mBitmap.getHeight(), MeasureSpec.EXACTLY);

            setMeasuredDimension(newWidthMeasure, newHeightMeasure);

            // super.setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
            // MeasureSpec.getSize(heightMeasureSpec));
            // super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
        public void clearScreen()
        {
            mCanvas.drawColor(Color.TRANSPARENT);
            mCanvas.drawBitmap(originalBitmap, 0, 0, mBitmapPaint);
            invalidate();
        }

        public Rect getCoordinateRect()
        {
            return new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
        }

    }

    public Bitmap getResizedBitmap(Bitmap bm)
    {
        int width = bm.getWidth();
        int height = bm.getHeight();

        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        int newDimen = size.x;

        float scaleWidth = ((float) newDimen) / width;
        float scaleHeight = ((float) newDimen) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        return resizedBitmap;

    }
公共类FingerPaintActivity扩展活动实现ColorPickerDialog.OnColorChangedListener
{
MyView mv;
警报对话框;
ArrayList颜色列表;
private LinearLayout.LayoutParams参数;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout布局=新的LinearLayout(本);
params=新的LinearLayout.LayoutParams(LayoutParams.MATCH\u父级,LayoutParams.MATCH\u父级);
布局。setLayoutParams(参数);
按钮提交按钮=新按钮(此按钮);
params=新的LinearLayout.LayoutParams(LayoutParams.MATCH\u父级,LayoutParams.WRAP\u内容);
submitButton.setLayoutParams(参数);
submitButton.setGravity(重心);
submitButton.setText(“完成”);
mv=新的MyView(本);
params=新的LinearLayout.LayoutParams(LayoutParams.WRAP\u内容,LayoutParams.MATCH\u父项);
mv.setLayoutParams(参数);
mv.setDrawingCacheEnabled(真);
mv.setAdjustViewBounds(真);
布局。添加视图(mv);
布局。添加视图(提交按钮);
setContentView(布局);
mPaint=新油漆();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(油漆、样式、笔划);
mPaint.setStrokeJoin(油漆.连接.圆形);
mPaint.setStrokeCap(油漆盖圆形);
mPaint.设定行程宽度(6);
}
私人油漆;
@凌驾
公共空白颜色已更改(整型颜色)
{
mPaint.setColor(颜色);
Log.i(“COLOR”,COLOR+);
}
公共类MyView扩展了ImageView
{
私有位图mBitmap、originalBitmap、originalnonresizedbramap;
私人帆布mCanvas;
专用路径mPath;
私人油漆;
语境;
私有BitmapFactory.Options=new BitmapFactory.Options();
公共MyView(上下文c)
{
超级(c);
上下文=c;
mPath=新路径();
mBitmapPaint=新油漆(油漆抖动标志);
options.inMutable=true;
originalNonResizedBitmap=Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.amsler_边界,选项));
originalNonResizedBitmap=getResizedBitmap(originalNonResizedBitmap);
originalBitmap=originalNonResizedBitmap.copy(Bitmap.Config.ARGB_8888,true);
mBitmap=originalBitmap.copy(Bitmap.Config.ARGB_8888,true);
mCanvas=新画布(mBitmap);
}
公共MyView(上下文、属性集属性)
{
超级(上下文,attrs);
}
@凌驾
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh)
{
super.onSizeChanged(w,h,oldw,oldh);
//mBitmap=Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
//mBitmap=Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
mCanvas=新画布(mBitmap);
}
@凌驾
受保护的void onDraw(画布)
{
super.onDraw(帆布);
drawBitmap(mBitmap,0,0,mbitMapPoint);
画布绘制路径(mPath,mPaint);
}
私人浮动mX,我的;
专用静态最终浮动接触公差=4;
专用无效触摸启动(浮动x、浮动y)
{
mPath.reset();
移动到(x,y)的速度;
mX=x;
mY=y;
}
私有无效触摸移动(浮动x、浮动y)
{
float dx=Math.abs(x-mX);
float dy=Math.abs(y-mY);
如果(dx>=接触公差| | dy>=接触公差)
{
兆帕四分之一秒(mX,mY,(x+mX)/2,(y+mY)/2);
mX=x;
mY=y;
}
}
私人空间修补()
{
mPath.lineTo(mX,mY);
//将路径提交到我们的屏幕外
mCanvas.drawPath(mPath,mPaint);
//杀了它,这样我们就不会重复抽签了
mPath.reset();
mPaint.setXfermode(新的PorterDuffXfermode(PorterDuff.Mode.SCREEN));
//mPaint.setMaskFilter(null);
}
@凌驾
公共布尔onTouchEvent(运动事件)
{
float x=event.getX();
float y=event.getY();
开关(event.getAction())
{
case MotionEvent.ACTION\u DOWN:
触摸启动(x,y);
使无效();
打破
case MotionEvent.ACTION\u移动:
触摸移动(x,y);
使无效();
打破
case MotionEvent.ACTION\u UP:
润色;
//需要设置空值或阻塞油漆
mPaint.setXfermode(null);
使无效();
打破
}
返回true;
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级)
{
int newWidthMeasure=MeasureSpec.makeMeasureSpec(mBitmap.getWidth(),MeasureSpec.justice);
int newHeightMeasure=MeasureSpec.makeMeasureSpec(mBitmap.getHeight(),MeasureSpec.justice);
设置测量尺寸(新宽度测量,ne