2011-04-27 76 views
0

我正试图在彼此之上放置两张图片。最重要的是透明的。在他们之上,我试图添加一个透明的画布,以便用户可以绘制,键入,无论他们需要做什么,底部的两个图像都可见。分层图像加上画布

所以我可以得到两个图像显示或只是画布。每次我尝试显示画布时,似乎都会覆盖覆盖它们的两幅图像。

这里是我迄今为止..

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

有什么建议吗?

回答

1

对不起不得不等待八个小时回答我的问题。

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

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

正常工作......

+0

我必须等待17个小时才能接受我自己的答案...... Don'y很好地说明了这些规则... – George 2011-04-29 00:30:13

+0

阅读[this] (http://meta.stackexchange.com/questions/6044/why-must-i-wait-2-days-before-accepting-my-own-answer)找出原因。 – dandan78 2011-04-29 16:47:58

0

U可以参考这个 Image in Canvas with touch events

+0

尊敬的hotveryspicy,感谢您的输入,但我可能会被误解。我可以管理平局和所有触摸事件。困难在于将三个物体重叠在一起。 – George 2011-04-27 17:45:39

+0

我的canavas是thransparent。没有下面的图像正在改变。画布填充透明 canvas.drawColor(Color.TRANSPARENT); – George 2011-04-27 17:48:14

+0

我能解决这个问题。我创建了一个班。 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); 工作正常... – George 2011-04-27 23:57:52

1

我能够做到通过做类似的事情(油漆与它后面的背景位图透​​明画布)以下(只显示部分代码):

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

希望它有帮助。