zoukankan      html  css  js  c++  java
  • android点击实现图片放大缩小 java技术博客

    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;  
          }  
     }
    路漫漫其修远兮 吾将上下而求索
  • 相关阅读:
    用 Mac 给树莓派重装系统
    Python:matplotlib 中文乱码的解决方案
    Python: 通过 pip 安装第三方包后依然不能 import
    Python: 安装 sklearn 包出现错误的解决方法
    grub2引导安装kali2.0及安装ibus拼音输入法
    2015移动安全挑战赛 第一题
    Linux kali 3.14-kali1-amd64 编译安装 wine 1.7.33
    git clone https协议问题
    常见问题
    Sieve_of_Eratosthenes
  • 原文地址:https://www.cnblogs.com/hudabing/p/3075355.html
Copyright © 2011-2022 走看看