markdowneide 2013-05-09 02:14 采纳率: 0%
浏览 3305

为什么自定义图像视图的 findviewbyId 返回 null

通过设定zooming 和 boundaruy 限制我可以在屏幕上拖动和缩放 imageview 。但是我想应用为多个图像。为此我在 main.xml中使用customview。但问题是findviewbyId 总返回null。问题出在哪里呢?

 NewzoomlimitActivity .java

    public class NewzoomlimitActivity extends Activity
    {
        /** Called when the activity is first created. */
        int h, w;

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

            Display display = getWindowManager().getDefaultDisplay();
            h = display.getHeight();
            w = display.getWidth();

            CustomTouchImageView  img1 = new CustomTouchImageView( NewzoomlimitActivity.this);

            CustomTouchImageView  img11 = new CustomTouchImageView( NewzoomlimitActivity.this);

            CustomTouchImageView img = ( CustomTouchImageView ) findViewById(R.id.gIFView1  );

            Bitmap snoop = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher );

            if ( null == img )
            {
                Log.e( "img", "img is null" );
            }

            else
            {
                img.setImage( snoop, w, h );
            }

            //img1.setImage( snoop, w, h );
            //img11.setImage( snoop, w, h );
            img.setImage( snoop, w, h );
            //setContentView( img1 );
           // setContentView( img11 );
           setContentView( R.layout.main );

        }
    }

    main.xml
    ---------

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

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />

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

            <com.coders.vimek.CustomTouchImageView
                android:id="@+id/gIFView1"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_gravity="left" 
                android:src="@drawable/icon"
                />
               <com.coders.vimek.CustomTouchImageView
                android:id="@+id/gIFView2"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_gravity="left" 
                android:src="@drawable/ic_launcher"
                />
        </LinearLayout>

    </LinearLayout>

    CustomTouchImageView .java
    -----------------------------
    public class CustomTouchImageView extends ImageView
    {

        private static final String TAG         = "Touch";
        Matrix                      matrix      = new Matrix();
        Matrix                      savedMatrix = new Matrix();

        // We can be in one of these 3 states
        static final int            NONE        = 0;
        static final int            DRAG        = 1;
        static final int            ZOOM        = 2;
        int                         mode        = NONE;

        int                         marg_left   = 80, marg_right = 80, marg_top = 100, marg_botom = 130, win_w, win_h;
        Bitmap                      bmp_picture;

        // Remember some things for zooming
        PointF                      start       = new PointF();
        PointF                      mid         = new PointF();
        float                       oldDist     = 1f;

        Context                     context;

        /*
         * public TouchImageView(Context context, AttributeSet attributeSet) {
         * super(context, attributeSet);
         * 
         * //TODO: }
         */

        public CustomTouchImageView( Context context )
        {
            super( context );
            super.setClickable( true );
            this.context = context;
            init();

        }

        public CustomTouchImageView( Context context, AttributeSet attrs )
        {
            super( context, attrs );
            super.setClickable( true );
            this.context = context;
            init();
            return;
        }

        public CustomTouchImageView( Context context, AttributeSet attrs, int defStyle )
        {
            super( context, attrs, defStyle );
            super.setClickable( true );
            this.context = context;
            init();
        }

        private void init()
        {
            matrix.setTranslate( 1f, 1f );
            setImageMatri(angry) matrix );
            setScaleType( ScaleType.MATRIX );

            setOnTouchListener( new OnTouchListener()
            {

                @Override
                public boolean onTouch( View v, MotionEvent rawEvent )
                {
                    WrapMotionEvent event = WrapMotionEvent.wrap( rawEvent );

                    // Dump touch event to log
                    // if (Viewer.isDebug == true){
                    // dumpEvent(event);
                    // }

                    // Handle touch events here...
                    switch ( event.getAction() & MotionEvent.ACTION_MASK )
                    {
                    case MotionEvent.ACTION_DOWN:
                        savedMatrix.set( matrix );
                        start.set( event.getX(), event.getY() );
                        Log.d( TAG, "mode=DRAG" );
                        mode = DRAG;
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        oldDist = spacing( event );
                        Log.d( TAG, "oldDist=" + oldDist );
                        if ( oldDist > 10f )
                        {
                            savedMatrix.set( matrix );
                            midPoint( mid, event );
                            mode = ZOOM;
                            Log.d( TAG, "mode=ZOOM" );
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        int xDiff = ( int ) Math.abs( event.getX() - start.x );
                        int yDiff = ( int ) Math.abs( event.getY() - start.y );
                        if ( xDiff < 8 && yDiff < 8 )
                        {
                            performClick();
                        }

                    case MotionEvent.ACTION_POINTER_UP:

                        // //////////////////////////////////////

                        // You need this first:

                        double max_zoom = 3,
                        min_zoom = 0.4;

                        float f[] = new float[ 9 ];
                        matrix.getValues( f );

                        if ( null == bmp_picture )
                        {
                            Log.e( "bmp_picture", "bmp is null" );
                        }
                        /*
                         * else if ( null == bmp_picture.getWidth() ) {} else if (
                         * null == bmp_picture ) {}
                         */
                        else
                        {
                            if ( f[ 0 ] > max_zoom )
                            {
                                matrix.postScale( ( float ) max_zoom / f[ 0 ], ( float ) max_zoom / f[ 0 ], mid.x, mid.y );
                            }
                            if ( f[ 0 ] < min_zoom )
                            {
                                matrix.postScale( ( float ) min_zoom / f[ 0 ], ( float ) min_zoom / f[ 0 ], mid.x, mid.y );
                            }

                            if ( -f[ 2 ] > ( bmp_picture.getWidth() * f[ 0 ] - marg_left ) )
                            {
                                matrix.postTranslate( -( f[ 2 ] + bmp_picture.getWidth() * f[ 0 ] ) + marg_left, 0 );
                            }
                            if ( -f[ 5 ] > ( bmp_picture.getHeight() * f[ 0 ] - marg_top ) )
                            {
                                matrix.postTranslate( 0, -( f[ 5 ] + bmp_picture.getHeight() * f[ 0 ] ) + marg_top );
                            }

                            if ( f[ 2 ] > win_w - marg_right )
                            {
                                matrix.postTranslate( -( f[ 2 ] - win_w + marg_right ), 0 );
                            }

                            if ( f[ 5 ] > win_h - marg_botom )
                            {
                                matrix.postTranslate( 0, -( f[ 5 ] - win_h + marg_botom ) );
                            }
                        }

                        // //////////////////////////////////////

                        mode = NONE;
                        Log.d( TAG, "mode=NONE" );
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if ( mode == DRAG )
                        {
                            // ...
                            matrix.set( savedMatrix );
                            matrix.postTranslate( event.getX() - start.x, event.getY() - start.y );
                        }
                        else if ( mode == ZOOM )
                        {
                            float newDist = spacing( event );
                            Log.d( TAG, "newDist=" + newDist );
                            if ( newDist > 10f )
                            {
                                matrix.set( savedMatrix );
                                float scale = newDist / oldDist;
                                matrix.postScale( scale, scale, mid.x, mid.y );
                            }
                        }
                        break;
                    }
                    setImageMatri(angry) matrix );
                    return true; // indicate event was handled
                }

            } );

        }

        public void setImage( Bitmap bm, int displayWidth, int displayHeight )
        {
            super.setImageBitmap( bm );
            Log.e( "win_w", "2" );
            bmp_picture = bm;

            win_h = displayHeight;
            win_w = displayWidth;
            Log.e( "win_h", String.valueOf( win_h ) );

            Log.e( "win_w", String.valueOf( win_w ) );

            float scale;
            if ( ( displayHeight / bm.getHeight() ) >= ( displayWidth / bm.getWidth() ) )
            {
                scale = ( float ) displayWidth / ( float ) bm.getWidth();
            }
            else
            {
                scale = ( float ) displayHeight / ( float ) bm.getHeight();
            }

            savedMatrix.set( matrix );
            matrix.set( savedMatrix );
            matrix.postScale( scale, scale, mid.x, mid.y );
            setImageMatri(angry) matrix );
            // Center the image
            float redundantYSpace = ( float ) displayHeight - ( scale * ( float ) bm.getHeight() );
            float redundantXSpace = ( float ) displayWidth - ( scale * ( float ) bm.getWidth() );
            redundantYSpace /= ( float ) 2;
            redundantXSpace /= ( float ) 2;
            savedMatrix.set( matrix );
            matrix.set( savedMatrix );
            matrix.postTranslate( redundantXSpace, redundantYSpace );
            setImageMatri(angry) matrix );
        }

        /** Show an event in the LogCat view, for debugging */
        @SuppressWarnings( "unused" )
        private void dumpEvent( WrapMotionEvent event )
        {
            String names[] =
                { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
            StringBuilder sb = new StringBuilder();
            int action = event.getAction();
            int actionCode = action & MotionEvent.ACTION_MASK;
            sb.append( "event ACTION_" ).append( names[ actionCode ] );
            if ( actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP )
            {
                sb.append( "(pid " ).append( action >> MotionEvent.ACTION_POINTER_ID_SHIFT );
                sb.append( ")" );
            }
            sb.append( "[" );
            for ( int i = 0; i < event.getPointerCount(); i++ )
            {
                sb.append( "#" ).append( i );
                sb.append( "(pid " ).append( event.getPointerId( i ) );
                sb.append( ")=" ).append( ( int ) event.get(angry) i ) );
                sb.append( "," ).append( ( int ) event.getY( i ) );
                if ( i + 1 < event.getPointerCount() )
                    sb.append( ";" );
            }
            sb.append( "]" );
            Log.d( TAG, sb.toString() );
        }

        /** Determine the space between the first two fingers */
        private float spacing( WrapMotionEvent event )
        {
            float x = event.get(angry) 0 ) - event.get(angry) 1 );
            float y = event.getY( 0 ) - event.getY( 1 );
            return FloatMath.sqrt( x * x + y * y );
        }

        /** Calculate the mid point of the first two fingers */
        private void midPoint( PointF point, WrapMotionEvent event )
        {
            float x = event.get(angry) 0 ) + event.get(angry) 1 );
            float y = event.getY( 0 ) + event.getY( 1 );
            point.set( x / 2, y / 2 );
        }
    }
  • 写回答

1条回答 默认 最新

  • franzhong 2013-05-09 03:17
    关注

    将setContentView( R.layout.main );往前放
    放到super.onCreate( savedInstanceState );下面

    布局未指定findViewById当然不对劲

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog