2012-10-19 4 views
0

뷰 내에 일련의 직사각형 모양을 그리는 사용자 정의보기가 있습니다. 뷰를 확장하고 onDraw 메서드를 재정의했습니다.android의 다른 화면 크기에 대한 사용자 정의 모양

이 생성자 (추가 단지 중요한 부분)

public CustomDrawableView(Context mContext , AttributeSet attr) 
    { 
     super(mContext,attr); 

     //setMeasuredDimension(measuredWidth, measuredHeight) 
     Log.e("CustomDrableView", "widht"+attr.getAttributeName(5)); 
     Log.e("CustomDrableView", "widht2"+attr.getAttributeValue(5)); 



     mColors = new int[] { 
      //color codes 
      }; 


     mColors_state_init = new int[] 
       { 
        //color codes 
       }; 

     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 

     mPaint2 = new Paint(mPaint); 
     mPaint2.setAlpha(64); 

     float[] radii = {15,15,15,15,15,15,15,15}; 
     mDrawable = new ShapeDrawable(new RoundRectShape(radii, null, null)); 


     // mDrawable.getPaint().setColor(0xff74AC23); 
     ShapeDrawable prev = mDrawable; 
     mDrawables = new ShapeDrawable[15]; 

     for (int i = 0; i < 15; i++) { 
      mDrawables[i] = new ShapeDrawable(new RoundRectShape(radii, null, null)); 
     } 
} 

이 너비와 높이 값이 다양한 화면 크기로 변경해야 된 onDraw

protected void onDraw(Canvas canvas) {  
    mDrawable.setBounds(x, y, x + width, y + height); 
     for (int i = 0; i < 15; i++) { 

     mDrawables[i].getPaint().setColor(0xff74AC23); 
     mDrawables[i].setDither(true); 
     mDrawables[i].setBounds(x, y + (i *20), x+width, y+height+(i*20)); 
     } 


    for(int i = 0 ; i < 15 ; i++) 
    { 
     ColorFilter filter = null; 

     if (mColors[i] == 0) { 
        filter = null; 
       } else { 
        filter = new PorterDuffColorFilter(mColors_test[i], 
          PorterDuff.Mode.SRC_ATOP); 


     } 


      mDrawables[i].setColorFilter(filter); 
      mDrawables[i].draw(canvas); 
    } 

이다. 어떻게하면 될까요?

xml에서보기의 정적 폭과 높이를 지정하고 있습니다. 나는 그것을 피해야합니까?

답변

0

우선, 정적 너비와 높이를 xml에 설정하지 말고 wrap_content로 지정해야합니다. 그런 다음 이렇게하는 대신

float[] radii = {15,15,15,15,15,15,15,15}; 

screenSize에 따라 15에 해당하는 값을 찾습니다. 이를 달성하기 위해서는 화면 크기를 얻어야합니다. 일단 당신이 screenSizes있어, 당신은 비례 크기하실 수 있습니다.

public void GetDimensions() { 
    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    width = metrics.widthPixels; 
    height = metrics.heightPixels; 
} 
: 당신은 화면 크기를 얻는 방법을 모르는 경우
int calculatedValue = screenWidth * 15/320; // I assume 320 as a default screen width here 
float[] radii = {calculatedValue, calculatedValue, calculatedValue, calculatedValue, calculatedValue, calculatedValue, calculatedValue, calculatedValue} 

그리고 경우에

가 ... 여기 싹둑 : 그것은이 같은 수
관련 문제