ScrollView 加入自定义画板View( onDraw())后 不显示的解决

ScrollView 加入自定义画板View( onDraw())后  不显示,

根据网上,即使模仿setListViewHeightBasedOnChildren()方法或者  从写onMeasure() 也还是无法实现;

最终我从新获取高度   重写了onKeyDown()getFocusedRect() onFocusChanged()  就出来了,当然这个方法也是从网上找到的;每次学到一点东西有博客来记着还是挺好的。
代码如下:

Fragment类:
@Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        myView= new MyView(getActivity(),2);
        View view = inflater.inflate(R.layout.pingfen_main, null);
        
        LinearLayout ll = (LinearLayout) view.findViewById(R.id.pingfen_liny_content);
            
        
        int screenHeight =
                getActivity().getWindowManager().getDefaultDisplay().getHeight();
             LinearLayout.LayoutParams llLp = new LinearLayout.LayoutParams(
                     ViewGroup.LayoutParams.MATCH_PARENT,
                    screenHeight);  
             // 2x screen height to ensure scrolling
            myView.setLayoutParams(llLp);
            ll.addView(myView);
        
        
        setupViews(view);
        return view;
    }

MyView类:
public class MyView extends View {

    private static final String title = "总评分";
    private Paint myPaint;
    private Paint mTextPaint = new Paint();
    private Rect mTempRect = new Rect();
    private int mNumRows = 5;
    private int mSelectedRow = 0;
    private final int mEstimatedPixelHeight = 5;

    private Integer mDesiredHeight = null;
    private String mLabel = null;

    public MyView(Context context, int numRows) {
        this(context, numRows, "");
    }

    public MyView(Context context, int numRows, String label) {
        super(context);
        mNumRows = numRows;
        mLabel = label;
        init();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setFocusable(true);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(10);
        mTextPaint.setColor(Color.WHITE);
    }

    public int getNumRows() {
        return mNumRows;
    }

    public int getSelectedRow() {
        return mSelectedRow;
    }

    public void setDesiredHeight(int desiredHeight) {
        mDesiredHeight = desiredHeight;
    }

