mainactivity类
package com.example.androidsvg; import android.app.Activity; import android.os.Bundle; import android.view.Menu; public class MainActivity extends Activity { private static final double ZOOM_IN_SCALE = 1.25;//放大系数 private static final double ZOOM_OUT_SCALE = 0.8;//缩小系数 @SuppressWarnings("null") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new Zoom(this)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
Zoom类
package com.example.androidsvg; import android.view.View; import android.content.Context; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.view.KeyEvent; 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); //控制图像的宽度和高度 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)//放大 zoomControler+=10; if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) //缩小 zoomControler-=10; if(zoomControler<10) zoomControler=10; invalidate(); return true; } }