绘制矩形和刻度以适应屏幕

时间:2017-03-03 10:35:34

标签: android scale draw

这是我的小应用程序。它在我的设备中看起来很完美但是当我在另一个尺寸屏幕的设备中使用时,矩形不适合屏幕边缘。我尝试将pix转换为dp,但效果不佳。

public class MyView extends View  {
private static final String TAG = "MyActivity";
Paint paint;
private Rect rectangle;
Paint paint2;
private Rect rectangle2;
Paint paint3;
private Rect rectangle3;
Paint paint4;
private Rect rectangle4;
Paint paint5;
private Rect rectangle5;
Paint paint6;
private Rect rectangle6;
Paint paint7;
private Rect rectangle7;
Paint paint8;
private Rect rectangle8;
Paint paint9;
private Rect rectangle9;
Paint paint10;
private Rect rectangle10;
Paint paint11;
private Rect rectangle11;
Paint paint12;
private Rect rectangle12;
Paint paint13;
private Rect rectangle13;
Paint paint14;
private Rect rectangle14;
Paint paint15;
private Rect rectangle15;
Paint paint16;
private Rect rectangle16;
Paint colorPicker1;
private Rect rectangleRed;
Paint colorPicker2;
private Rect rectangleYellow;
Paint colorPicker3;
private Rect rectangleBlue;
Paint colorPicker4;
private Rect rectangleWhite;
int xStored;
int yStored;
int colorStore1;

public MyView(Context context) {
    super(context);
    init();
}

private void init(){
    Log.i(TAG, "hola "+ screen);
    paint = new Paint();
    paint.setColor(Color.WHITE);
    rectangle = new Rect(0, 0, 100, 100);
    paint2 = new Paint();
    paint2.setColor(Color.WHITE);
    rectangle2 = new Rect(110, 0, 250, 100);
    paint3 = new Paint();
    paint3.setColor(Color.WHITE);
    rectangle3 = new Rect(260, 0, 340, 100);
    paint4 = new Paint();
    paint4.setColor(Color.WHITE);
    rectangle4 = new Rect(350, 0, 480, 100);
    paint5 = new Paint();
    paint5.setColor(Color.WHITE);
    rectangle5 = new Rect(0, 110, 100, 260);
    paint6 = new Paint();
    paint6.setColor(Color.WHITE);
    rectangle6 = new Rect(110, 110, 250, 260);
    paint7 = new Paint();
    paint7.setColor(Color.WHITE);
    rectangle7 = new Rect(260, 110, 340, 260);
    paint8 = new Paint();
    paint8.setColor(Color.WHITE);
    rectangle8 = new Rect(350, 110, 480, 260);
    paint9 = new Paint();
    paint9.setColor(Color.WHITE);
    rectangle9 = new Rect(0, 270, 100, 340);
    paint10 = new Paint();
    paint10.setColor(Color.WHITE);
    rectangle10 = new Rect(110, 270, 250, 340);
    paint11 = new Paint();
    paint11.setColor(Color.WHITE);
    rectangle11 = new Rect(260, 270, 340, 340);
    paint12 = new Paint();
    paint12.setColor(Color.WHITE);
    rectangle12 = new Rect(350, 270, 480, 340);
    paint13 = new Paint();
    paint13.setColor(Color.WHITE);
    rectangle13 = new Rect(0, 350, 100, 440);
    paint14 = new Paint();
    paint14.setColor(Color.WHITE);
    rectangle14 = new Rect(110, 350, 250, 440);
    paint15 = new Paint();
    paint15.setColor(Color.WHITE);
    rectangle15 = new Rect(260, 350, 340, 440);
    paint16 = new Paint();
    paint16.setColor(Color.WHITE);
    rectangle16 = new Rect(350, 350, 480, 440);

    colorPicker1 = new Paint();
    colorPicker1.setColor(Color.RED);
    rectangleRed= new Rect(0, 580 , 120+ screen, 680);
    colorPicker2 = new Paint();
    colorPicker2.setColor(Color.YELLOW);
    rectangleYellow = new Rect(120+ screen, 580, 240+ screen2, 680);
    colorPicker3 = new Paint();
    colorPicker3.setColor(Color.BLUE);
    rectangleBlue = new Rect(240+ screen3, 580, 360,680 );
    colorPicker4 = new Paint();
    colorPicker4.setColor(Color.WHITE);
    rectangleWhite = new Rect(360+ screen, 580, 480+ screen, 680);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(BLACK);
    canvas.drawRect(rectangle, paint);
    canvas.drawRect(rectangle2, paint2);
    canvas.drawRect(rectangle3, paint3);
    canvas.drawRect(rectangle4, paint4);
    canvas.drawRect(rectangle5, paint5);
    canvas.drawRect(rectangle6, paint6);
    canvas.drawRect(rectangle7, paint7);
    canvas.drawRect(rectangle8, paint8);
    canvas.drawRect(rectangle9, paint9);
    canvas.drawRect(rectangle10, paint10);
    canvas.drawRect(rectangle11, paint11);
    canvas.drawRect(rectangle12, paint12);
    canvas.drawRect(rectangle13, paint13);
    canvas.drawRect(rectangle14, paint14);
    canvas.drawRect(rectangle15, paint15);
    canvas.drawRect(rectangle16, paint16);
    canvas.drawRect(rectangleRed, colorPicker1);
    canvas.drawRect(rectangleYellow,colorPicker2 );
    canvas.drawRect(rectangleBlue,colorPicker3 );
    //canvas.drawRect(rectangleWhite,colorPicker4 );
}

@Override
public boolean onTouchEvent( MotionEvent event) {
    super.onTouchEvent(event);

    int x = (int)event.getX();
    int y = (int)event.getY();
    xStored = x; yStored=y;
    if (event.getAction()==MotionEvent.ACTION_UP){

    }else if(event.getAction()==MotionEvent.ACTION_DOWN){

            if(rectangleRed.contains(x,y)){
            colorStore1 = colorPicker1.getColor();
                              }
            if(rectangleYellow.contains(x,y)){
                colorStore1 = colorPicker2.getColor();
            }
            if (rectangleBlue.contains(x,y)){
                colorStore1 = colorPicker3.getColor();
            }
            if (rectangleWhite.contains(x,y)){
                colorStore1 = colorPicker4.getColor();
            }
        if(rectangle.contains(x,y)){
            paint.setColor(colorStore1);
        }
        if(rectangle2.contains(x,y)){
            paint2.setColor(colorStore1);
        }
        if(rectangle3.contains(x,y)){
            paint3.setColor(colorStore1);
        }
        if(rectangle4.contains(x,y)){
            paint4.setColor(colorStore1);
        }
        if(rectangle5.contains(x,y)){
            paint5.setColor(colorStore1);
        }
        if(rectangle6.contains(x,y)){
            paint6.setColor(colorStore1);
        }
        if(rectangle7.contains(x,y)){
            paint7.setColor(colorStore1);
        }
        if(rectangle8.contains(x,y)){
            paint8.setColor(colorStore1);
        }
        if(rectangle9.contains(x,y)){
            paint9.setColor(colorStore1);
        }
        if(rectangle10.contains(x,y)){
            paint10.setColor(colorStore1);
        }
        if(rectangle11.contains(x,y)){
            paint11.setColor(colorStore1);
        }
        if(rectangle12.contains(x,y)){
            paint12.setColor(colorStore1);
        }
        if(rectangle13.contains(x,y)){
            paint13.setColor(colorStore1);
        }
        if(rectangle14.contains(x,y)){
            paint14.setColor(colorStore1);
        }
        if(rectangle15.contains(x,y)){
            paint15.setColor(colorStore1);
        }
        if(rectangle16.contains(x,y)){
            paint16.setColor(colorStore1);
        }

    }else if(event.getAction()==MotionEvent.ACTION_MOVE){

    }
    this.postInvalidate();
    return true;
  }
 }

enter image description here

1 个答案:

答案 0 :(得分:0)

使用View的getWidth()和getHeight()方法在运行时获取宽度和高度,与实际屏幕匹配。