Android 分层图像加画布

Android 分层图像加画布,android,android-layout,android-relativelayout,android-canvas,Android,Android Layout,Android Relativelayout,Android Canvas,我正试着把两张画叠在一起。上面的那个是透明的。在它们上面,我试图添加一个透明的画布,这样用户可以绘制、键入他们需要做的任何事情,并且底部的两个图像是可见的 所以我可以让这两个图像显示出来,或者只是在画布上。每次我试图显示画布时,它似乎位于覆盖它们的两幅图像的[p]上 这是我到目前为止得到的 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s

我正试着把两张画叠在一起。上面的那个是透明的。在它们上面,我试图添加一个透明的画布,这样用户可以绘制、键入他们需要做的任何事情,并且底部的两个图像是可见的

所以我可以让这两个图像显示出来,或者只是在画布上。每次我试图显示画布时,它似乎位于覆盖它们的两幅图像的[p]上

这是我到目前为止得到的

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(new MyView(this));

..... some code to set up the paint
}

private Paint       mPaint;
private MaskFilter  mEmboss;
private MaskFilter  mBlur;

public class MyView extends View {

        private static final float MINP = 0.25f;
        private static final float MAXP = 0.75f;

        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;                

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

            // Creating a new relative layout add the definition again.       
            RelativeLayout relativeLayout = new RelativeLayout(TwoPicksOnEachOther.this);                   
            // Setting the orientation to vertical         
            ////relativeLayout.setOrientation(LinearLayout.VERTICAL);                   

            // Creating Fish  image       
            final ImageView iv = new ImageView(TwoPicksOnEachOther.this);         
            iv.setImageResource(R.drawable.fish2);
            // relative layout parameters
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(     
                    RelativeLayout.LayoutParams.FILL_PARENT, 
                    RelativeLayout.LayoutParams.FILL_PARENT);        
            //iv.setId(1);                          
            relativeLayout.addView(iv,lp);        

            // Creating transparent image with boat.
            final ImageView iv2 = new ImageView(TwoPicksOnEachOther.this);
            iv2.setImageResource(R.drawable.ctdeasytwo);
            //iv2.setId(2);
            RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(     
                    RelativeLayout.LayoutParams.FILL_PARENT, 
                    RelativeLayout.LayoutParams.FILL_PARENT);
            relativeLayout.addView(iv2,lp2);     

            mBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888); 
            //mBitmap = BitmapFactory.decodeResource(getResources(),
            //        R.drawable.ctdeasytwo);                      
            mCanvas = new Canvas(mBitmap);
           // mCanvas.drawBitmap(mBitmap, 10, 10, null);
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);

        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.TRANSPARENT);

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

            canvas.drawPath(mPath, mPaint);
        }

................... More code to manage touch events.
现在我怀疑画布不是相对布局的一部分。这可能是问题吗?如果是的话,我不能做relativeLayout.addView(mCanvas,lp)

有什么建议吗?

你可以参考这个
对不起,我不得不等了八个小时才回答我自己的问题


我能够解决这个问题。我创建了一个类

public class newClass extends View { 
public newClass (Context context){ super(context); } 
... 
@Override protected void onDraw(Canvas canvas) { 
canvas.drawColor(Color.TRANSPARENT); } 
然后我要做的就是

final newClass myCanvas = new newClass (this); 

relativeLayout.addView(myCanvas,lp2); 

工作正常…

我通过执行以下操作(仅显示部分代码),成功地完成了类似的操作(在透明画布上绘制背景位图):


希望能有所帮助。

亲爱的hotveryspicy,感谢您的输入,但我可能被误解了。我可以管理绘图和所有触摸事件。困难在于将三个对象放在彼此的顶部。我的canavas是thransparent。下面的图像都没有被更改。画布填充为透明画布。drawColor(Color.TRANSPARENT);我能够解决问题。我创建了一个类。公共类newClass扩展视图{public newClass(Context Context){super(Context);}…@Override protected void onDraw(Canvas Canvas){Canvas.drawColor(Color.TRANSPARENT);}然后我所要做的就是最终的newClass myCanvas=newClass(this);relativeLayout.addView(我的画布,lp2);效果很好……我必须等17个小时才能接受我自己的答案……我不太了解这些规则……请阅读以了解为什么会是这样。
public class MyView extends View {
    public MyView(Context c) {
        super(c);
        Bitmap tmpBitmap;

        // Create the background bitmap and convert it to a drawable object
        mBackBitmap = Bitmap.createBitmap(tmpBitmap, 0, 0, tmpBitmap.getWidth(), tmpBitmap.getHeight(), aMatrix, false);
        mBackBitmapDrawable = new BitmapDrawable(mBackBitmap);
        // Set the drawable object as the background
        setBackgroundDrawable(mBackBitmapDrawable);
        // Create an empty bitmap for the canvas
        mBitmap = Bitmap.createBitmap(tmpBitmap.getWidth(), tmpBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //Log.d("MyView", "onDraw");
        // Use transparent background
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
    }
}