el texto diseñado usando lienzo en onDraw () según el requisito para admitir todas las pantallas

I designed a sidebar, which contains 26 characters vertically.

It is fitted to the screen when I use HVGA screen (emulator) but it is not fitting to the other screens. I designed that sidebar using canvas in onDraw() método.

How can I fit this sidebar to all types of screens?

private void init() {  
     float GESTURE_THRESHOLD_DP = 13.9f;
     // Get the screen's density scale
        float scale = getResources().getDisplayMetrics().density;
     // Convert the dps to pixels, based on density scale
     mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);
     // Use mGestureThreshold as a distance in pixels...
    l = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',  
            'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };  
    setBackgroundColor(0x44FFFFFF);  
}    
public SideBar(Context context, AttributeSet attrs, int defStyle) {  
    super(context, attrs, defStyle);  
    init();  
}  
public void setListView(ListView _list) {  
    list = _list;  
    sectionIndexter = (SectionIndexer) _list.getAdapter();  
}  
public boolean onTouchEvent(MotionEvent event) {  
    super.onTouchEvent(event);         
    int i = (int) event.getY();  
    System.out.println("i is:"+i);
    int idx = i / m_nItemHeight;      
    if (idx >= l.length) {  
        idx = l.length - 1;           
    } else if (idx < 0) {           
        idx = 0;  
    }  
    if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {         
        if (sectionIndexter == null) {              
            sectionIndexter = (SectionIndexer) list.getAdapter();  
        }  
        int position = sectionIndexter.getPositionForSection(l[idx]);           
        if (position == -1) {       
            return true;  
        }          
        list.setSelection(position);  
    }  
    return true;  
}
protected void onDraw(Canvas canvas) {  
    Paint paint = new Paint(); 
    float f;
    paint.setColor(Color.BLACK);  
    paint.setTextSize(18);  
   float scale = (float) getWidth();
   System.out.println(""+Paint.FAKE_BOLD_TEXT_FLAG);
    paint.setTextAlign(Paint.Align.CENTER);  
    paint.setAntiAlias(true);
    float widthCenter = getMeasuredWidth() /2;  
    for (int i = 0; i < l.length; i++) {  
        canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight + (i * m_nItemHeight), paint);  
    }  
    super.onDraw(canvas);  
}
}  

preguntado el 31 de enero de 12 a las 08:01

1 Respuestas

Publicando tu onDraw() would help. Anyway, you must use getHeight() / 26 as a reference to fit all screens. It seems you drew with constant measures.

Respondido el 26 de junio de 16 a las 01:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.