Java 在 Android 中绘制片段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23280685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing in a Fragment in Android
提问by Peter Barnett
I have the following code:
我有以下代码:
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;
}
}
I'm trying to draw a rectangle, but for the life of me can't figure it out. I've looked everywhere for the onDraw() method, but I don't believe that is possible in a fragment.
我正在尝试绘制一个矩形,但我一生都无法弄清楚。我到处寻找 onDraw() 方法,但我不相信在片段中这是可能的。
采纳答案by Libin
There is no difference in drawing a Rectangle
in Activity
or Fragment
. You just need a View
added to your layout. You can draw anything as you wish.
Rectangle
在Activity
或中绘制 a 没有区别Fragment
。您只需要View
添加到您的布局。你可以随心所欲地画任何东西。
Create a custom view and override the onDraw()
like this to create a rectangle.
创建一个自定义视图并覆盖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 );
}
}
Now add this view into your layout , it could be the layout set in Fragment or Activity
.
现在将此视图添加到您的布局中,它可能是Fragment or Activity
.
RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.container);
relativeLayout.addView(new Rectangle(getActivity()));
ie, R.id.container is the layout id.
即,R.id.container 是布局 ID。
回答by mrres1
Create your own RelativeLayout
class, in this case MyRelativeLayout
(which extends the RelativeLayout class).
RelativeLayout
在本例中创建您自己的类MyRelativeLayout
(它扩展了 RelativeLayout 类)。
Then implement the onDraw
feature and add your square.
然后实现该onDraw
功能并添加您的方块。
During the Fragment
's onCreateView
method, return the MyRelativeLayout
.
在Fragment
的onCreateView
方法中,返回MyRelativeLayout
。
Read the comments.
阅读评论。
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;
}
}