【问题标题】:layered images plus canvas分层图像加画布
【发布时间】:2011-12-16 05:34:58
【问题描述】:

我正在尝试将两张图片放在一起。顶部是透明的。在它们之上,我正在尝试添加一个透明画布,以便用户可以绘制、键入,以及他们需要做的任何事情,并且底部的两个图像是可见的。

所以我可以让两个图像显示或只是画布。每次我尝试显示画布时,它似乎都在[覆盖它们的两个图像中的 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);

有什么建议吗?

【问题讨论】:

    标签: android android-layout android-relativelayout android-canvas


    【解决方案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); 
    

    工作正常...

    【讨论】:

    • 我必须等待 17 个小时才能接受我自己的答案...不要对这些规则非常了解...
    • 阅读this 了解为什么会这样。
    【解决方案2】:

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

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

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 亲爱的 hotveryspicy, 感谢您的意见,但我可能被误解了。我可以管理平局和所有的触摸事件。困难在于将三个物体放在一起。
      • 我的画布是透明的。以下图片均未更改。画布填充透明 canvas.drawColor(Color.TRANSPARENT);
      • 我能够解决这个问题。我创建了一个类。 public class newClass 扩展 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);工作正常...
      猜你喜欢
      • 2016-03-31
      • 2014-11-03
      • 1970-01-01
      • 2014-01-31
      • 2013-12-09
      • 2015-02-15
      • 2020-01-04
      • 2015-02-10
      • 1970-01-01
      相关资源
      最近更新 更多