图形中的片段的Andr​​oid [英] Drawing in a Fragment in Android

查看:142
本文介绍了图形中的片段的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的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;
  }
}

我想绘制一个矩形,但对我的生活不能弄明白。我到处找对的onDraw()方法,但我不相信这是可能的一个片段。

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.

推荐答案

有是在绘制长方形活动片段。你只需要一个查看添加到您的布局。如你愿意,你可以画任何东西。

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.

创建自定义视图和重写的onDraw()像这样以创建一个矩形。

Create a custom view and override the onDraw() like this to create a rectangle.

  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.

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

即R.id.container是布局ID。

ie, R.id.container is the layout id.

这篇关于图形中的片段的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