使用ontouch放大

时间:2010-06-14 01:27:25

标签: android zoom ontouchlistener

我已经使用了一些示例代码并试图调整它以允许用户触摸屏幕并放大代码运行正常且没有错误但是当我触摸屏幕时没有任何反应

package com.thomas.zoom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class Zoom extends View {
        private Drawable image;

        private int zoomControler=20;

        public Zoom(Context context)
        {
                super(context);
                image=context.getResources().getDrawable(R.drawable.icon);
                setFocusable(true);

        }
        @Override
        protected void onDraw(Canvas canvas) {
                // TODO Auto-generated method stub
                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);
        }
        public boolean onTouch(int action, MotionEvent event) {
               action= event.getAction();
               if(action == MotionEvent.ACTION_DOWN){
                   zoomControler+=10;               
                   }


                invalidate();
                return true;
        }
}

1 个答案:

答案 0 :(得分:1)

hi ontouch事件不适合缩放图像

所以你使用onkeylistener

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