SurfaceView with setContentView

时间:2014-04-24 02:18:31

标签: android

我有一个表面视图,将图像作为我的画布的背景,另一个作为拖放图像放在第一张图片的顶部。我的代码部分工作正常(除了在我触摸它时启动屏幕是黑色的)然后出现背景图像,一切正常。此活动在没有setContentView调用的情况下全屏运行,因此我想使用活动布局添加按钮以启动下一个活动。

的setContentView(R.layout.image_edit_activity);

          public class ImageEditActivity extends Activity {

        public String reportDate;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.image_edit_activity);

            Intent intent = getIntent();
            reportDate = intent.getStringExtra("reportDate");


            int w=getWindowManager().getDefaultDisplay().getWidth()-25;
            int h=getWindowManager().getDefaultDisplay().getHeight()-25;

            Crosshair crosshairView = new Crosshair(this,w,h, reportDate);


            //app works fine without this line, but I think this is the key to 
                    //have the surfaceview inside the activity layout.xml
            crosshairView = (Crosshair) findViewById(R.id.surfaceView);
        }
    }




    class Crosshair extends SurfaceView implements SurfaceHolder.Callback {
        private Bitmap bitmap ;
        private int x=20,y=20;int width,height;
        String reportDate1;

        public Crosshair(Context context,int w,int h, String reportDate) {
            super(context);

            reportDate1 = reportDate;
            width=w;
            height=h;
            getHolder().addCallback(this);
            setFocusable(true);
        }

        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/VibrationMeasurement/" + reportDate1 + ".machine" + ".jpg";  

            Bitmap bitmap2 = BitmapFactory.decodeFile(path);

            Bitmap scaled = Bitmap.createScaledBitmap(bitmap2, width, height, true);

            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.crosshair);

           // canvas.drawColor(Color.BLUE);//To make background 
            canvas.drawBitmap(scaled,0,0,null);
            canvas.drawBitmap(bitmap,x-(bitmap.getWidth()/2),y-(bitmap.getHeight()/2),null);

     //       canvas.drawBitmap(;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            x=(int)event.getX();
            y=(int)event.getY();

            if(x<25)
                x=25;
            if(x> width)   
                x=width;
            if(y <25)
                y=25;
            if(y > height)
                y=height;



            updateCrosshair();

            return true;
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
            // TODO Auto-generated method stub
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
        }

        private void updateCrosshair() {
            Canvas canvas = null;
            try {
                canvas = getHolder().lockCanvas(null);
                synchronized (getHolder()) {
                    this.onDraw(canvas);
                }
            }
            finally {
                if (canvas != null) {
                    getHolder().unlockCanvasAndPost(canvas);
                }
            }


        }
    }

这是xml文件:

                 <?xml version="1.0" encoding="utf-8"?>
          <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
       android:layout_height="fill_parent" >

          <com.effbe.accelerometer.ImageEditActivity.Crosshair
    android:id="@+id/surfaceView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="114dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/ok_button"
        android:padding="10dp"
        android:paddingLeft="10dip"
        android:text="OK"
        android:textColor="#ffffff" />
</LinearLayout>

当我尝试运行应用程序时,它会为Crosshair类抛出NoClassDefFoundError。

logcat的

 04-23 21:12:28.618: E/AndroidRuntime(27487): FATAL EXCEPTION: main
 04-23 21:12:28.618: E/AndroidRuntime(27487): Process: com.effbe.accelerometer
 04-23 21:12:28.618: E/AndroidRuntime(27487): java.lang.RuntimeException: Unable to     start activity   ComponentInfo{com.effbe.accelerometer/com.effbe.accelerometer.ImageEditActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class com.effbe.accelerometer.ImageEditActivity.Crosshair
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.effbe.accelerometer.ImageEditActivity.Crosshair
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): 
 04-23 21:12:28.618: E/AndroidRuntime(27487): Caused by: java.lang.ClassNotFoundException:  Didn't find class "com.effbe.accelerometer.ImageEditActivity.Crosshair" on path: DexPathList[[zip file "/data/app/com.effbe.accelerometer-1.apk"]

1 个答案:

答案 0 :(得分:0)

每次使用xml类时都需要三个构造函数。

    public Crosshair(Context context) {
    super(context);
    init(context);
}

public Crosshair(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public Crosshair(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}