我使用Actionbar
为我的应用创建了viewpager
,但它运行正常。现在我想在每个片段上绘制一些对象。任何人都可以建议一个关于如何绘制片段的好教程或示例代码。当myclass扩展Activity时,我可以在我的视图上绘制对象,但是当它扩展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;
}
}
答案 0 :(得分:4)
这里的片段没什么特别的。
用片段写的代码 -
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();
}
}
活动代码onCreateView()
仅 -
@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();
}
片段的布局 -
<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>
编辑:检查画布上绘制线条的以下链接 -