在视图中添加ImageView?

时间:2010-09-30 08:35:05

标签: android view imageview

我想动态地将一个ImageView或从一个xml添加到一个扩展View的类..... 请求帮助 这是我的代码...但它没有显示任何图像...... 头等舱

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);

    }
}

第二课

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 >

我正在尝试使用上面的代码,它在模拟器中崩溃了。有没有语法错误或我犯了错误的东西。请帮忙。

0 个答案:

没有答案