gpt4 book ai didi

android - 如何在 fragment 中绘制?

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:32 25 4
gpt4 key购买 nike

我已经使用 viewpager 为我的应用程序创建了 Actionbar 并且它工作正常。现在我想在每个 fragment 上绘制一些对象。任何人都可以建议一个关于如何在 fragment 上绘制的好的教程或示例代码。当 myclass 扩展 Activity 时,我可以在我的 View 上绘制对象,但当它扩展 Fragment 时,我不知道该怎么做。

到目前为止我已经尝试过这段代码,但它不起作用

public class MoviesFragment extends Fragment {

ImageView imageView;
Bitmap bitmap; Bitmap bit;
Canvas canvas;
Paint paint;


public class DrawView extends View {
Paint paint = new Paint();

public DrawView(Context context) {
super(context);
paint.setColor(Color.BLACK);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setColor(Color.BLACK);
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint.setColor(Color.BLACK);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

imageView = (ImageView)this.findViewById(R.id.imageView);


bit= BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
bitmap=Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap,0,0,null);
paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
imageView.setImageBitmap(bitmap);
canvas.drawLine(0, 0, 250, 250, paint);
canvas.drawLine(50, 0, 0, 50, paint);

imageView.invalidate();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth, parentHeight);
}
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

View rootView = inflater.inflate(R.layout.fragment_movies, container, false);


return rootView;

}





}

最佳答案

这里的 fragment 没有什么特别的。

用 Fragment 编写的代码 -

public class DrawFragment extends Fragment {
DrawView drawView;

public DrawFragment() {
}

/**
* Returns Instance of DrawFragment
*
* @return Instance of DrawFragment
*/
public static DrawFragment getInstance() {
DrawFragment fragment = new DrawFragment();
fragment.setRetainInstance(true);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View _view = inflater.inflate(R.layout.layout_drawing_fragment,
container, false);
//lets keep a reference of DrawView
drawView = (DrawView ) _view.findViewById(R.id.drawing);
return _view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}

@Override
public void onDestroy() {
super.onDestroy();
}
}

Activity 代码 onCreateView() only-

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_base_activity);
// set Draw Fragment
mFragmentManager = getFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mDrawFragment = DrawFragment.getInstance();
mFragmentTransaction.add(R.id.frame_drawing, mDrawFragment, "draw");
mFragmentTransaction.commit();
}

fragment 的布局 -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent"
android:layout_width="fill_parent"
android:layout_height="match_parent" >

<com.app.widgets.DrawView
android:id="@+id/drawing"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:background="@android:color/transparent" >
</com.app.widgets.DrawView>

</RelativeLayout>

EDIT: Check following links for drawing lines on Canvas -

Android Canvas drawLine

How to draw a line in android

关于android - 如何在 fragment 中绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24033227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com