Hi, I want to add an ImageView dynamically or from an xml into a class which extends View..... pleas help this is my code ...but it doesn't show any image... first class
public class Draw extends Activity {
DrawView drawView;
ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this, null);
setContentView(drawView);
}
}
2nd class
public class DrawView extends View implements OnTouchListener {
Path path;
Paint paint = new Paint();
private ShapeDrawable mDrawable;
ImageView imageView;
// image img=new image();
private ArrayList<Path> graphics = new ArrayList<Path>();
public DrawView(Context context,AttributeSet attrs) {
super(context,attrs);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
paint.setDither(true);
paint.setColor(0xFFFFFF00);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(3);
ImageView iv = (ImageView)findViewById(R.id.image_id); /* Load your ImageView
based on your id from your XML file */
iv.setImageDrawable(
this.getResources().getDrawable(R.drawable.yourimageinyourdrawablefolder)
);
// img.showImage();
}
@Override
public void onDraw(Canvas canvas) {
System.out.println("onDraw"+graphics);
for (Path path : graphics) {
//canvas.drawPoint(graphic.x, graphic.y, mPaint);
canvas.drawPath(path, paint);
}
}public boolean onTouch(View view, MotionEvent event) {
// if(event.getAction() != MotionEvent.ACTION_DOWN)
// return super.onTouchEvent(event);
if(event.getAction() == MotionEvent.ACTION_DOWN){
path = new Path();
System.out.println("ACTION_DOWN");
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
}
else if(event.getAction() == MotionEvent.ACTION_MOVE){
path.lineTo(event.getX(), event.getY());
graphics.add(path);
System.out.println("ACTION_MOVE");
path = new Path();
path.moveTo(event.getX(), event.getY());
}else if(event.getAction() == MotionEvent.ACTION_UP){
path.lineTo(event.getX(), event.getY());
graphics.add(path);
}
invalidate();
return true;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_id"
/>
</LinearLayout >
I am trying with the above code and it crashes in the emulator. Is there any syntax error or something that I have made mistake. Please help.