Image的放大,缩小(zoom in,zoom out)

Java代码

packagecom.example;

importandroid.content.Context;

importandroid.graphics.Canvas;

importandroid.graphics.drawable.Drawable;

importandroid.view.KeyEvent;

importandroid.view.View;

publicclassZoomextendsView{

privateDrawableimage;

privateintzoomControler=20;

publicZoom(Contextcontext){

super(context);

image=context.getResources().getDrawable(R.drawable.icon);

setFocusable(true);

}

protectedvoidonDraw(Canvascanvas){

//TODOAuto-generatedmethodstub

super.onDraw(canvas);

//hereucancontrolthewidthandheightoftheimages........this

//lineisveryimportant

image.setBounds((getWidth()/2)-zoomControler,(getHeight()/2)

-zoomControler,(getWidth()/2)+zoomControler,

(getHeight()/2)+zoomControler);

image.draw(canvas);

}

publicbooleanonKeyDown(intkeyCode,KeyEventevent){

if(keyCode==KeyEvent.KEYCODE_DPAD_UP)//zoomin

zoomControler+=10;

if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN)//zoomout

zoomControler-=10;

if(zoomControler<10)

zoomControler=10;

invalidate();

returntrue;

}

}

相关推荐