如何在android中模拟放大或缩小事件

时间:2011-06-15 05:47:03

标签: android events zooming

如何在android中模拟放大和缩小事件?它需要存在于服务中并在任何Android AP中启动。    例如:在图像相对AP和图像工具中放大和缩小事件。当服务启动放大或缩小事件时,图像将执行其操作。非常感谢。

1 个答案:

答案 0 :(得分:1)

可以轻松放大和缩小图像。看看这里 使用向上和向下箭头键,您可以放大和缩小图像。

<强> zooming.java

import android.app.Activity;
import android.os.Bundle;

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

<强> Zoom.java

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
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);
        }
        @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;
        }
}