Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7964387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:52:43+00:00 2026-06-04T05:52:43+00:00

Using this i can drag and zoom the imageview with in the screen by

  • 0

Using this i can drag and zoom the imageview with in the screen by giving zoominglimits and boundaruy limits.But i want it for multiple images.For that i used the customview in the main.xml.But the problem is its findviewbyId returns null.What could be the problem? what am i missing here? Please help
PS:- im a 2months old android developer

 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:52:44+00:00Added an answer on June 4, 2026 at 5:52 am

    You are calling

    findViewById()
    

    before the

    setContentView( R.layout.main );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want DBSession.query(Article).group_by(Article.created.month).all() But this query can't using How do I do this using
i'm using this code below to drag an image across the screen. The one
using this mark up and script I can create drag file to desktop links
Using this: Can I use Facebook's fb:friend-selector in an iframe? I created a multi-friend
Not sure if yall can help this time, as I'm just using this particular
This can certainly be done using a simple script and reading the database. I
While using this code, i can't get any results... Ext.define('Teste.view.Main', { extend: 'Ext.Container', initialize:
Can not parse json using this var res = eval('(' + response + ')');
I know this can be done and i have seen it done using some
I can draw many things using this : NSString *imagePath = [[NSBundle mainBundle] pathForResource:@dummy2.png

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.