    public String getLabel() {
        return mLabel;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        int desiredWidth = 300 + getPaddingLeft() + getPaddingRight();
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            return specSize;
        } else if (specMode == MeasureSpec.AT_MOST) {
            return desiredWidth < specSize ? desiredWidth : specSize;
        } else {
            return desiredWidth;
        }
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        int desiredHeight = mDesiredHeight != null ? mDesiredHeight : mNumRows
                * mEstimatedPixelHeight + getPaddingTop() + getPaddingBottom();
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            return specSize;
        } else if (specMode == MeasureSpec.AT_MOST) {
            return desiredHeight < specSize ? desiredHeight : specSize;
        } else {
            return desiredHeight;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {

        myPaint = new Paint();// 初始化画笔
        myPaint.setColor(android.graphics.Color.parseColor("#5cc6ed"));// 标题 xy轴
                                                                        // 坐标
        myPaint.setTextSize(13);
        canvas.drawText(title, 20, 20, myPaint);
        // drawText (“标题”,“位置”,“画笔”)
        // 绘制坐标

        canvas.drawLine(40, 30, 40, 310, myPaint);// 绘制纵坐标
        canvas.drawLine(80, 30, 80, 310, myPaint);// 绘制纵坐标
        canvas.drawLine(120, 30, 120, 310, myPaint);
        canvas.drawLine(160, 30, 160, 310, myPaint);
        canvas.drawLine(200, 30, 200, 310, myPaint);
        canvas.drawLine(240, 30, 240, 310, myPaint);

        canvas.drawLine(40, 310, 280, 310, myPaint);// 绘制横坐标
        int[] array = { 180, 210, 240, 270, 300, 330, 360, 390, 420, 450 };

        myPaint.setColor(android.graphics.Color.parseColor("#ef6574"));
        // 显示的纵坐标
        String[] array1 = { "", "大良", "陈村", "北滘", "伦教", "乐从", "龙江", "勒流", "容桂",
                "杏坛" };
        myPaint.setTextSize(13);// 设置字体大小
        for (int i = 0; i < array.length; i++) {
            //canvas.drawLine(50, 500 - array[i], 54, 500 - array[i], myPaint);
            canvas.drawText(array1[i], 10, 500 - array[i], myPaint);
        }
        int[] array3 = { 80, 120, 160, 200, 240 };
        // 显示的横坐标
        String[] array2 = { "20", "40", "60", "80", "100" };
        for (int i = 0; i < array2.length; i++) {
            canvas.drawText(array2[i], array3[i]-7 , 325, myPaint);
        }

        Map<String, Integer> scores = new HashMap<String, Integer>();
        scores.put("daling", 84);
        scores.put("chencun", 58);
        scores.put("beijiao", 40);
        scores.put("luenjiao", 36);
        scores.put("lecong", 80);
        scores.put("longjiang", 69);
        scores.put("leliu", 60);
        scores.put("ronggui", 65);
        scores.put("xingtan", 40);

        Map<String, Integer> xvalues = getXvalueByScores(scores);
         //柱形填充
        myPaint.setStyle(Style.FILL);
        myPaint.setColor(android.graphics.Color.parseColor("#adc44f"));
        canvas.drawRect(new Rect(40, 295, xvalues.get("daling"), 275), myPaint);

        myPaint.setColor(android.graphics.Color.parseColor("#4f80c5"));
        canvas.drawRect(new Rect(40, 265, xvalues.get("chencun"), 245), myPaint);

        myPaint.setColor(android.graphics.Color.parseColor("#834ec4"));
        canvas.drawRect(new Rect(40, 235, xvalues.get("beijiao"), 215), myPaint);

        myPaint.setColor(android.graphics.Color.parseColor("#e48545"));
        canvas.drawRect(new Rect(40, 205, xvalues.get("luenjiao"), 185),
                myPaint);

        myPaint.setColor(android.graphics.Color.parseColor("#c650be"));
        canvas.drawRect(new Rect(40, 175, xvalues.get("lecong"), 155), myPaint);

        myPaint.setColor(android.graphics.Color.parseColor("#e0d45e"));
        canvas.drawRect(new Rect(40, 145, xvalues.get("longjiang"), 125),
                myPaint);

        myPaint.setColor(android.graphics.Color.parseColor("#e15e7e"));
        canvas.drawRect(new Rect(40, 115, xvalues.get("leliu"), 95), myPaint);

        myPaint.setColor(android.graphics.Color.parseColor("#5ee066"));
        canvas.drawRect(new Rect(40, 85, xvalues.get("ronggui"), 65), myPaint);

        myPaint.setColor(android.graphics.Color.parseColor("#e15eae"));
        canvas.drawRect(new Rect(40, 55, xvalues.get("xingtan"), 35), myPaint);

    }

    private int getRowHeight() {
        return (getHeight() - getPaddingTop() - getPaddingBottom()) / mNumRows;
    }

    public void getRectForRow(Rect rect, int row) {
        final int rowHeight = getRowHeight();
        final int top = getPaddingTop() + row * rowHeight;
        rect.set(getPaddingLeft(), top, getWidth() - getPaddingRight(), top
                + rowHeight);
    }

    void ensureRectVisible() {
        getRectForRow(mTempRect, mSelectedRow);
        requestRectangleOnScreen(mTempRect);
    }

    /*
     * (non-Javadoc)
     *
     * @see android.view.KeyEvent.Callback#onKeyDown(int, android.view.KeyEvent)
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_UP:
            if (mSelectedRow > 0) {
                mSelectedRow--;
                invalidate();
                ensureRectVisible();
                return true;
            }
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (mSelectedRow < (mNumRows - 1)) {
                mSelectedRow++;
                invalidate();
                ensureRectVisible();
                return true;
            }
            break;
        }
        return false;
    }

    @Override
    public void getFocusedRect(Rect r) {
        getRectForRow(r, mSelectedRow);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction,
            Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);

        if (focused) {
            switch (direction) {
            case View.FOCUS_DOWN:
                mSelectedRow = 0;
                break;
            case View.FOCUS_UP:
                mSelectedRow = mNumRows - 1;
                break;
            case View.FOCUS_LEFT: // fall through
            case View.FOCUS_RIGHT:
                // set the row that is closest to the rect
                if (previouslyFocusedRect != null) {
                    int y = previouslyFocusedRect.top
                            + (previouslyFocusedRect.height() / 2);
                    int yPerRow = getHeight() / mNumRows;
                    mSelectedRow = y / yPerRow;
                } else {
                    mSelectedRow = 0;
                }
                break;
            default:
                // can't gleam any useful information about what internal
                // selection should be...
                return;
            }
            invalidate();
        }
    }

    @Override
    public String toString() {
        if (mLabel != null) {
            return mLabel;
        }
        return super.toString();
    }

    public Map getXvalueByScores(Map scores) {

        Map<String, Integer> xvalues = new HashMap<String, Integer>();
        Set<String> keys = scores.keySet();
        int x = 0;
        for (String key : keys) {
            x = (Integer) scores.get(key);
            xvalues.put(key, (2*x+40));
        }

        return xvalues;
    }

}





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值