views:

89

answers:

1

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.

A: 

In your XML file, place an ImageView:

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/yourimagehere"
android:id="@+id/image_id"
  />

and then, if you want to change a drawable during runtime, do like this:

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)
);
Charlie Sheen
this is my code ...but it doesn't show any image...public class DrawView extends View implements OnTouchListener { ImageView imageView; private ArrayList<Path> graphics = new ArrayList<Path>(); public DrawView(Context context,AttributeSet attrs) { super(context,attrs); imageView=new ImageView(context); imageView.setImageDrawable( this.getResources().getDrawable(R.drawable.image) ); }
antony
Please update that info inside the code tag in your question post.
Charlie Sheen
Create an ImageView in your XML file, it's easy and fast.
Charlie Sheen
Hi charlie, I can create an ImageView in my xml file, but i dont know how to load that xml file inside my class which extends View
antony
@antony: look at my edit.
Charlie Sheen
@ Charlie Sheen. Let me try and inform here. Thanks
antony
@ Charlie Sheen: I have edited with my new code. Can you please have a look and tell me suggestions
antony
Your XML isn't fulfilled. Can you post the full XML source?
Charlie Sheen
@ Charlie Sheen: I have added XML file, but itz not showing in the post. Nyway I am pasting here. Please look. <?xml version="1.0" encoding="utf-8"?><AbsoluteLayout 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" ><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/image_id" /></AbsoluteLayout>
antony
Please use http://pastebin.com/ or a similiar web site. Do not post full source code in comments.
Charlie Sheen
AbsoluteLayout is deprecated, use LinearLayout or RelativeLayout.
Charlie Sheen
I hav changed the layout. But it is still crashing. Should I change anything in my class to load xml?
antony
@Charlie Sheen : Can you please help me here
antony
Post full source code inside code tag or at www.pastebin.com if your editing not works.
Charlie Sheen
@ Charlie Sheen: I have posted full source code ........
antony
@ Charlie Sheen: I cant post xml code using the <code> and <pre> tags. XML content is same as I added as comment. Can you please look
antony
Since I'm not comfortable with Canvas stuff I can't tell you. Your question is different now and you should consider to write more complete questions in the future. At the moment, you have to wait for another response.
Charlie Sheen
@ Charlie Sheen: Ok..then leave canvas. can u tell hw to load an xml file into a class which extends the View
antony
You load it as I answered in my post.
Charlie Sheen
but its working only in class which extends activity........
antony
You may use some kind of inflater (I don't know). You can locate the error easier if you turn on the DDMS and copy the error message.
Charlie Sheen
Thanx for ur time........
antony