无法绘制加载片段的圆圈

时间:2016-05-25 06:07:12

标签: java android android-fragments canvas

我想在画布上绘制圆圈。目前我可以在按钮点击时执行此操作,但是在加载Fragment时我也需要执行相同的操作。以下是我的Fragment代码。

public class StepTwentyOneFragment extends Fragment {

    private CanvasView customCanvas;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.step21_fragment, container, false);
        customCanvas=(CanvasView)v.findViewById(R.id.signature_canvas);

        final Button button1=(Button)v.findViewById(R.id.step18button1);

        float radius=(customCanvas.getCanvasWidth()/2) - ((customCanvas.getCanvasWidth()/2)/100)*60;
        customCanvas.drawCircle(radius);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(v.getId()==R.id.step18button1){

                    float radius=(customCanvas.getCanvasWidth()/2) - ((customCanvas.getCanvasWidth()/2)/100)*60;
                    customCanvas.drawCircle(radius);
                    Log.d("An_Width", "" + customCanvas.getCanvasWidth());
                    Log.d("An_Height" ,""+ customCanvas.getCanvasHeight());

                    v.setBackgroundResource(R.drawable.button_border_5);
                    button1.setTextColor(Color.WHITE);;
                }
            }
        });    

        return v;
    }

    public static StepTwentyOneFragment newInstance() {

        StepTwentyOneFragment f = new StepTwentyOneFragment();
        Bundle b = new Bundle();

        f.setArguments(b);

        return f;
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser) {
            Activity a = getActivity();
            if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }    

}

以下是我的Canvas代码

public class CanvasView extends View {

    public int width;
    public int height;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    Context context;
    private Paint mPaint;
    private float mX, mY;
    private static final float TOLERANCE = 5;
    private int canvasHeight, canvasWidth;
    private float radius;

    public CanvasView(Context c, AttributeSet attrs) {
        super(c, attrs);
        context = c;

        mPath = new Path();

        mPaint = new Paint();
        mPaint.setStrokeWidth(3);
        mPaint.setColor(Color.CYAN);
    }

    // override onDraw
    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        mCanvas=canvas;

        Drawable d = getResources().getDrawable(R.drawable.circle_1);

        canvasHeight= canvas.getHeight();
        canvasWidth= canvas.getWidth();

        Log.d("Height - "," / "+canvas.getHeight());
        Log.d("Width - "," / "+canvas.getWidth());

//         DisplayMetrics displaymetrics = new DisplayMetrics();
//        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
//        int height = displaymetrics.heightPixels;
//        int width = displaymetrics.widthPixels;

        float h=canvasHeight/2;
        float w=canvasWidth/2;


        d.setBounds(0, 0, canvasWidth, canvasHeight);
        d.draw(canvas);

        canvas.drawCircle(w, h, radius, mPaint);
    }

    public void clear2(){
        radius=0;
        //important. Refreshes the view by calling onDraw function
        invalidate();

    }

    public void drawCircle(float radius1) {

        radius=radius1;
        //important. Refreshes the view by calling onDraw function
        invalidate();

    }

    public int getCanvasHeight()
    {
        return canvasHeight;
    }

    public int getCanvasWidth()
    {
        return canvasWidth;
    }

}

现在我的问题是,无法绘制加载片段的圆圈。这意味着没有按钮动作就无法绘制圆圈。但我想在片段加载时这样做。

有任何想法吗?

谢谢。

0 个答案:

没有答案