在Android中绘制片段

我有以下代码:

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()方法,但我不相信片段中有可能。

ActivityFragment中绘制Rectangle没有区别。 您只需要在布局中添加一个View 。 你可以随意画出任何东西。

创建一个自定义视图并覆盖这样的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 ); } } 

现在将此视图添加到您的布局中,它可以是Fragment or Activity设置的布局。

 RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.container); relativeLayout.addView(new Rectangle(getActivity())); 

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

创建自己的RelativeLayout类,在本例中为MyRelativeLayout (扩展RelativeLayout类)。

然后实现onDrawfunction并添加你的方块。

FragmentonCreateView方法中,返回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; } }