Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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
Java 在Android中绘制片段_Java_Android_Android Fragments_Ondraw - Fatal编程技术网

Java 在Android中绘制片段

Java 在Android中绘制片段,java,android,android-fragments,ondraw,Java,Android,Android Fragments,Ondraw,我有以下代码: import android.app.Fragment; import android.graphics.Rect; import android.graphics.Typeface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.

我有以下代码:

import android.app.Fragment;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.TextView;

public class MainFragment extends Fragment {

protected AlphaAnimation fadeIn = new AlphaAnimation(0.0f , 1.0f ); 


public MainFragment(){}


public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.main_fragment, container, false);
    String fontPath = "fonts/roboto.ttf";
    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), fontPath);

    TextView txt1 = (TextView) rootView.findViewById(R.id.headerTextView);
    txt1.setTypeface(font); 

    TextView txt2 = (TextView) rootView.findViewById(R.id.instructions);
    txt2.setTypeface(font);


    txt1.startAnimation(fadeIn);
    txt2.startAnimation(fadeIn);
    fadeIn.setDuration(1400);
    fadeIn.setFillAfter(true);

    Rect rectangle = new Rect(200, 56, 200, 112);


    return rootView;
  }
}

我想画一个长方形,但无论如何我都画不出来。我到处都找过onDraw方法,但我认为在片段中不可能实现这一点。

在活动或片段中绘制矩形没有区别。您只需要在布局中添加一个视图。你想画什么就画什么

创建自定义视图,并像这样覆盖onDraw以创建矩形

  private class Rectangle extends View{
    Paint paint = new Paint();

    public Rectangle(Context context) {
        super(context);
    }
    @Override
    public void onDraw(Canvas canvas) {
        paint.setColor(Color.GREEN);
        Rect rect = new Rect(20, 56, 200, 112);
        canvas.drawRect(rect, paint );
    }
 }
现在将此视图添加到布局中,它可以是片段或活动中设置的布局

即,R.id.container是布局id。

创建您自己的RelativeLayout类,在本例中,MyRelativeLayout扩展了RelativeLayout类

然后实现onDraw特性并添加正方形

在片段的onCreateView方法中,返回MyRelativeLayout

阅读评论

public class MainFragment extends Fragment
{
    protected AlphaAnimation fadeIn = new AlphaAnimation(0.0f, 1.0f);

    public MainFragment() { }

    /*
     *  Create a custom RelativeLayout and implement the inherited 'onDraw' 
     *  method
     */
    class MyRelativeLayout extends RelativeLayout
    {
        public MyRelativeLayout(Context context)
        {
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas)
        {
            /*
             *  Draw your rectangle
             */
            Rect rectangle = new Rect(200, 56, 200, 112);

            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.BLACK);

            canvas.drawRect(rectangle, paint);

            super.onDraw(canvas);
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        /*
         *  Create the layout
         */
        MyRelativeLayout layout = new MyRelativeLayout(getActivity());
        layout.setLayoutParams(
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
                                       ViewGroup.LayoutParams.MATCH_PARENT));

        /*
         *  Inflate your xml view
         */
        View rootView = 
            inflater.inflate(R.layout.main_fragment, container, false);

        String fontPath = "fonts/roboto.ttf";
        Typeface font = 
            Typeface.createFromAsset(getActivity().getAssets(), fontPath);

        TextView txt1 = (TextView) rootView.findViewById(R.id.headerTextView);
        txt1.setTypeface(font);

        TextView txt2 = (TextView) rootView.findViewById(R.id.instructions);
        txt2.setTypeface(font);

        txt1.startAnimation(fadeIn);
        txt2.startAnimation(fadeIn);

        fadeIn.setDuration(1400);
        fadeIn.setFillAfter(true);

        /*
         *  Add your view to the MyRelativeLayout you made, 'layout'
         */
        layout.addView(rootView);

        /*
         *  Return the 'layout' instead of just the 'rootView'
         */
        return layout;
    }
}
public class MainFragment extends Fragment
{
    protected AlphaAnimation fadeIn = new AlphaAnimation(0.0f, 1.0f);

    public MainFragment() { }

    /*
     *  Create a custom RelativeLayout and implement the inherited 'onDraw' 
     *  method
     */
    class MyRelativeLayout extends RelativeLayout
    {
        public MyRelativeLayout(Context context)
        {
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas)
        {
            /*
             *  Draw your rectangle
             */
            Rect rectangle = new Rect(200, 56, 200, 112);

            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.BLACK);

            canvas.drawRect(rectangle, paint);

            super.onDraw(canvas);
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        /*
         *  Create the layout
         */
        MyRelativeLayout layout = new MyRelativeLayout(getActivity());
        layout.setLayoutParams(
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
                                       ViewGroup.LayoutParams.MATCH_PARENT));

        /*
         *  Inflate your xml view
         */
        View rootView = 
            inflater.inflate(R.layout.main_fragment, container, false);

        String fontPath = "fonts/roboto.ttf";
        Typeface font = 
            Typeface.createFromAsset(getActivity().getAssets(), fontPath);

        TextView txt1 = (TextView) rootView.findViewById(R.id.headerTextView);
        txt1.setTypeface(font);

        TextView txt2 = (TextView) rootView.findViewById(R.id.instructions);
        txt2.setTypeface(font);

        txt1.startAnimation(fadeIn);
        txt2.startAnimation(fadeIn);

        fadeIn.setDuration(1400);
        fadeIn.setFillAfter(true);

        /*
         *  Add your view to the MyRelativeLayout you made, 'layout'
         */
        layout.addView(rootView);

        /*
         *  Return the 'layout' instead of just the 'rootView'
         */
        return layout;
    }
}