I have created Actionbar
for my app with viewpager
and it works properly. Now I want to draw some objects on each fragment. Can anyone suggest a good tutorial or sample code on how to draw on fragment. I can draw objects on my view when myclass extends Activity but I don't know what to do when it extends Fragment.
I have tried this code till now but it does not work
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;
}
}