Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 根据要求在onDraw()中使用画布设计的文本,以支持所有屏幕_Javascript_Html_Html5 Canvas - Fatal编程技术网

Javascript 根据要求在onDraw()中使用画布设计的文本,以支持所有屏幕

Javascript 根据要求在onDraw()中使用画布设计的文本,以支持所有屏幕,javascript,html,html5-canvas,Javascript,Html,Html5 Canvas,我设计了一个边栏,其中包含26个垂直字符 当我使用HVGA屏幕模拟器时,它适合于屏幕,但不适合于其他屏幕。我使用onDraw方法中的canvas设计了那个边栏 如何使此侧边栏适合所有类型的屏幕 private void init() { float GESTURE_THRESHOLD_DP = 13.9f; // Get the screen's density scale float scale = getResources().getDisplayMe

我设计了一个边栏,其中包含26个垂直字符

当我使用HVGA屏幕模拟器时,它适合于屏幕,但不适合于其他屏幕。我使用onDraw方法中的canvas设计了那个边栏

如何使此侧边栏适合所有类型的屏幕

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);  
}
}  
发布您的onDraw将有所帮助。无论如何,您必须使用getHeight/26作为参考以适应所有屏幕。看来你一直在画画