Android ImageView Zoom-in and Zoom-Out

前端 未结 23 2351
死守一世寂寞
死守一世寂寞 2020-11-22 09:31

I want to Zoom-in and Zoom-out an Android ImageView. I tried most of the samples but in all of them the image in the ImageView itself is getting Zoomed-in and Zoomed-out, wh

23条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 10:16

    Make two java classes

    Zoom class

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.drawable.Drawable;
    import android.view.KeyEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageButton;
    
    public class Zoom extends View {
    
        private Drawable image;
        ImageButton img,img1;
        private int zoomControler=20;
    
        public Zoom(Context context){
                super(context);
    
                image=context.getResources().getDrawable(R.drawable.j);
                //image=context.getResources().getDrawable(R.drawable.icon);
    
                setFocusable(true);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            //here u can control the width and height of the images........ this line is very important
            image.setBounds((getWidth()/2)-zoomControler, (getHeight()/2)-zoomControler, (getWidth()/2)+zoomControler, (getHeight()/2)+zoomControler);
            image.draw(canvas);
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
    
                if(keyCode==KeyEvent.KEYCODE_DPAD_UP){
                        // zoom in
                        zoomControler+=10;
                }
                if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN){
                        // zoom out
                        zoomControler-=10;
                }
                if(zoomControler<10){
                        zoomControler=10;
                }
    
                invalidate();
                return true;
        }
    }
    

    make second class

    import android.app.Activity;
    import android.os.Bundle;
    
    public class Zoomexample extends Activity {
       /** Called when the activity is first created. */
    
       @Override
       public void onCreate(Bundle icicle) {
           super.onCreate(icicle);
           setContentView(new Zoom(this));
       }
    }
    

提交回复
热议问题