首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在imageView和textView下面显示画布矩形

在imageView和textView下面显示画布矩形
EN

Stack Overflow用户
提问于 2013-10-25 09:35:56
回答 2查看 474关注 0票数 0

我有过

代码语言:javascript
复制
imageView 
TextView
Rectangle drawn from canvas

我在做

代码语言:javascript
复制
     public class MainActivity extends Activity {
        @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                drawView = new DrawView(this);
                drawView.setBackgroundColor(Color.WHITE);
                setContentView(drawView);
    setContentView(R.layout.description);// If this get invoked then how will `drawView` will get overlapped.
// then normal 


            }


            public class DrawView extends View {
                Paint paint = new Paint();

                public DrawView(Context context) {
                    super(context);            
                }

                @Override
                public void onDraw(Canvas canvas) {
                     paint.setColor(Color.BLACK);
                    paint.setStrokeWidth(3);
                    canvas.drawRect(30, 30, 80, 80, paint);
                   paint.setColor(Color.BLACK);
                    paint.setColor(Color.CYAN);
                    canvas.drawRect(33, 60, 77, 77, paint );
                    paint.setColor(Color.YELLOW);
                    canvas.drawRect(33, 33, 77, 60, paint );




                }

编辑1

代码语言:javascript
复制
protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_new_oct23);

        episode_image=(ImageView)findViewById(R.id.imageView_EpisodeImage);
        characters=(ImageView)findViewById(R.id.button_Characters);
progressBarLayout = (LinearLayout) findViewById(R.id.timerDisplay);
        DrawView drawView = new DrawView(this);
    progressBarLayout = (LinearLayout) findViewById(R.id.timerDisplay);
        DrawView drawView = new DrawView(this);
        //  drawView = new DrawView(this);
        progressBarLayout.addView(drawView);






    }

    public class DrawView extends View {
        Paint paint = new Paint();

        public DrawView(Context context) {
            super(context);            
        }

        @Override
        public void onDraw(Canvas canvas) {
            /* paint.setColor(Color.BLACK);
            paint.setStrokeWidth(3);
            canvas.drawRect(30, 30, 80, 80, paint);
           paint.setColor(Color.BLACK);
            paint.setColor(Color.CYAN);
            canvas.drawRect(33, 60, 77, 77, paint );
            paint.setColor(Color.YELLOW);
            canvas.drawRect(33, 33, 77, 60, paint );

             */
            draw(canvas);


        }


        @SuppressLint("NewApi")
        public void draw(Canvas canvas){

            int width = 0;  
            int height = 0;  
            Point size = new Point();
            WindowManager w = getWindowManager();

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)    {
                w.getDefaultDisplay().getSize(size);
                width = size.x;
                height=size.y;

            }else{
                Display d = w.getDefaultDisplay(); 
                width = d.getWidth(); 
                height=d.getHeight();
            }
            Random rnd = new Random(); 
            int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));  
            width=width-20;
            int margin=width/SIZE;
            int begin=10;

            paint.setStrokeWidth(3);    
            for (int iTmp=0;iTmp<SIZE;iTmp++){
                begin=begin+(iTmp)*(margin);
                //  begin=begin-2;
                System.out.println("onkar "+begin);

                paint.setColor(Color.CYAN);
                canvas.drawRect(begin, 20, margin-2, 30, paint);

            }

        }

    }

        //  drawView = new DrawView(this);
        progressBarLayout.addView(drawView);`
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-25 09:46:48

你应该有这样的方法:

代码语言:javascript
复制
// set content view
setContentView(R.layout.description);
// get the parent of imageview and textview, i'll suppose its LinearLayout.
LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout);
// then add your view, since its a linear layout it will automatically be added below textview. for other layouts you use LayoutParams to position your View.
ll.addView(new DrawView());
票数 0
EN

Stack Overflow用户

发布于 2013-10-25 09:46:33

您可以使用FrameLayout来堆栈视图,并使用SurfaceView作为底层视图进行绘图。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19586215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档