Android:在不遮挡一行按钮的情况下绘制画布

Android:在不遮挡一行按钮的情况下绘制画布,android,canvas,Android,Canvas,我的问题可能很简单——有很多应用程序可以完成我想要做的事情 我有一个带有一行按钮的相对布局,然后在下面我想在画布上绘制 我的问题是画布画在按钮上,或者代替按钮,所以我最终得到的只是一个形状 这是我的代码: package com.android.phil.graphtoggle; import android.app.Activity; import android.content.Context; import android.graphics.Canvas

我的问题可能很简单——有很多应用程序可以完成我想要做的事情

我有一个带有一行按钮的相对布局,然后在下面我想在画布上绘制

我的问题是画布画在按钮上,或者代替按钮,所以我最终得到的只是一个形状

这是我的代码:


    package com.android.phil.graphtoggle;

    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.drawable.ShapeDrawable;
    import android.graphics.drawable.shapes.OvalShape;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageButton;

    public class MainActivity extends Activity 
    {
        public int graph_toggle = 0;
        public int data_toggle=0;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            final ImageButton graph_toggle_button = (ImageButton) findViewById(R.id.graph_toggle);
            final ImageButton graph_settings_button = (ImageButton) findViewById(R.id.graph_type);
            final ImageButton data_toggle_button = (ImageButton) findViewById(R.id.data_toggle);

            CustomDrawableView mCustomDrawableView;

            mCustomDrawableView = new CustomDrawableView(this);
            setContentView(mCustomDrawableView);

            graph_toggle_button.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View arg0) 
                {
                    if (graph_toggle==2)
                    {
                        graph_toggle=0;
                    }
                    else
                    {
                        graph_toggle++;
                    }

                    if (graph_toggle==0)
                    {
                        graph_settings_button.setImageResource(R.drawable.close);
                    }
                    if (graph_toggle==1)
                    {
                        graph_settings_button.setImageResource(R.drawable.ohlc_bars);
                    }
                    if(graph_toggle==2)
                    {
                        graph_settings_button.setImageResource(R.drawable.candles);
                    }               
                }         
            });
            data_toggle_button.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View arg0) 
                {
                    if (data_toggle==2)
                    {
                        data_toggle=0;
                    }
                    else
                    {
                        data_toggle++;
                    }

                    if (data_toggle==0)
                    {
                        data_toggle_button.setImageResource(R.drawable.ohlc_bars_daily);
                    }
                    if (data_toggle==1)
                    {
                        data_toggle_button.setImageResource(R.drawable.ohlc_bars_weekly);
                    }
                    if(data_toggle==2)
                    {
                        data_toggle_button.setImageResource(R.drawable.ohlc_bars_monthly);
                    }               
                }         
            });
        }
        public class CustomDrawableView extends View 
        {
            private ShapeDrawable mDrawable;

            public CustomDrawableView(Context context) 
            {
                super(context);

                int x = 10;
                int y = 100;
                int width = 300;
                int height = 50;

                mDrawable = new ShapeDrawable(new OvalShape());
                mDrawable.getPaint().setColor(0xff74AC23);
                mDrawable.setBounds(x, y, x + width, y + height);  
           }

            protected void onDraw(Canvas canvas)
            {
                    mDrawable.draw(canvas);
            }
        }
    }
这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
android:layout_width="fill_parent" 
android:id="@+id/relativeLayout1" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent">
    <ImageButton 
    android:layout_height="40dip" 
    android:layout_width="40dip" 
    android:id="@+id/graph_toggle" 
    android:src="@drawable/graph_toggle" 
    android:layout_alignParentLeft="true"
    ></ImageButton>
    <ImageButton 
    android:layout_height="40dip"  
    android:layout_width="40dip"  
    android:id="@+id/graph_type" 
    android:src="@drawable/close" 
    android:layout_toRightOf="@+id/graph_toggle"
    ></ImageButton>
    <ImageButton 
    android:layout_height="40dip" 
    android:layout_width="40dip" 
    android:id="@+id/data_toggle" 
    android:src="@drawable/ohlc_bars_daily" 
    android:layout_toRightOf="@+id/graph_type"
    ></ImageButton>
    <ImageButton 
    android:layout_height="40dip" 
    android:layout_width="40dip" 
    android:id="@+id/data_toggle1" 
    android:src="@drawable/ohlc_bars_daily" 
    android:layout_toRightOf="@+id/data_toggle"
    ></ImageButton>
    <ImageButton 
    android:layout_height="40dip" 
    android:layout_width="40dip" 
    android:id="@+id/data_toggle2" 
    android:src="@drawable/ohlc_bars_daily" 
    android:layout_toRightOf="@+id/data_toggle1"
    ></ImageButton>
    <ImageButton 
    android:layout_height="40dip" 
    android:layout_width="40dip" 
    android:id="@+id/data_toggle3" 
    android:src="@drawable/ohlc_bars_daily" 
    android:layout_toRightOf="@+id/data_toggle2"
    ></ImageButton>
    <ImageButton 
    android:layout_height="40dip" 
    android:layout_width="40dip" 
    android:id="@+id/data_toggle4" 
    android:src="@drawable/ohlc_bars_daily" 
    android:layout_toRightOf="@+id/data_toggle3"
    ></ImageButton>
</RelativeLayout>


创建一个扩展framelayout类并覆盖onDraw函数的类
在那里,您可以绘制所需的所有内容,它的行为将与任何其他视图类似

调用以下命令时,您将使用自定义视图替换整个xml布局:

setContentView(mCustomDrawableView);
您应该将自定义视图添加到布局中,例如:

RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.relativeLayout1);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(/*up to you*/);
// add here other layout params rules to make your
// custom view stay below the buttons

mCustomDrawableView.setLayoutParams(lp);
mainLayout.addView(mCustomDrawableView);

但您也应该考虑添加自定义视图这种类型的构造函数:

public CustomDrawableView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // add other init stuff
}

因此,您可以在xml中使用自定义视图,从而更容易指定布局参数,因为在代码中执行此操作对于
RelativeLayout

来说可能会很无聊,它尝试了RelativeLayout位,但不了解如何将可绘制的应用于它。不过我知道你的例子。谢谢你的帮助。我已经尝试过了,但mainLayout.add(mCustomDrawableView)出现了一个错误;它想“向mainLayout添加Cast”有什么想法吗?@Phil oops,对不起,它是
addView(…)
。好的,现在我遇到了一个问题:CustomDrawableView mcCustomDrawableView;它想初始化变量,但我认为这会破坏脚本。对不起,如果我感到疼痛。提前谢谢。Phil@Phil我想你需要发布一个新问题并粘贴代码,我不能从这里说出什么问题。